main.c 264 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351
  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.01.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. if (getTargetChargingInfoData(0)->SystemStatus == SYS_MODE_CHARGING
  3843. && getTargetChargingInfoData(1)->SystemStatus == SYS_MODE_CHARGING)
  3844. result = TRUE;
  3845. return result;
  3846. }
  3847. void checkConnectionTimeout()
  3848. {
  3849. if ((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  3850. {
  3851. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  3852. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING);
  3853. }
  3854. else
  3855. {
  3856. ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
  3857. if ((ShmCharger->timeoutSpec.Setting_Timeout_Spec) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  3858. {
  3859. ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE + 10);
  3860. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  3861. }
  3862. else
  3863. {
  3864. ShmCharger->timeoutSpec.Present_Timeout_Spec = ShmCharger->timeoutSpec.Setting_Timeout_Spec;
  3865. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  3866. }
  3867. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3868. }
  3869. }
  3870. void checkReset()
  3871. {
  3872. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3873. {
  3874. if (ShmOCPP16Data->MsMsg.bits.ResetReq)
  3875. {
  3876. if ((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE))
  3877. && (modelnameInfo.GetGunCount > 1 ? (!isMode(1, SYS_MODE_CHARGING)
  3878. && !isMode(1, SYS_MODE_TERMINATING)
  3879. && !isMode(1, SYS_MODE_COMPLETE)) : TRUE))
  3880. {
  3881. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  3882. sprintf((char*) ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3883. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  3884. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  3885. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  3886. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3887. if (strcmp((char*) ShmOCPP16Data->Reset.Type, "Hard") == 0)
  3888. {
  3889. system("sync");
  3890. sleep(10);
  3891. system("reboot -f");
  3892. sleep(10);
  3893. system("reboot -f");
  3894. }
  3895. else
  3896. {
  3897. sleep(10);
  3898. close(wtdFd);
  3899. system("/usr/bin/run_evse_restart.sh");
  3900. }
  3901. }
  3902. }
  3903. }
  3904. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3905. {
  3906. if (ShmOCPP20Data->MsMsg.bits.ResetReq)
  3907. {
  3908. if ((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE))
  3909. && (modelnameInfo.GetGunCount > 1 ? (!isMode(1, SYS_MODE_CHARGING)
  3910. && !isMode(1, SYS_MODE_TERMINATING)
  3911. && !isMode(0, SYS_MODE_COMPLETE)) : TRUE))
  3912. {
  3913. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  3914. sprintf((char*) ShmOCPP20Data->Reset.Response_status, "Accepted");
  3915. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  3916. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  3917. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  3918. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3919. if (strcmp((char*) ShmOCPP20Data->Reset.type, "Immediate") == 0)
  3920. {
  3921. system("sync");
  3922. sleep(10);
  3923. system("reboot -f");
  3924. sleep(10);
  3925. system("reboot -f");
  3926. }
  3927. else
  3928. {
  3929. sleep(10);
  3930. close(wtdFd);
  3931. system("/usr/bin/run_evse_restart.sh");
  3932. }
  3933. }
  3934. }
  3935. }
  3936. }
  3937. void checkReservation(uint8_t gun_index)
  3938. {
  3939. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3940. {
  3941. if (ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  3942. {
  3943. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3944. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3945. if (isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3946. {
  3947. sleep(5);
  3948. getTargetChargingInfoData(gun_index)->ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3949. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3950. }
  3951. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3952. }
  3953. }
  3954. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3955. {
  3956. if (ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  3957. {
  3958. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3959. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3960. if (isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3961. {
  3962. getTargetChargingInfoData(gun_index)->ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  3963. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3964. }
  3965. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3966. }
  3967. }
  3968. }
  3969. void checkUnlocker(uint8_t gun_index)
  3970. {
  3971. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3972. {
  3973. if (ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3974. {
  3975. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3976. sprintf( (char*) ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].ResponseStatus, "Unlocked");
  3977. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].UnlockConnectorConf = ON;
  3978. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].isUnlockerConnetor = ON;
  3979. }
  3980. }
  3981. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3982. {
  3983. if (ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3984. {
  3985. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3986. sprintf((char*) ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].Response_status, "UnlockFailed");
  3987. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].UnlockConnectorConf = ON;
  3988. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].isUnlockerConnetor = ON;
  3989. }
  3990. }
  3991. }
  3992. void checkAvailability(uint8_t gun_index)
  3993. {
  3994. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3995. {
  3996. if (ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3997. {
  3998. if (strcmp((char*) ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  3999. {
  4000. DB_Update_Operactive(localDb, gun_index, true);
  4001. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  4002. }
  4003. else
  4004. {
  4005. DB_Update_Operactive(localDb, gun_index, false);
  4006. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  4007. }
  4008. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  4009. }
  4010. }
  4011. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4012. {
  4013. if (ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  4014. {
  4015. if (strcmp((char*) ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  4016. {
  4017. DB_Update_Operactive(localDb, gun_index, true);
  4018. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  4019. }
  4020. else
  4021. {
  4022. DB_Update_Operactive(localDb, gun_index, false);
  4023. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  4024. }
  4025. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  4026. }
  4027. }
  4028. }
  4029. void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
  4030. {
  4031. uint16_t MaxChargingProfileChargingCurrent = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  4032. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4033. {
  4034. // Get profile charging profile limit
  4035. if ((ocpp_get_smartcharging_profileId(gun_index) > 0))
  4036. {
  4037. // Checking profile kind
  4038. if ((mystrcmp((char*) ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  4039. {
  4040. // Checking limitation
  4041. for (uint8_t idx_period = 0; idx_period < ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod); idx_period++)
  4042. {
  4043. if ((getScheduleStart(gun_index) >= ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod)
  4044. && ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0)))
  4045. {
  4046. //lwtest
  4047. ShmCharger->gun_info[gun_index].ChargingProfilePower = AC_OUTPUT_VOL
  4048. * ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit
  4049. * ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases;
  4050. ShmCharger->gun_info[gun_index].ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit;
  4051. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].ChargingProfileCurrent;
  4052. }
  4053. else
  4054. break;
  4055. }
  4056. }
  4057. }
  4058. else
  4059. {
  4060. //lwtest
  4061. if (isTwoGunInChargingMode())
  4062. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.5;
  4063. else
  4064. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  4065. //printf("Gun %d charging targetCurrent %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4066. }
  4067. // Get max charging profile limit
  4068. if ((ocpp_get_maxcharging_profileId() > 0))
  4069. {
  4070. // Absolute profile
  4071. if ((mystrcmp((char*) ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == PASS))
  4072. {
  4073. for (uint8_t idx_period = 0; idx_period < ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod); idx_period++)
  4074. {
  4075. if ((getMaxScheduleStart() >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod)
  4076. && ((idx_period == 0) || (ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0)))
  4077. {
  4078. //lwtest
  4079. MaxChargingProfileChargingCurrent = ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit;
  4080. // printf("Gun %d Get Max Profile limit = %.2f\n", gun_index,
  4081. // ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  4082. }
  4083. else
  4084. break;
  4085. }
  4086. }
  4087. }
  4088. // Compare target current is over max current for EVSE limit
  4089. //lwtest
  4090. if (isTwoGunInChargingMode() == FALSE)
  4091. {
  4092. //AC type or only one connector in charging
  4093. if (ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  4094. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  4095. }
  4096. else
  4097. {
  4098. uint8_t _otherIndex = 0;
  4099. _otherIndex = (gun_index > 0) ? 0 : 1;
  4100. if ((ShmCharger->gun_info[_otherIndex].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent)
  4101. > MaxChargingProfileChargingCurrent)
  4102. {
  4103. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent
  4104. - ShmCharger->gun_info[_otherIndex].targetCurrent);
  4105. }
  4106. }
  4107. }
  4108. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4109. {
  4110. // Get profile charging profile limit
  4111. if ((ocpp_get_smartcharging_profileId(gun_index) > 0))
  4112. {
  4113. // Checking profile kind
  4114. if ((mystrcmp((char*) ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  4115. {
  4116. // Checking limitation
  4117. for (uint8_t idx_period = 0; idx_period < ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod); idx_period++)
  4118. {
  4119. if ((getScheduleStart(gun_index) >= ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod)
  4120. && ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0)))
  4121. {
  4122. ShmCharger->gun_info[gun_index].ChargingProfilePower = AC_OUTPUT_VOL
  4123. * ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit
  4124. * ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases;
  4125. ShmCharger->gun_info[gun_index].ChargingProfileCurrent = ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit;
  4126. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].ChargingProfileCurrent;
  4127. }
  4128. else
  4129. break;
  4130. }
  4131. }
  4132. }
  4133. else
  4134. {
  4135. //lwtest
  4136. if (isTwoGunInChargingMode())
  4137. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.5;
  4138. else
  4139. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  4140. }
  4141. // Get max charging profile limit
  4142. if ((ocpp_get_maxcharging_profileId() > 0))
  4143. {
  4144. // Absolute profile
  4145. if ((mystrcmp((char*) ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, "Absolute") == PASS))
  4146. {
  4147. // Checking limitation
  4148. for (uint8_t idx_period = 0; idx_period < ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod); idx_period++)
  4149. {
  4150. if ((getMaxScheduleStart() >= ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod)
  4151. && ((idx_period == 0) || (ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0)))
  4152. {
  4153. //lwtest
  4154. MaxChargingProfileChargingCurrent = ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit;
  4155. }
  4156. else
  4157. break;
  4158. }
  4159. }
  4160. }
  4161. // Compare target current is over max current for EVSE limit
  4162. //lwtest
  4163. if (isTwoGunInChargingMode() == FALSE)
  4164. {
  4165. //AC type or only one connector in charging
  4166. if (ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  4167. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  4168. }
  4169. else
  4170. {
  4171. uint8_t _otherIndex = 0;
  4172. _otherIndex = (gun_index > 0) ? 0 : 1;
  4173. if ((ShmCharger->gun_info[_otherIndex].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent)
  4174. > MaxChargingProfileChargingCurrent)
  4175. {
  4176. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent
  4177. - ShmCharger->gun_info[_otherIndex].targetCurrent);
  4178. }
  4179. }
  4180. }
  4181. // Charging session target current check if OCPP disconnect and power sharing server connected
  4182. if (ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > 0)
  4183. {
  4184. if (ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  4185. ShmCharger->gun_info[gun_index].targetCurrent =
  4186. ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index] > ShmSysConfigAndInfo->SysConfig.RatingCurrent
  4187. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index];
  4188. else
  4189. {
  4190. if (ShmCharger->gun_info[gun_index].targetCurrent != 0)
  4191. DEBUG_WARN("Disconnect from power sharing server, target current set to 0.\n");
  4192. ShmCharger->gun_info[gun_index].targetCurrent = 0;
  4193. }
  4194. }
  4195. switch (system_mode)
  4196. {
  4197. case SYS_MODE_IDLE:
  4198. if (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4199. {
  4200. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4201. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4202. if (ShmCharger->isCcsEnable)
  4203. {
  4204. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  4205. }
  4206. else
  4207. {
  4208. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4209. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4210. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4211. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4212. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4213. ? 6 : ((ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4214. }
  4215. }
  4216. else
  4217. {
  4218. ShmCharger->gun_info[gun_index].targetCurrent =
  4219. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4220. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4221. if (ShmCharger->isCcsEnable)
  4222. {
  4223. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  4224. }
  4225. else
  4226. {
  4227. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4228. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4229. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4230. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4231. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4232. ? 6 : ((ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4233. }
  4234. }
  4235. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4236. break;
  4237. case SYS_MODE_PREPARING:
  4238. if (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4239. {
  4240. ShmCharger->gun_info[gun_index].targetCurrent =
  4241. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4242. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4243. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4244. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4245. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4246. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4247. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4248. ? 6 : ( (ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4249. }
  4250. else
  4251. {
  4252. ShmCharger->gun_info[gun_index].targetCurrent =
  4253. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4254. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4255. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4256. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4257. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4258. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4259. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4260. ? 6 : ( (ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4261. }
  4262. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4263. break;
  4264. case SYS_MODE_CHARGING:
  4265. case SYS_MODE_TERMINATING:
  4266. if (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4267. {
  4268. ShmCharger->gun_info[gun_index].targetCurrent =
  4269. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4270. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4271. if ((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  4272. || (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4273. {
  4274. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4275. {
  4276. if (getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4277. {
  4278. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4279. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4280. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4281. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4282. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4283. ? 6 : ((ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4284. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4285. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4286. }
  4287. }
  4288. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4289. {
  4290. ShmCharger->gun_info[gun_index].targetCurrent =
  4291. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4292. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4293. ShmCharger->gun_info[gun_index].targetCurrent =
  4294. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4295. ? 6 : ShmCharger->gun_info[gun_index].targetCurrent);
  4296. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float) ShmCharger->gun_info[gun_index].targetCurrent;
  4297. }
  4298. }
  4299. }
  4300. else
  4301. {
  4302. ShmCharger->gun_info[gun_index].targetCurrent =
  4303. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4304. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4305. if ((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  4306. || (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4307. {
  4308. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4309. {
  4310. if (getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4311. {
  4312. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4313. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4314. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4315. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4316. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4317. ? 6 : ((ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4318. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4319. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4320. }
  4321. }
  4322. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4323. {
  4324. ShmCharger->gun_info[gun_index].targetCurrent =
  4325. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4326. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4327. ShmCharger->gun_info[gun_index].targetCurrent =
  4328. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4329. ? 6 : ShmCharger->gun_info[gun_index].targetCurrent);
  4330. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float) ShmCharger->gun_info[gun_index].targetCurrent;
  4331. }
  4332. }
  4333. }
  4334. if (ShmCharger->gun_info[gun_index].targetCurrent != previousData[gun_index].targetCurrent)
  4335. {
  4336. DEBUG_INFO("==================================================\n");
  4337. DEBUG_INFO("Gun-%02d targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4338. DEBUG_INFO("MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4339. DEBUG_INFO("==================================================\n");
  4340. previousData[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent;
  4341. }
  4342. if (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != previousData[gun_index].current_limit)
  4343. {
  4344. DEBUG_INFO("==================================================\n");
  4345. DEBUG_INFO("Gun-%02d mcu current_limit: %d\n", gun_index,
  4346. ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  4347. DEBUG_INFO("==================================================\n");
  4348. previousData[gun_index].current_limit = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  4349. }
  4350. //lwtest
  4351. if (ShmCharger->gun_info[gun_index].ChargingProfileCurrent != previousData[gun_index].ChargingProfileCurrent)
  4352. {
  4353. DEBUG_INFO("==================================================\n");
  4354. DEBUG_INFO("Gun-%02d Profile limit : %.2f\n", gun_index,
  4355. ShmCharger->gun_info[gun_index].ChargingProfileCurrent);
  4356. DEBUG_INFO("Gun-%02d Profile power : %.2f\n", gun_index,
  4357. ShmCharger->gun_info[gun_index].ChargingProfilePower);
  4358. DEBUG_INFO("==================================================\n");
  4359. previousData[gun_index].ChargingProfileCurrent = ShmCharger->gun_info[gun_index].ChargingProfileCurrent;
  4360. previousData[gun_index].ChargingProfilePower = ShmCharger->gun_info[gun_index].ChargingProfilePower;
  4361. }
  4362. break;
  4363. }
  4364. }
  4365. void checkStopReason(uint8_t gun_index)
  4366. {
  4367. sleep(2);
  4368. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4369. {
  4370. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00,
  4371. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4372. if (!ocpp_get_starttransaction_result(gun_index))
  4373. {
  4374. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  4375. }
  4376. else if (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  4377. {
  4378. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  4379. }
  4380. else if ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A)
  4381. || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  4382. {
  4383. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4384. memcpy((char*) ShmOCPP16Data->StopTransaction[gun_index].IdTag,
  4385. (char*) ShmOCPP16Data->StartTransaction[gun_index].IdTag,
  4386. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4387. }
  4388. else if (ShmOCPP16Data->MsMsg.bits.ResetReq)
  4389. {
  4390. if (strcmp((char*) ShmOCPP16Data->Reset.Type, "Hard") == 0)
  4391. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  4392. else
  4393. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  4394. }
  4395. else if (ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  4396. {
  4397. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  4398. if (!isMatchStartUser())
  4399. {
  4400. memcpy((char*) ShmOCPP16Data->StopTransaction[gun_index].IdTag,
  4401. (char*) ShmSysConfigAndInfo->SysConfig.UserId,
  4402. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4403. }
  4404. else
  4405. {
  4406. memcpy((char*) ShmOCPP16Data->StopTransaction[gun_index].IdTag,
  4407. (char*) getTargetChargingInfoData(gun_index)->StartUserId,
  4408. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4409. }
  4410. DEBUG_INFO("Gun-[%d] : IdTag [ %s ].\n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  4411. }
  4412. else if (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  4413. {
  4414. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  4415. }
  4416. else if (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].isUnlockerConnetor == ON)
  4417. {
  4418. sprintf((char*) ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].StopReason, "UnlockCommand");
  4419. }
  4420. else
  4421. {
  4422. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  4423. }
  4424. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4425. getTargetChargingInfoData(gun_index)->PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption / 10000.0);
  4426. presentChargedEnergyUpdate(gun_index);
  4427. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n", gun_index,getTargetChargingInfoData(gun_index)->PresentChargedEnergy);
  4428. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  4429. }
  4430. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4431. {
  4432. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00,
  4433. ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  4434. if (!ocpp_get_starttransaction_result(gun_index))
  4435. {
  4436. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  4437. }
  4438. else if (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  4439. {
  4440. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  4441. }
  4442. else if ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A)
  4443. || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  4444. {
  4445. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  4446. }
  4447. else if (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  4448. {
  4449. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  4450. }
  4451. else if (ShmOCPP20Data->MsMsg.bits.ResetReq)
  4452. {
  4453. if (strcmp((char*) ShmOCPP20Data->Reset.type, "Immediate") == 0)
  4454. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  4455. else
  4456. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OnIdle");
  4457. }
  4458. else if (ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  4459. {
  4460. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  4461. if (!isMatchStartUser())
  4462. {
  4463. memcpy((char*) ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken,
  4464. (char*) ShmSysConfigAndInfo->SysConfig.UserId,
  4465. ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  4466. }
  4467. else
  4468. {
  4469. memcpy((char*) ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken,
  4470. (char*) getTargetChargingInfoData(gun_index)->StartUserId,
  4471. ARRAY_SIZE(getTargetChargingInfoData(gun_index)->StartUserId));
  4472. }
  4473. DEBUG_INFO("Gun-[%d] : idToken [ %s ].\n", gun_index,
  4474. ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  4475. }
  4476. else if ((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L1_OVER_CURRENT)
  4477. || (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L2_OVER_CURRENT)
  4478. || (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L3_OVER_CURRENT))
  4479. {
  4480. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  4481. }
  4482. else if (ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  4483. {
  4484. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  4485. }
  4486. else if (ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].isUnlockerConnetor == ON)
  4487. {
  4488. sprintf( (char*) ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].transactionInfo.stoppedReason,
  4489. "UnlockCommand");
  4490. }
  4491. else
  4492. {
  4493. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  4494. }
  4495. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n", gun_index,
  4496. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4497. getTargetChargingInfoData(gun_index)->PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/ 10000.0);
  4498. presentChargedEnergyUpdate(gun_index);
  4499. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n", gun_index,
  4500. getTargetChargingInfoData(gun_index)->PresentChargedEnergy);
  4501. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  4502. }
  4503. // Maintain server
  4504. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != 0)
  4505. {
  4506. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason,
  4507. ShmOCPP16Data->StopTransaction[gun_index].StopReason,
  4508. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  4509. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].IdTag, ShmOCPP16Data->StopTransaction[gun_index].IdTag,
  4510. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4511. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = ON;
  4512. }
  4513. }
  4514. void checkRemoteUpgradeStatus()
  4515. {
  4516. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4517. {
  4518. if (strcmp((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed") == 0)
  4519. {
  4520. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4521. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4522. ShmCharger->isUpdateSuccess = NO;
  4523. }
  4524. else if (strcmp((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == 0)
  4525. {
  4526. DEBUG_INFO("Firmware remote upgrading...\n");
  4527. sprintf((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4528. sleep(1);
  4529. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4530. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4531. int result = upgrade_check();
  4532. DEBUG_INFO("Remote update Result: %s... \n", ((result == PASS)?"Pass": "Fail"));
  4533. if (result == PASS)
  4534. {
  4535. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4536. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4537. ShmCharger->isUpdateSuccess = YES;
  4538. DEBUG_INFO("Remote update success...\n");
  4539. }
  4540. else
  4541. {
  4542. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4543. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4544. ShmCharger->isUpdateSuccess = NO;
  4545. DEBUG_INFO("Remote update unsuccess...\n");
  4546. }
  4547. }
  4548. else
  4549. {
  4550. }
  4551. }
  4552. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4553. {
  4554. if (strcmp((char*) ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed") == 0)
  4555. {
  4556. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4557. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4558. ShmCharger->isUpdateSuccess = NO;
  4559. }
  4560. else if (strcmp((char*) ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded") == 0)
  4561. {
  4562. DEBUG_INFO("Firmware remote upgrading...\n");
  4563. sprintf((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4564. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4565. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4566. int result = upgrade_check();
  4567. DEBUG_INFO("Remote update Result: %s... \n", ((result == PASS)?"Pass": "Fail"));
  4568. if (result == PASS)
  4569. {
  4570. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4571. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4572. ShmCharger->isUpdateSuccess = YES;
  4573. DEBUG_INFO("Remote update success...\n");
  4574. }
  4575. else
  4576. {
  4577. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4578. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4579. ShmCharger->isUpdateSuccess = NO;
  4580. DEBUG_INFO("Remote update unsuccess...\n");
  4581. }
  4582. }
  4583. else
  4584. {
  4585. }
  4586. }
  4587. }
  4588. void checkHandshakeCountdown(uint8_t gun_index)
  4589. {
  4590. switch (getTargetChargingInfoData(gun_index)->SystemStatus)
  4591. {
  4592. case SYS_MODE_IDLE:
  4593. if (ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
  4594. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4595. else
  4596. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout())
  4597. - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4598. if (!ShmCharger->isAuthrizing)
  4599. {
  4600. if (ocpp_get_isRemoteStartWait())
  4601. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout())
  4602. - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4603. else
  4604. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4605. }
  4606. break;
  4607. case SYS_MODE_PREPARING:
  4608. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout())
  4609. - getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]));
  4610. break;
  4611. case SYS_MODE_RESERVATION:
  4612. if (ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
  4613. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4614. else
  4615. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout())
  4616. - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4617. break;
  4618. }
  4619. }
  4620. void checkRfidAuthrize()
  4621. {
  4622. static uint8_t isCheckdResult = FALSE;
  4623. uint8_t isStartByWrongReservation = NO;
  4624. if (!ShmCharger->isAuthrizing)
  4625. {
  4626. // Read RFID
  4627. if (GetCardSerialNumber() != FAIL)
  4628. {
  4629. uint8_t isSnStart = FALSE;
  4630. uint8_t bufferRFID[ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId)];
  4631. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4632. {
  4633. // Big endian
  4634. switch (rfid.snType)
  4635. {
  4636. case RFID_SN_TYPE_6BYTE:
  4637. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0],
  4638. rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4],
  4639. rfid.currentCard[5]);
  4640. break;
  4641. case RFID_SN_TYPE_7BYTE:
  4642. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0],
  4643. rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4],
  4644. rfid.currentCard[5], rfid.currentCard[6]);
  4645. break;
  4646. case RFID_SN_TYPE_8BYTE:
  4647. sprintf((char *) bufferRFID, "%02X%02X%02X%02X%02X%02X%02X%02X",
  4648. rfid.currentCard[0], rfid.currentCard[1],
  4649. rfid.currentCard[2], rfid.currentCard[3],
  4650. rfid.currentCard[4], rfid.currentCard[5],
  4651. rfid.currentCard[6], rfid.currentCard[7]);
  4652. break;
  4653. case RFID_SN_TYPE_10BYTE:
  4654. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0],
  4655. rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4],
  4656. rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8],
  4657. rfid.currentCard[9]);
  4658. break;
  4659. case RFID_SN_TYPE_4BYTE:
  4660. default:
  4661. sprintf((char*) bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1],
  4662. rfid.currentCard[2], rfid.currentCard[3]);
  4663. break;
  4664. }
  4665. }
  4666. else
  4667. {
  4668. // Little endian
  4669. switch (rfid.snType)
  4670. {
  4671. case RFID_SN_TYPE_6BYTE:
  4672. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5],
  4673. rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1],
  4674. rfid.currentCard[0]);
  4675. break;
  4676. case RFID_SN_TYPE_7BYTE:
  4677. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6],
  4678. rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2],
  4679. rfid.currentCard[1], rfid.currentCard[0]);
  4680. break;
  4681. case RFID_SN_TYPE_8BYTE:
  4682. sprintf((char *) bufferRFID,
  4683. "%02X%02X%02X%02X%02X%02X%02X%02X",
  4684. rfid.currentCard[7], rfid.currentCard[6],
  4685. rfid.currentCard[5], rfid.currentCard[4],
  4686. rfid.currentCard[3], rfid.currentCard[2],
  4687. rfid.currentCard[1], rfid.currentCard[0]);
  4688. break;
  4689. case RFID_SN_TYPE_10BYTE:
  4690. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9],
  4691. rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5],
  4692. rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1],
  4693. rfid.currentCard[0]);
  4694. break;
  4695. case RFID_SN_TYPE_4BYTE:
  4696. default:
  4697. sprintf((char*) bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2],
  4698. rfid.currentCard[1], rfid.currentCard[0]);
  4699. break;
  4700. }
  4701. }
  4702. DEBUG_INFO("Authorize request User Id : %s\n", bufferRFID);
  4703. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4704. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  4705. // Check SN already start
  4706. if (isMatchStartUser() || isMatchPresentUser()) //lwtest
  4707. {
  4708. //Already entry charging
  4709. if (strcmp((char*) getTargetChargingInfoData(ShmCharger->gun_selectd)->StartUserId, "") != 0)
  4710. DEBUG_INFO("%s running on connector-%02d.\n",
  4711. getTargetChargingInfoData(ShmCharger->gun_selectd)->StartUserId, ShmCharger->gun_selectd);
  4712. //Already entry preparing
  4713. else if (strcmp((char*) ShmCharger->gun_info[ShmCharger->gun_selectd].AuthAcceptUserId, "") != 0)
  4714. DEBUG_INFO("%s ready running on connector-%02d.\n",
  4715. ShmCharger->gun_info[ShmCharger->gun_selectd].AuthAcceptUserId, ShmCharger->gun_selectd);
  4716. isSnStart = TRUE;
  4717. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  4718. ocpp_set_auth_conf(ON);
  4719. setLedMotion(ShmCharger->gun_selectd, LED_ACTION_RFID_PASS);
  4720. setSpeaker(ON, SPEAKER_SHORT);
  4721. sleep(3);
  4722. //break;
  4723. }
  4724. else if (isNeedToAuth() == FALSE)
  4725. {
  4726. //lwtest
  4727. isSnStart = TRUE;
  4728. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = OFF;
  4729. ocpp_set_auth_conf(ON);
  4730. }
  4731. else
  4732. {
  4733. //lwtest .. for GL testing control.
  4734. for (uint8_t index = 0; index < modelnameInfo.GetGunCount; index++)
  4735. {
  4736. if (ShmCharger->gun_info[index].isCmdToPrepareMode == ON)
  4737. {
  4738. ShmCharger->gun_info[index].rfidReq = ON;
  4739. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00,
  4740. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4741. return;
  4742. }
  4743. }
  4744. }
  4745. // Request authorize if isSnStart is false
  4746. if (!isSnStart)
  4747. {
  4748. DEBUG_INFO("RFID start. [ %s ]\n", bufferRFID);
  4749. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, bufferRFID,
  4750. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4751. refreshStartTimer(&startTime[ShmCharger->gun_selectd][TMR_IDX_AUTH]); //lwtest
  4752. ocpp_set_auth_conf(OFF);
  4753. ocpp_set_auth_req(ON, "ISO14443");
  4754. setLedMotion(0, LED_ACTION_AUTHED);
  4755. ShmCharger->isAuthrizing = TRUE;
  4756. ShmCharger->isGetAuthResult = FALSE;
  4757. isCheckdResult = FALSE;
  4758. }
  4759. }
  4760. }
  4761. else //isAuthrizing = true
  4762. {
  4763. // Wait authorize result
  4764. if (!ocpp_get_auth_conf()
  4765. && (getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH))
  4766. {
  4767. // Authorization timeout process.
  4768. DEBUG_WARN("Authorize timeout [ %s ]!!!\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4769. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4770. {
  4771. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4772. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  4773. }
  4774. //lwtest
  4775. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4776. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  4777. sleep(3);
  4778. ShmCharger->isAuthrizing = FALSE;
  4779. }
  4780. else
  4781. {
  4782. if (ocpp_get_auth_conf()
  4783. || (!ocpp_get_connection_status()
  4784. && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)
  4785. || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE)))
  4786. || (!ocpp_get_connection_status()
  4787. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)
  4788. && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > 2)))
  4789. {
  4790. if (ocpp_get_auth_result(NO)
  4791. || (!ocpp_get_connection_status()
  4792. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE))
  4793. || (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS)
  4794. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  4795. {
  4796. if (!isCheckdResult)
  4797. {
  4798. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4799. ShmCharger->isGetAuthResult = TRUE;
  4800. isCheckdResult = TRUE;
  4801. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") == 0)
  4802. {
  4803. ocpp_set_auth_conf(ON);
  4804. }
  4805. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4806. {
  4807. if ((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_RESERVATION)
  4808. && (strcmp((char*) ShmSysConfigAndInfo->SysConfig.UserId,
  4809. (char*) ShmOCPP16Data->ReserveNow[gun_index].IdTag) != 0))
  4810. {
  4811. sleep(1);
  4812. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4813. isStartByWrongReservation = YES;
  4814. DEBUG_INFO("Not match: UserId = [%s] / Reserve IdTag = [%s] \n",
  4815. ShmSysConfigAndInfo->SysConfig.UserId,
  4816. ShmOCPP16Data->ReserveNow[ShmCharger->gun_selectd].IdTag);
  4817. DEBUG_INFO("Authorize fail.\n");
  4818. //lwtest
  4819. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00,
  4820. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4821. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  4822. }
  4823. else
  4824. {
  4825. //lwtest
  4826. if (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_IDLE)
  4827. {
  4828. DEBUG_INFO("Authorize pass (gun %d) [ %s ].\n", gun_index,
  4829. ShmSysConfigAndInfo->SysConfig.UserId);
  4830. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  4831. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_B;
  4832. ShmCharger->gun_selectd = gun_index;
  4833. memcpy(ShmCharger->gun_info[gun_index].AuthAcceptUserId,
  4834. ShmSysConfigAndInfo->SysConfig.UserId,
  4835. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4836. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00,
  4837. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4838. break;
  4839. }
  4840. setSpeaker(ON, SPEAKER_SHORT);
  4841. }
  4842. }
  4843. }
  4844. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4845. checkHandshakeCountdown(gun_index);
  4846. if ((getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < (ocpp_get_connection_timeout()))
  4847. && (isStartByWrongReservation != YES))
  4848. {
  4849. if (GetCardSerialNumber() != FAIL)
  4850. {
  4851. if (isMatchPresentUser())
  4852. {
  4853. DEBUG_INFO("Cancel present user.\n");
  4854. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4855. {
  4856. setLedMotion(gun_index, LED_ACTION_IDLE);
  4857. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4858. }
  4859. setSpeaker(ON, SPEAKER_SHORT);
  4860. ShmCharger->isAuthrizing = FALSE;
  4861. }
  4862. }
  4863. else
  4864. {
  4865. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4866. {
  4867. if (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < 3)
  4868. setLedMotion(gun_index, LED_ACTION_RFID_PASS);
  4869. else
  4870. setLedMotion(gun_index, LED_ACTION_AUTHED);
  4871. if ((((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_IDLE)
  4872. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_RESERVATION))
  4873. && (((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_B)
  4874. || (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C))
  4875. || (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)))
  4876. || ((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_PREPARING)
  4877. && (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_B)
  4878. && (ShmCharger->gun_selectd == gun_index))
  4879. || (((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING)
  4880. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_TERMINATING))
  4881. && (ShmCharger->gun_selectd == gun_index)))
  4882. {
  4883. DEBUG_INFO("Connector-%02d action detect.\n", gun_index);
  4884. ShmCharger->isAuthrizing = FALSE;
  4885. ShmCharger->gun_info[gun_index].rfidReq = ON;
  4886. break;
  4887. }
  4888. }
  4889. }
  4890. }
  4891. else
  4892. {
  4893. if (isStartByWrongReservation != YES)
  4894. DEBUG_INFO("Connector action detect timeout. \n");
  4895. else
  4896. DEBUG_INFO("User Id and reservation IdTag is not match. \n");
  4897. ShmCharger->isAuthrizing = FALSE;
  4898. ShmCharger->isGetAuthResult = TRUE;
  4899. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4900. {
  4901. if (isStartByWrongReservation != YES)
  4902. {
  4903. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  4904. sleep(6);
  4905. }
  4906. else
  4907. {
  4908. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  4909. sleep(3);
  4910. }
  4911. setLedMotion(gun_index, LED_ACTION_IDLE);
  4912. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4913. }
  4914. }
  4915. }
  4916. else
  4917. {
  4918. DEBUG_INFO("Authorize fail.\n");
  4919. ShmCharger->isAuthrizing = FALSE;
  4920. ShmCharger->isGetAuthResult = TRUE;
  4921. //lwtest
  4922. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00,
  4923. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4924. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4925. {
  4926. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4927. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  4928. }
  4929. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  4930. sleep(3);
  4931. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4932. {
  4933. if ((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_PREPARING))
  4934. setLedMotion(gun_index, LED_ACTION_AUTHED);
  4935. else
  4936. setLedMotion(gun_index, LED_ACTION_IDLE);
  4937. }
  4938. }
  4939. }
  4940. }
  4941. }
  4942. }
  4943. //======================================================
  4944. // Main process
  4945. //======================================================
  4946. int main(void)
  4947. {
  4948. //==================================================
  4949. // Create all share memory
  4950. //==================================================
  4951. if (CreatShareMemory() == 0)
  4952. {
  4953. DEBUG_ERROR("CreatShareMemory NG\n");
  4954. if (ShmStatusCodeData != NULL)
  4955. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = TRUE;
  4956. sleep(5);
  4957. system("reboot -f");
  4958. sleep(5);
  4959. system("reboot -f");
  4960. }
  4961. else
  4962. {
  4963. modelnameInfo.GetGunCount = 1;
  4964. DEBUG_INFO("CreatShareMemory OK\n");
  4965. }
  4966. for (int gun_index = 0; gun_index < MAX_GUN_NUM; gun_index++)
  4967. {
  4968. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[gun_index].SystemStatus = 0x00;
  4969. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[gun_index].PreviousSystemStatus = 0xff;
  4970. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[gun_index].Index = 0xff;
  4971. ShmSysConfigAndInfo->SysInfo.CcsChargingData[gun_index].SystemStatus = 0x00;
  4972. ShmSysConfigAndInfo->SysInfo.CcsChargingData[gun_index].PreviousSystemStatus = 0xff;
  4973. ShmSysConfigAndInfo->SysInfo.CcsChargingData[gun_index].Index = 0xff;
  4974. ShmSysConfigAndInfo->SysInfo.GbChargingData[gun_index].SystemStatus = 0x00;
  4975. ShmSysConfigAndInfo->SysInfo.GbChargingData[gun_index].PreviousSystemStatus = 0xff;
  4976. ShmSysConfigAndInfo->SysInfo.GbChargingData[gun_index].Index = 0xff;
  4977. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = 0x00;
  4978. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  4979. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = 0xff;
  4980. }
  4981. //==================================================
  4982. // Main loop
  4983. //==================================================
  4984. for (;;)
  4985. {
  4986. //==============================================
  4987. // Synchronize share memory from OCPP struct
  4988. //==============================================
  4989. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  4990. //==============================================
  4991. // Period check for 10 seconds
  4992. //==============================================
  4993. if ((getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) > TIMEOUT_SPEC_TASK_CHECK)
  4994. || (getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
  4995. {
  4996. //==============================================
  4997. // Check task processing
  4998. //==============================================
  4999. if (getTargetChargingInfoData(0)->SystemStatus != SYS_MODE_BOOTING
  5000. && getTargetChargingInfoData(1)->SystemStatus != SYS_MODE_BOOTING) //lwtest
  5001. checkTask();
  5002. //==============================================
  5003. // Check connection timeout specification
  5004. //==============================================
  5005. checkConnectionTimeout();
  5006. refreshStartTimer(&startTime[0][TMR_IDX_CHECK_TASK]);
  5007. }
  5008. //==============================================
  5009. // Something need run in Idle mode
  5010. //==============================================
  5011. if (((getTargetChargingInfoData(0)->SystemStatus == SYS_MODE_IDLE)
  5012. || (getTargetChargingInfoData(0)->SystemStatus == SYS_MODE_ALARM)
  5013. || (getTargetChargingInfoData(0)->SystemStatus == SYS_MODE_DEBUG))
  5014. && (modelnameInfo.GetGunCount > 1 ? ((getTargetChargingInfoData(1)->SystemStatus == SYS_MODE_IDLE)
  5015. || (getTargetChargingInfoData(1)->SystemStatus == SYS_MODE_ALARM)
  5016. || (getTargetChargingInfoData(1)->SystemStatus == SYS_MODE_DEBUG)) : TRUE))
  5017. {
  5018. // Check restore factory setting request
  5019. if (ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  5020. {
  5021. sleep(5);
  5022. system("cd /root;./Module_FactoryConfig -m");
  5023. system("rm -f /Storage/OCPP/OCPPConfiguration");
  5024. system("sync");
  5025. sleep(5);
  5026. system("reboot -f");
  5027. sleep(5);
  5028. system("reboot -f");
  5029. }
  5030. // Check upgrade firmware request
  5031. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate || ocpp_get_update_firmware_req())
  5032. {
  5033. // If available memory too small, free memory cache first
  5034. if (getAvailableMemory() < (200 * 1024 * 1024))
  5035. {
  5036. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n",
  5037. getAvailableMemory() / (1024 * 1024.0));
  5038. system("echo 3 > /proc/sys/vm/drop_caches");
  5039. }
  5040. ShmCharger->isUpdateSuccess = NO;
  5041. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  5042. {
  5043. setChargerMode(gun_index, SYS_MODE_UPDATE);
  5044. }
  5045. }
  5046. }
  5047. //==============================================
  5048. // Check remote reset request
  5049. //==============================================
  5050. checkReset();
  5051. //==============================================
  5052. // Check RFID authorization
  5053. //==============================================
  5054. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  5055. {
  5056. // The user is not allowed to use RFID when the system change to under those modes.
  5057. if ((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_ALARM)
  5058. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_FAULT)
  5059. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_DEBUG)
  5060. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_UPDATE)
  5061. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_BOOTING)
  5062. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_COMPLETE)
  5063. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_MAINTAIN)
  5064. || (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE))
  5065. {
  5066. }
  5067. else
  5068. {
  5069. checkRfidAuthrize();
  5070. }
  5071. }
  5072. //==============================================
  5073. // Connector loop
  5074. //==============================================
  5075. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  5076. {
  5077. /*
  5078. * TODO:
  5079. * 1. Simulate voltage, current, power consumption
  5080. */
  5081. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] == 'A')
  5082. {
  5083. static uint64_t tmpPowerConsumption[3];
  5084. static long long tsLast[3], tsNow[3];
  5085. // Voltage
  5086. ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12 =
  5087. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5088. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5089. {
  5090. ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23 =
  5091. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5092. ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31 =
  5093. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5094. }
  5095. getTargetChargingInfoData(gun_index)->PresentChargingVoltage = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  5096. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  5097. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5098. {
  5099. getTargetChargingInfoData(gun_index)->PresentChargingVoltageL2 = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  5100. getTargetChargingInfoData(gun_index)->PresentChargingVoltageL3 = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  5101. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  5102. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  5103. }
  5104. else
  5105. {
  5106. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  5107. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  5108. }
  5109. //lwtest
  5110. // if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5111. // printf("=== AC 3 phase R=%.2f v | S=%.2f v | T%.2f v\n", ShmSysConfigAndInfo->SysInfo.InputVoltageR,
  5112. // ShmSysConfigAndInfo->SysInfo.InputVoltageS, ShmSysConfigAndInfo->SysInfo.InputVoltageT);
  5113. // else
  5114. // printf("=== AC 1 phase R=%.2f v\n", ShmSysConfigAndInfo->SysInfo.InputVoltageR);
  5115. //------------------------------------------------------------------------------------
  5116. // Current
  5117. uint16_t _targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent/ShmSysConfigAndInfo->SysConfig.AcPhaseCount;
  5118. getTargetChargingInfoData(gun_index)->PresentChargingCurrent =
  5119. (float) (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5120. ? (((rand() % 10) + ((_targetCurrent * 10) - SIM_DEVIATION)) / 10.0) : 0);
  5121. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5122. {
  5123. getTargetChargingInfoData(gun_index)->PresentChargingCurrentL2 =
  5124. (float) (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5125. ? (((rand() % 10) + ((_targetCurrent * 10) - SIM_DEVIATION)) / 10.0) : 0);
  5126. getTargetChargingInfoData(gun_index)->PresentChargingCurrentL3 =
  5127. (float) (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5128. ? (((rand() % 10) + ((_targetCurrent * 10) - SIM_DEVIATION)) / 10.0) : 0);
  5129. }
  5130. //lwtest
  5131. if (getTargetChargingInfoData(gun_index)->PresentChargingCurrent < 0.0)
  5132. {
  5133. //lwtest for debug
  5134. DEBUG_INFO("LW debug message : PresentChargingCurrent < 0, value = %.2f A\n",
  5135. getTargetChargingInfoData(gun_index)->PresentChargingCurrent);
  5136. getTargetChargingInfoData(gun_index)->PresentChargingCurrent = 0;
  5137. }
  5138. //lwtest
  5139. // if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5140. // printf("=== AC 3 phase Gun %d Current I1=%.2f A| I2=%.2f A| I3=%.2f A\n", gun_index,
  5141. // getTargetChargingInfoData(gun_index)->PresentChargingCurrent,
  5142. // getTargetChargingInfoData(gun_index)->PresentChargingCurrentL2,
  5143. // getTargetChargingInfoData(gun_index)->PresentChargingCurrentL3);
  5144. // else
  5145. // printf("=== AC 1 phase Gun %d Current=%.2f A\n", gun_index,
  5146. // getTargetChargingInfoData(gun_index)->PresentChargingCurrent);
  5147. //------------------------------------------------------------------------------------
  5148. // Energy
  5149. tsNow[gun_index] = current_timestamp();
  5150. tmpPowerConsumption[gun_index] += (uint64_t) ((getTargetChargingInfoData(gun_index)->PresentChargingVoltage
  5151. * getTargetChargingInfoData(gun_index)->PresentChargingCurrent)
  5152. * ((tsNow[gun_index] - tsLast[gun_index]) / 360000.0));
  5153. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5154. {
  5155. tmpPowerConsumption[gun_index] += (uint64_t) ((getTargetChargingInfoData(gun_index)->PresentChargingVoltageL2
  5156. * getTargetChargingInfoData(gun_index)->PresentChargingCurrentL2)
  5157. * ((tsNow[gun_index] - tsLast[gun_index]) / 360000.0));
  5158. tmpPowerConsumption[gun_index] += (uint64_t) ((getTargetChargingInfoData(gun_index)->PresentChargingVoltageL3
  5159. * getTargetChargingInfoData(gun_index)->PresentChargingCurrentL3)
  5160. * ((tsNow[gun_index] - tsLast[gun_index]) / 360000.0));
  5161. }
  5162. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption = tmpPowerConsumption[gun_index];
  5163. tsLast[gun_index] = tsNow[gun_index];
  5164. // Connector temperature
  5165. getTargetChargingInfoData(gun_index)->ConnectorTemp =
  5166. (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5167. ? ((rand() % 3) + ((50) - 3)) : ((rand() % 3) + ((25) - 3)));
  5168. }
  5169. else
  5170. {
  5171. //DC type
  5172. static uint64_t tmpPowerConsumption[3];
  5173. static long long tsLast[3], tsNow[3];
  5174. // Input Voltage
  5175. ShmSysConfigAndInfo->SysInfo.InputVoltageR =
  5176. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5177. ShmSysConfigAndInfo->SysInfo.InputVoltageS =
  5178. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5179. ShmSysConfigAndInfo->SysInfo.InputVoltageT =
  5180. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5181. //lwtest
  5182. // printf("=== DC input Voltage R=%.2f v| S=%.2f v| T=%.2f v\n",
  5183. // ShmSysConfigAndInfo->SysInfo.InputVoltageR, ShmSysConfigAndInfo->SysInfo.InputVoltageS,
  5184. // ShmSysConfigAndInfo->SysInfo.InputVoltageT);
  5185. //------------------------------------------------------------------------------------
  5186. // Output Voltage
  5187. getTargetChargingInfoData(gun_index)->PresentChargingVoltage =
  5188. (float) (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5189. ? (((rand() % 10) + ((DC_SIMULATE_OUTPUT_VOL * 10) - 100)) / 10.0) : 0);
  5190. //lwtest
  5191. // printf("=== DC Gun %d Present Charging Voltage %.2f v\n", gun_index,
  5192. // getTargetChargingInfoData(gun_index)->PresentChargingVoltage);
  5193. //------------------------------------------------------------------------------------
  5194. // Current
  5195. getTargetChargingInfoData(gun_index)->PresentChargingCurrent =
  5196. (float) (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5197. ? (((rand() % 10) + ((ShmCharger->gun_info[gun_index].targetCurrent * 10) - SIM_DEVIATION)) / 10.0) : 0);
  5198. //lwtest
  5199. if (getTargetChargingInfoData(gun_index)->PresentChargingCurrent < 0.0)
  5200. {
  5201. //lwtest for debug
  5202. DEBUG_INFO("LW debug message : PresentChargingCurrent < 0, value = %.2f A\n",
  5203. getTargetChargingInfoData(gun_index)->PresentChargingCurrent);
  5204. getTargetChargingInfoData(gun_index)->PresentChargingCurrent = 0;
  5205. }
  5206. // //lwtest
  5207. // printf("=== DC Gun %d Present Charging Current %.2f A\n", gun_index,
  5208. // getTargetChargingInfoData(gun_index)->PresentChargingCurrent);
  5209. //------------------------------------------------------------------------------------
  5210. // Energy
  5211. tsNow[gun_index] = current_timestamp();
  5212. tmpPowerConsumption[gun_index] += (uint64_t) ((getTargetChargingInfoData(gun_index)->PresentChargingVoltage
  5213. * getTargetChargingInfoData(gun_index)->PresentChargingCurrent)
  5214. * ((tsNow[gun_index] - tsLast[gun_index]) / 360000.0));
  5215. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption = tmpPowerConsumption[gun_index];
  5216. tsLast[gun_index] = tsNow[gun_index];
  5217. //lwtest
  5218. // printf("=== DC Gun %d power_consumption %.2lld v\n", gun_index, ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption);
  5219. //------------------------------------------------------------------------------------
  5220. //Power Offered , Current Offered
  5221. //lwtest
  5222. if (isTwoGunInChargingMode())
  5223. {
  5224. getTargetChargingInfoData(gun_index)->CurrentOffered = 120 * 10 * 0.5;
  5225. getTargetChargingInfoData(gun_index)->PowerOffered = (modelnameInfo.ratedPower / 100.0) * 0.5;
  5226. }
  5227. else
  5228. {
  5229. getTargetChargingInfoData(gun_index)->CurrentOffered = 120 * 10;
  5230. getTargetChargingInfoData(gun_index)->PowerOffered = (modelnameInfo.ratedPower / 100.0);
  5231. }
  5232. //------------------------------------------------------------------------------------
  5233. // SOC
  5234. if (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_IDLE)
  5235. getTargetChargingInfoData(gun_index)->EvBatterySoc = 0;
  5236. else
  5237. {
  5238. if (getDiffSecNow(startTime[gun_index][TMR_IDX_SIMULATION]) > TIMEOUT_SPEC_SIMULATION)
  5239. {
  5240. if (getTargetChargingInfoData(gun_index)->EvBatterySoc < 100)
  5241. getTargetChargingInfoData(gun_index)->EvBatterySoc += 1;
  5242. refreshStartTimer(&startTime[gun_index][TMR_IDX_SIMULATION]);
  5243. }
  5244. }
  5245. //------------------------------------------------------------------------------------
  5246. // Connector temperature
  5247. getTargetChargingInfoData(gun_index)->ConnectorTemp =
  5248. (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5249. ? ((rand() % 3) + ((80) - 3)) : ((rand() % 3) + ((25) - 3)));
  5250. //------------------------------------------------------------------------------------
  5251. // System fan RPM
  5252. getTargetChargingInfoData(gun_index)->ConnectorTemp =
  5253. (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5254. ? ((rand() % 3) + ((50) - 3)) : ((rand() % 3) + ((25) - 3))) + 60;
  5255. //------------------------------------------------------------------------------------
  5256. // Remain charging time
  5257. getTargetChargingInfoData(gun_index)->RemainChargingDuration =
  5258. (100 - getTargetChargingInfoData(gun_index)->EvBatterySoc) * 10;
  5259. //------------------------------------------------------------------------------------
  5260. // EV battery max voltage
  5261. getTargetChargingInfoData(gun_index)->EvBatteryMaxVoltage = DC_SIMULATE_OUTPUT_VOL;
  5262. // EV battery target voltage
  5263. getTargetChargingInfoData(gun_index)->EvBatterytargetVoltage = DC_SIMULATE_OUTPUT_VOL - SIM_DEVIATION;
  5264. }
  5265. /*
  5266. * TODO:
  5267. * 1. Synchronize current rating value from MCU
  5268. */
  5269. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] == 'A')
  5270. {
  5271. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_TYPE] == 'X'
  5272. && strncmp((char*) &ShmSysConfigAndInfo->SysConfig.ModelName[RATING_POWER], "111", 3) == 0)
  5273. ShmSysConfigAndInfo->SysConfig.RatingCurrent = 48;
  5274. else if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_TYPE] == 'X'
  5275. && strncmp((char*) &ShmSysConfigAndInfo->SysConfig.ModelName[RATING_POWER], "221", 3) == 0)
  5276. ShmSysConfigAndInfo->SysConfig.RatingCurrent = 32;
  5277. else
  5278. ShmSysConfigAndInfo->SysConfig.RatingCurrent = modelnameInfo.ratedPower / AC_OUTPUT_VOL;
  5279. }
  5280. else
  5281. ShmSysConfigAndInfo->SysConfig.RatingCurrent = modelnameInfo.ratedPower / DC_SIMULATE_OUTPUT_VOL;
  5282. //lwtest
  5283. // printf("=== Rating Current %d, ratedPower = %d\n", ShmSysConfigAndInfo->SysConfig.RatingCurrent,
  5284. // modelnameInfo.ratedPower);
  5285. //==========================================
  5286. // Synchronize present charging power
  5287. //==========================================
  5288. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  5289. {
  5290. if (getRelay(gun_index) == ON)
  5291. {
  5292. getTargetChargingInfoData(gun_index)->PresentChargingPower =
  5293. (getTargetChargingInfoData(gun_index)->PresentChargingVoltage
  5294. * getTargetChargingInfoData(gun_index)->PresentChargingCurrent) / 1000;
  5295. //min power
  5296. //lwtest
  5297. if (getTargetChargingInfoData(gun_index)->PresentChargingPower <= (ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.01))
  5298. getTargetChargingInfoData(gun_index)->PresentChargingPower = ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.01;
  5299. // printf("===Gun %d 1 Phase relay ON Rating Pow %.2f w\n", gun_index,
  5300. // getTargetChargingInfoData(gun_index)->PresentChargingPower);
  5301. }
  5302. else
  5303. getTargetChargingInfoData(gun_index)->PresentChargingPower = 0;
  5304. }
  5305. else
  5306. {
  5307. if (getRelay(gun_index) == ON)
  5308. {
  5309. getTargetChargingInfoData(gun_index)->PresentChargingPower = (((getTargetChargingInfoData(gun_index)->PresentChargingVoltage
  5310. * getTargetChargingInfoData(gun_index)->PresentChargingCurrent) / 1000)
  5311. + ((getTargetChargingInfoData(gun_index)->PresentChargingVoltageL2
  5312. * getTargetChargingInfoData(gun_index)->PresentChargingCurrentL2) / 1000)
  5313. + ((getTargetChargingInfoData(gun_index)->PresentChargingVoltageL3
  5314. * getTargetChargingInfoData(gun_index)->PresentChargingCurrentL3) / 1000));
  5315. //lwtest
  5316. //min power
  5317. if (getTargetChargingInfoData(gun_index)->PresentChargingPower <= (ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.01))
  5318. getTargetChargingInfoData(gun_index)->PresentChargingPower = ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.01;
  5319. //lwtest
  5320. // printf("===Gun %d 3 Phase relay ON Rating Pow %.2f\n", gun_index,
  5321. // getTargetChargingInfoData(gun_index)->PresentChargingPower);
  5322. }
  5323. else
  5324. {
  5325. getTargetChargingInfoData(gun_index)->PresentChargingPower = 0;
  5326. }
  5327. }
  5328. //==========================================
  5329. // Check initialization "PASS" or "FAIL"
  5330. //==========================================
  5331. if (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  5332. {
  5333. // Alarm event check
  5334. if ((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode > 0)
  5335. || (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode > 0)
  5336. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
  5337. {
  5338. if (getTargetChargingInfoData(gun_index)->SystemStatus != SYS_MODE_ALARM)
  5339. {
  5340. if (getTargetChargingInfoData(gun_index)->SystemStatus != SYS_MODE_UPDATE)
  5341. {
  5342. setChargerMode(gun_index, SYS_MODE_ALARM);
  5343. }
  5344. }
  5345. }
  5346. }
  5347. //==========================================
  5348. // Reservation request check
  5349. //==========================================
  5350. checkReservation(gun_index);
  5351. //==========================================
  5352. // Change availability check
  5353. //==========================================
  5354. //lwtest
  5355. if (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_BOOTING)
  5356. {
  5357. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  5358. DEBUG_INFO("connector-0 %d operative state %d\n", gun_index,
  5359. ShmCharger->gun_info[gun_index].isOperactive);
  5360. }
  5361. checkAvailability(gun_index);
  5362. if (ShmCharger->gun_info[gun_index].isOperactive)
  5363. {
  5364. if (isMode(gun_index, SYS_MODE_MAINTAIN))
  5365. setChargerMode(gun_index, SYS_MODE_IDLE);
  5366. }
  5367. else
  5368. {
  5369. if (isMode(gun_index, SYS_MODE_IDLE))
  5370. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  5371. }
  5372. //==========================================
  5373. // Unlock Connector signal check
  5374. //==========================================
  5375. checkUnlocker(gun_index);
  5376. //==========================================
  5377. // Clean isRemoteStartWait flag
  5378. //==========================================
  5379. if (ocpp_get_isRemoteStartWait())
  5380. {
  5381. if (ShmCharger->gun_info[gun_index].isRemoteStartWait == OFF)
  5382. {
  5383. ShmCharger->gun_info[gun_index].isRemoteStartWait = ON;
  5384. DEBUG_INFO("Remote start without connector id... \n");
  5385. }
  5386. refreshStartTimer(&startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]);
  5387. }
  5388. else
  5389. {
  5390. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]) > 5))
  5391. {
  5392. if (ShmCharger->gun_info[gun_index].isRemoteStartWait == ON)
  5393. {
  5394. ShmCharger->gun_info[gun_index].isRemoteStartWait = OFF;
  5395. DEBUG_INFO("Clean isRemoteStartWait flag... \n");
  5396. }
  5397. }
  5398. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]) > 10))
  5399. refreshStartTimer(&startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]);
  5400. }
  5401. //==========================================
  5402. // Connector process
  5403. //==========================================
  5404. switch (getTargetChargingInfoData(gun_index)->SystemStatus)
  5405. {
  5406. case SYS_MODE_BOOTING:
  5407. if (isModeChange(gun_index))
  5408. {
  5409. uint8_t idxCHAdeMO = 0;
  5410. uint8_t idxCCS = 0;
  5411. uint8_t idxGB = 0;
  5412. uint8_t idxAC = 0;
  5413. DEBUG_INFO("========== SYS_MODE_BOOTING (%d) ========== \n", gun_index);
  5414. //CSU Initialization & task spawn
  5415. if ((Initialization(gun_index) != PASS) || (SpawnTask(gun_index) != PASS))
  5416. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  5417. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  5418. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  5419. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  5420. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  5421. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  5422. refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
  5423. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  5424. {
  5425. if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_Chademo)
  5426. {
  5427. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].Index = gun_index;
  5428. idxCHAdeMO ++;
  5429. }
  5430. else if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_CCS_2)
  5431. {
  5432. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].Index = gun_index;
  5433. idxCCS ++;
  5434. }
  5435. else if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_GB)
  5436. {
  5437. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGB].Index = gun_index;
  5438. idxGB ++;
  5439. }
  5440. else if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_AC)
  5441. {
  5442. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].Index = gun_index;
  5443. idxAC ++;
  5444. }
  5445. }
  5446. }
  5447. if (gun_index == 0)//lwtest
  5448. {
  5449. ShmCharger->gun_selectd = 0;
  5450. // Firmware version
  5451. get_firmware_version(gun_index);
  5452. // OCPP BootNotification info set
  5453. DEBUG_INFO("==========================================\n");
  5454. DEBUG_INFO("System ID: %s\n", ShmSysConfigAndInfo->SysConfig.SystemId);
  5455. DEBUG_INFO("==========================================\n");
  5456. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  5457. ocpp_boot_info_sync();
  5458. DEBUG_INFO("OcppServerURL: %s\n", ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  5459. DEBUG_INFO("ChargeBoxId: %s\n", ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  5460. DEBUG_INFO("ChargePointVendor: %s\n", ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  5461. DEBUG_INFO("==========================================\n");
  5462. // Set max current to rating current
  5463. if (modelnameInfo.GetGunCount > 1)
  5464. {
  5465. ShmCharger->gun_info[0].primaryMcuCp_Pwn_Duty.max_current = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  5466. ShmCharger->gun_info[1].primaryMcuCp_Pwn_Duty.max_current = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  5467. }
  5468. else
  5469. {
  5470. ShmCharger->gun_info[0].primaryMcuCp_Pwn_Duty.max_current = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  5471. }
  5472. // Default Ethernet / Wifi / 4G to 1:disconnected
  5473. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5474. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  5475. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  5476. //lwtest
  5477. switch (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE])
  5478. {
  5479. case 'E':
  5480. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5481. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  5482. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  5483. break;
  5484. case 'W':
  5485. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5486. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  5487. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  5488. break;
  5489. case 'T':
  5490. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5491. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  5492. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  5493. break;
  5494. case 'D':
  5495. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5496. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  5497. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  5498. break;
  5499. }
  5500. // If Web Server OPCC URL is empty kill Module_OcppBackend
  5501. if ((strcmp((char *) &ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") == 0))
  5502. {
  5503. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") == 0)
  5504. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  5505. system("pkill OcppBackend");
  5506. }
  5507. // The system identifies 1 phase or 3 phases depending on the model name
  5508. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = getAcPhaseCount();
  5509. }
  5510. setChargerMode(gun_index, SYS_MODE_IDLE);
  5511. //lwtest .. for GL testing control.
  5512. ShmCharger->gun_info[gun_index].isCmdToPrepareMode = OFF;
  5513. break;
  5514. case SYS_MODE_IDLE:
  5515. if (isModeChange(gun_index))
  5516. {
  5517. DEBUG_INFO("========== SYS_MODE_IDLE (%d) ========== \n", gun_index);
  5518. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_A;
  5519. setLedMotion(gun_index, LED_ACTION_IDLE);
  5520. setRelay(gun_index, OFF);
  5521. setRequest(gun_index, OFF);
  5522. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  5523. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5524. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5525. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  5526. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  5527. ShmCharger->gun_info[gun_index].isGetEvCCID = OFF;
  5528. ocpp_set_remotestart(gun_index, OFF);
  5529. ocpp_set_remotestop(gun_index, OFF);
  5530. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  5531. getTargetChargingInfoData(gun_index)->PresentChargedDuration = 0;
  5532. presentChargedEnergyClear(gun_index);
  5533. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  5534. ocpp_set_unlocker_req(gun_index, OFF);
  5535. getTargetChargingInfoData(gun_index)->ReservationId = -1;
  5536. // Response StopTransactionConf
  5537. ocpp_set_stoptransaction_conf(gun_index, OFF);
  5538. memset(getTargetChargingInfoData(gun_index)->StartUserId, 0x00,
  5539. ARRAY_SIZE(getTargetChargingInfoData(gun_index)->StartUserId));
  5540. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00,
  5541. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5542. //lwtest
  5543. memset(ShmCharger->gun_info[gun_index].AuthAcceptUserId, 0x00,
  5544. ARRAY_SIZE(ShmCharger->gun_info[gun_index].AuthAcceptUserId));
  5545. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_IDLE;
  5546. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5547. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5548. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5549. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = OFF;
  5550. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5551. //if(ShmCharger->isCcsEnable)system("pkill Module_CCS");
  5552. DB_Check_Record_Buf(localDb, gun_index);
  5553. ShmCharger->gun_info[gun_index].isEmergencyStopReport = OFF;
  5554. // Default previousData every time
  5555. previousData[gun_index].primaryMcuCp_Pwn_Duty = 0;
  5556. previousData[gun_index].targetCurrent = 0;
  5557. previousData[gun_index].current_limit = 0;
  5558. previousData[gun_index].current_limit = 0;
  5559. }
  5560. if (((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  5561. && (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_B))
  5562. || ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  5563. && (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C))
  5564. || ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  5565. && (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON))
  5566. || (ShmCharger->gun_info[gun_index].rfidReq == ON)
  5567. || (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  5568. || (ocpp_get_remotestart(gun_index) == ON)
  5569. || (getTargetChargingInfoData(gun_index)->schedule.isTriggerStart == ON)
  5570. || (ShmCharger->gun_info[gun_index].isGetEvCCID == ON)
  5571. || (ShmCharger->gun_info[gun_index].isCmdToPrepareMode == ON)) //lwtest
  5572. {
  5573. if ((ShmCharger->gun_info[gun_index].rfidReq == ON))
  5574. {
  5575. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_RFID;
  5576. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_ISO14443;
  5577. DEBUG_INFO("Start Method : RFID...\n");
  5578. }
  5579. else if (ocpp_get_remotestart(gun_index))
  5580. {
  5581. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_BACKEND;
  5582. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_Central;
  5583. ocpp_copy_userid_from_remotestart(gun_index);
  5584. setSpeaker(ON, SPEAKER_SHORT);
  5585. DEBUG_INFO("Start Method : BACKEND...\n");
  5586. }
  5587. else if (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  5588. {
  5589. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_BLE;
  5590. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_Local;
  5591. memcpy(ShmSysConfigAndInfo->SysConfig.UserId,
  5592. ShmCharger->gun_info[gun_index].bleLoginCentralId.id,
  5593. ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  5594. setSpeaker(ON, SPEAKER_SHORT);
  5595. DEBUG_INFO("Start Method : BLE...\n");
  5596. }
  5597. else if (ShmCharger->gun_info[gun_index].isGetEvCCID == ON)
  5598. {
  5599. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_EVCCID;
  5600. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_MacAddress;
  5601. setSpeaker(ON, SPEAKER_SHORT);
  5602. DEBUG_INFO("Start Method : EVCCID...\n");
  5603. }
  5604. else if (ShmCharger->gun_info[gun_index].isCmdToPrepareMode == ON)//lwtest .. for GL testing control
  5605. {
  5606. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_CMD;
  5607. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_B;
  5608. DEBUG_INFO("Start Method : COMMAND...\n");
  5609. }
  5610. else
  5611. {
  5612. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_FREE;
  5613. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_NoAuthorization;
  5614. ocpp_get_freevend_idtag(ShmSysConfigAndInfo->SysConfig.UserId);
  5615. setSpeaker(ON, SPEAKER_SHORT);
  5616. DEBUG_INFO("Start Method : FREE...\n");
  5617. }
  5618. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5619. ocpp_set_remotestart(gun_index, OFF);
  5620. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  5621. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  5622. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5623. getTargetChargingInfoData(gun_index)->schedule.isTriggerStart = OFF;
  5624. ShmCharger->gun_info[gun_index].isGetEvCCID = OFF;
  5625. // Get target current
  5626. ocpp_set_profile_req(gun_index, ON);
  5627. sleep(1);
  5628. checkChargingProfileLimit(gun_index, getTargetChargingInfoData(gun_index)->SystemStatus);
  5629. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5630. }
  5631. else
  5632. {
  5633. }
  5634. break;
  5635. case SYS_MODE_AUTHORIZING:
  5636. if (isModeChange(gun_index))
  5637. {
  5638. DEBUG_INFO("========== SYS_MODE_AUTHORIZING (%d)========== \n", gun_index);
  5639. refreshStartTimer(&startTime[gun_index][TMR_IDX_AUTH]);
  5640. if (ocpp_isAuthorizeRemoteStart()
  5641. && (getTargetChargingInfoData(gun_index)->StartMethod == START_METHOD_BACKEND))
  5642. {
  5643. DEBUG_INFO("Remote start request authorize.\n");
  5644. ocpp_set_auth_req(ON, "ISO14443");
  5645. }
  5646. else if ((getTargetChargingInfoData(gun_index)->StartMethod == START_METHOD_EVCCID))
  5647. {
  5648. DEBUG_INFO("EVCCID request authorize.\n");
  5649. ocpp_set_auth_req(ON, "MacAddress");
  5650. }
  5651. }
  5652. if (getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  5653. {
  5654. // Authorization timeout process.
  5655. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  5656. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  5657. sleep(3);
  5658. setChargerMode(gun_index, SYS_MODE_IDLE);
  5659. DEBUG_WARN("Authorize timeout !!!\n");
  5660. }
  5661. else
  5662. {
  5663. switch (getTargetChargingInfoData(gun_index)->StartMethod)
  5664. {
  5665. case START_METHOD_BACKEND:
  5666. if (ocpp_isAuthorizeRemoteStart())
  5667. {
  5668. if (ocpp_get_auth_conf())
  5669. {
  5670. if (ocpp_get_auth_result(gun_index))
  5671. {
  5672. memcpy((char*) getTargetChargingInfoData(gun_index)->StartUserId,
  5673. ShmSysConfigAndInfo->SysConfig.UserId,
  5674. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5675. DEBUG_INFO("Authorize pass.\n");
  5676. setSpeaker(ON, SPEAKER_SHORT);
  5677. setLedMotion(gun_index, LED_ACTION_RFID_PASS);
  5678. sleep(4);
  5679. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5680. }
  5681. else
  5682. {
  5683. DEBUG_INFO("Authorize fail.\n");
  5684. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  5685. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  5686. sleep(3);
  5687. setChargerMode(gun_index, SYS_MODE_IDLE);
  5688. }
  5689. ocpp_set_auth_conf(OFF);
  5690. }
  5691. }
  5692. else
  5693. {
  5694. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5695. }
  5696. break;
  5697. case START_METHOD_EVCCID:
  5698. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  5699. if (ocpp_get_auth_conf()
  5700. || (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)
  5701. || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE)))
  5702. || (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)
  5703. && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > 2)))
  5704. {
  5705. if (ocpp_get_auth_result(gun_index) || (!ocpp_get_connection_status()
  5706. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE))
  5707. || (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS)
  5708. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  5709. {
  5710. memcpy((char*) getTargetChargingInfoData(gun_index)->StartUserId,
  5711. ShmSysConfigAndInfo->SysConfig.UserId,
  5712. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5713. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  5714. DEBUG_INFO("Authorize pass [EVCCID].\n");
  5715. setSpeaker(ON, SPEAKER_SHORT);
  5716. setLedMotion(gun_index, LED_ACTION_RFID_PASS);
  5717. sleep(1);
  5718. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5719. ShmCharger->gun_info[gun_index].isEvCCIDAuthorizeFail = NO;
  5720. }
  5721. else
  5722. {
  5723. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  5724. DEBUG_INFO("Authorize fail [EVCCID].\n");
  5725. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  5726. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  5727. sleep(3);
  5728. setChargerMode(gun_index, SYS_MODE_IDLE);
  5729. ShmCharger->gun_info[gun_index].isEvCCIDAuthorizeFail = YES;
  5730. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5731. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5732. }
  5733. ocpp_set_auth_conf(OFF);
  5734. }
  5735. break;
  5736. case START_METHOD_RFID:
  5737. case START_METHOD_BLE:
  5738. case START_METHOD_FREE:
  5739. case START_METHOD_CMD:
  5740. default:
  5741. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5742. break;
  5743. }
  5744. }
  5745. break;
  5746. case SYS_MODE_PREPARING:
  5747. if (isModeChange(gun_index))
  5748. {
  5749. DEBUG_INFO("========== SYS_MODE_PREPARING (%d)========== \n", gun_index);
  5750. //lwtest .. for GL testing control
  5751. if (ShmCharger->gun_info[gun_index].isCmdToPrepareMode != ON)
  5752. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_C;
  5753. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5754. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5755. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5756. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5757. setRequest(gun_index, ON);
  5758. sleep(3);
  5759. }
  5760. // If control pilot detect Bx, skip watch dog time out.
  5761. if ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_B)
  5762. || (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C))
  5763. {
  5764. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  5765. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5766. switch (ShmCharger->gun_info[gun_index].ccsHandshakeState)
  5767. {
  5768. case HANDSHAKE_CP_STATE_E:
  5769. DEBUG_INFO("Determine max charging current to MCU.\n");
  5770. ocpp_set_profile_req(gun_index, ON);
  5771. sleep(1);
  5772. checkChargingProfileLimit(gun_index, getTargetChargingInfoData(gun_index)->SystemStatus);
  5773. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  5774. refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  5775. sleep(1);
  5776. break;
  5777. case HANDSHAKE_SET_MAX_CURRENT:
  5778. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_BS_MODE;
  5779. DEBUG_INFO("Enter BS Mode charging.\n");
  5780. //for EV READY 30 secs didn't start charging to STATE E
  5781. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5782. sleep(1);
  5783. break;
  5784. case HANDSHAKE_BS_MODE:
  5785. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5786. checkChargingProfileLimit(gun_index, getTargetChargingInfoData(gun_index)->SystemStatus);
  5787. if (ShmCharger->gun_info[gun_index].targetCurrent != 0
  5788. && ShmCharger->gun_info[gun_index].isCmdToPrepareMode == OFF)
  5789. setRelay(gun_index, ON);
  5790. //---------------LW test for GL test platform start-----------------
  5791. else if (ShmCharger->gun_info[gun_index].isCmdToPrepareMode == ON)
  5792. {
  5793. //Plugin state and wait trigger start.
  5794. if (ShmCharger->gun_info[gun_index].isCmdToPrepareMode == ON
  5795. && (ocpp_get_remotestart(gun_index) == ON || ShmCharger->gun_info[gun_index].rfidReq == ON))
  5796. {
  5797. if (ocpp_get_remotestart(gun_index))
  5798. {
  5799. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_Central;
  5800. ocpp_copy_userid_from_remotestart(gun_index);
  5801. DEBUG_INFO("LW debug Message : GL Remote Start.\n");
  5802. ///////////////
  5803. if (ocpp_isAuthorizeRemoteStart())
  5804. {
  5805. DEBUG_INFO("LW debug Message : GL Remote Start request authorize.\n");
  5806. ocpp_set_auth_req(ON, "ISO14443");
  5807. if (ocpp_get_auth_conf())
  5808. {
  5809. if (ocpp_get_auth_result(gun_index))
  5810. {
  5811. memcpy( (char*) getTargetChargingInfoData(gun_index)->StartUserId,
  5812. ShmSysConfigAndInfo->SysConfig.UserId,
  5813. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5814. DEBUG_INFO("LW debug Message : GL Authorize pass.\n");
  5815. }
  5816. else
  5817. {
  5818. DEBUG_INFO("LW debug Message : GL Authorize fail.\n");
  5819. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5820. }
  5821. ocpp_set_auth_conf(OFF);
  5822. }
  5823. }
  5824. }
  5825. setRelay(gun_index, ON);
  5826. ShmCharger->gun_info[gun_index].isCmdToPrepareMode = OFF;
  5827. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5828. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_C;
  5829. }
  5830. //---------------LW test for GL test platform end-----------------
  5831. }
  5832. if ((getRelay(gun_index) == ON) || (ShmCharger->gun_info[gun_index].targetCurrent == 0))
  5833. {
  5834. ocpp_set_unlocker_req(gun_index, OFF);
  5835. presentChargedEnergyClear(gun_index);
  5836. getDateTimeString((char*) getTargetChargingInfoData(gun_index)->StartDateTime);
  5837. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  5838. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  5839. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  5840. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  5841. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  5842. getTargetChargingInfoData(gun_index)->PowerConsumption =
  5843. (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption / 10000.0);
  5844. else
  5845. {
  5846. getTargetChargingInfoData(gun_index)->PowerConsumption =
  5847. (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption / 10000.0);
  5848. }
  5849. //lwtest
  5850. if (ocpp_get_remotestart(gun_index))
  5851. {
  5852. memcpy((char*) getTargetChargingInfoData(gun_index)->StartUserId,
  5853. ShmSysConfigAndInfo->SysConfig.UserId,
  5854. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5855. }
  5856. else
  5857. {
  5858. memcpy((char*) getTargetChargingInfoData(gun_index)->StartUserId,
  5859. ShmCharger->gun_info[gun_index].AuthAcceptUserId,
  5860. ARRAY_SIZE(ShmCharger->gun_info[gun_index].AuthAcceptUserId));
  5861. }
  5862. ocpp_copy_userid_to_starttransaction(gun_index);
  5863. ocpp_set_starttransaction_req(gun_index, ON);
  5864. refreshStartTimer(&startChargingTime[gun_index]);
  5865. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5866. }
  5867. sleep(1);
  5868. break;
  5869. default:
  5870. break;
  5871. }
  5872. }
  5873. // Unplug charging gun to Idle mode
  5874. if ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A)
  5875. && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  5876. {
  5877. DEBUG_INFO("The connector was connected to the EV before.\n");
  5878. //Cancel CCS task negotiating
  5879. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5880. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5881. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5882. }
  5883. // Use RFID card to stop handshaking
  5884. if ((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchPresentUser())
  5885. {
  5886. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  5887. //Cancel CCS task negotiating
  5888. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5889. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5890. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5891. //lwtest
  5892. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5893. }
  5894. else
  5895. {
  5896. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5897. }
  5898. //lwtest
  5899. if (getTargetChargingInfoData(gun_index)->schedule.isTriggerStop == ON)
  5900. {
  5901. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5902. }
  5903. checkHandshakeCountdown(gun_index);
  5904. if (getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  5905. {
  5906. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  5907. if (getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec + 5))
  5908. {
  5909. DEBUG_INFO("Handshaking timeout...\n");
  5910. //Cancel CCS task negotiating
  5911. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5912. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5913. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5914. }
  5915. }
  5916. break;
  5917. case SYS_MODE_CHARGING:
  5918. if (isModeChange(gun_index))
  5919. {
  5920. DEBUG_INFO("========== SYS_MODE_CHARGING (%d) ========== \n", gun_index);
  5921. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5922. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  5923. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5924. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  5925. ocpp_set_auth_req(OFF);
  5926. ocpp_reset_smartcharging_profileId(gun_index);
  5927. ocpp_set_profile_req(gun_index, ON);
  5928. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5929. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5930. }
  5931. if ((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)
  5932. || (ShmCharger->gun_info[gun_index].rfidReq == ON)
  5933. || (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON)
  5934. || ocpp_get_remotestop(gun_index)
  5935. || (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index))
  5936. || ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE)
  5937. && getTargetChargingInfoData(gun_index)->PilotState != CP_STATE_C)
  5938. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5939. && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)
  5940. || ocpp_get_reset_req() || ocpp_get_unlocker_req(gun_index)
  5941. || (getTargetChargingInfoData(gun_index)->schedule.isTriggerStop == ON)
  5942. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5943. && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE))
  5944. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5945. && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress
  5946. == HLC_RENEGOTIATE_MODE))
  5947. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5948. && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE))
  5949. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5950. && ((49 <= ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus)
  5951. && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus <= 255))))
  5952. {
  5953. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5954. }
  5955. else
  5956. {
  5957. // Charging session info calculation
  5958. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  5959. getTargetChargingInfoData(gun_index)->PowerConsumption =
  5960. (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption / 10000.0);
  5961. else
  5962. {
  5963. getTargetChargingInfoData(gun_index)->PowerConsumption =
  5964. (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption / 10000.0);
  5965. }
  5966. refreshStartTimer(&endChargingTime[gun_index]);
  5967. getTargetChargingInfoData(gun_index)->PresentChargedDuration =
  5968. getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  5969. presentChargedEnergyUpdate(gun_index);
  5970. // Response StartTransactionConf
  5971. ocpp_set_starttransaction_conf(gun_index, OFF);
  5972. // Charging profile preparation
  5973. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  5974. || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  5975. {
  5976. if (!ocpp_get_profile_conf(gun_index))
  5977. {
  5978. ocpp_set_profile_req(gun_index, ON);
  5979. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5980. }
  5981. else
  5982. {
  5983. ocpp_set_profile_conf(gun_index, OFF);
  5984. }
  5985. }
  5986. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  5987. checkChargingProfileLimit(gun_index, getTargetChargingInfoData(gun_index)->SystemStatus);
  5988. // Charging session local limit condition check
  5989. if (ocpp_get_connection_status())
  5990. {
  5991. // On-line max condition check
  5992. if ((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  5993. && (getTargetChargingInfoData(gun_index)->PresentChargedDuration
  5994. >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60)))
  5995. {
  5996. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5997. {
  5998. setRelay(gun_index, OFF);
  5999. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  6000. }
  6001. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6002. {
  6003. setRelay(gun_index, OFF);
  6004. }
  6005. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6006. {
  6007. setRelay(gun_index, OFF);
  6008. }
  6009. else
  6010. {
  6011. }
  6012. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  6013. DEBUG_INFO("Connector-%d charging duration(%d) already over max duration(%d) in second.\n",
  6014. gun_index, getTargetChargingInfoData(gun_index)->PresentChargedDuration,
  6015. (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60));
  6016. }
  6017. else if ((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  6018. && (getTargetChargingInfoData(gun_index)->PresentChargedEnergy
  6019. >= ((float) ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  6020. {
  6021. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6022. {
  6023. setRelay(gun_index, OFF);
  6024. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  6025. }
  6026. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6027. {
  6028. setRelay(gun_index, OFF);
  6029. }
  6030. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6031. {
  6032. setRelay(gun_index, OFF);
  6033. }
  6034. else
  6035. {
  6036. }
  6037. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  6038. DEBUG_INFO("Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\n",
  6039. gun_index, getTargetChargingInfoData(gun_index)->PresentChargedEnergy,
  6040. ((float )ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
  6041. }
  6042. else
  6043. {
  6044. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6045. {
  6046. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  6047. if (ShmCharger->gun_info[gun_index].targetCurrent > 0)
  6048. setRelay(gun_index, ON);
  6049. else
  6050. setRelay(gun_index, OFF);
  6051. }
  6052. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6053. {
  6054. if (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C)
  6055. setRelay(gun_index, ON);
  6056. }
  6057. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6058. {
  6059. if (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  6060. setRelay(gun_index, ON);
  6061. else
  6062. setRelay(gun_index, OFF);
  6063. }
  6064. else
  6065. {
  6066. }
  6067. }
  6068. }
  6069. else
  6070. {
  6071. // Off-line max condition check
  6072. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  6073. || ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  6074. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  6075. {
  6076. if (((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  6077. ? (getTargetChargingInfoData(gun_index)->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  6078. : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  6079. && (getTargetChargingInfoData(gun_index)->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))))
  6080. {
  6081. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6082. {
  6083. setRelay(gun_index, OFF);
  6084. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  6085. }
  6086. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6087. {
  6088. setRelay(gun_index, OFF);
  6089. }
  6090. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6091. {
  6092. setRelay(gun_index, OFF);
  6093. }
  6094. else
  6095. {
  6096. }
  6097. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  6098. DEBUG_INFO( "Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\n",
  6099. gun_index, getTargetChargingInfoData(gun_index)->PresentChargedDuration,
  6100. (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60));
  6101. }
  6102. else if (((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  6103. ? (getTargetChargingInfoData(gun_index)->PresentChargedEnergy >= ((float) ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  6104. : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  6105. && (getTargetChargingInfoData(gun_index)->PresentChargedEnergy >= ((float) ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
  6106. {
  6107. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6108. {
  6109. setRelay(gun_index, OFF);
  6110. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  6111. }
  6112. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6113. {
  6114. setRelay(gun_index, OFF);
  6115. }
  6116. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6117. {
  6118. setRelay(gun_index, OFF);
  6119. }
  6120. else
  6121. {
  6122. }
  6123. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  6124. DEBUG_INFO( "Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\n",
  6125. gun_index, getTargetChargingInfoData(gun_index)->PresentChargedEnergy,
  6126. ((float )ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
  6127. }
  6128. else
  6129. {
  6130. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6131. {
  6132. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  6133. if (ShmCharger->gun_info[gun_index].targetCurrent > 0)
  6134. setRelay(gun_index, ON);
  6135. else
  6136. setRelay(gun_index, OFF);
  6137. }
  6138. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6139. {
  6140. if (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C)
  6141. setRelay(gun_index, ON);
  6142. }
  6143. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6144. {
  6145. if (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  6146. setRelay(gun_index, ON);
  6147. else
  6148. setRelay(gun_index, OFF);
  6149. }
  6150. else
  6151. {
  6152. }
  6153. }
  6154. }
  6155. else
  6156. {
  6157. if (ShmCharger->gun_info[gun_index].primaryMcuState.relay_state > 0)
  6158. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  6159. setRelay(gun_index, OFF);
  6160. }
  6161. }
  6162. // Debug information
  6163. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  6164. || (getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
  6165. {
  6166. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  6167. getDateTimeString((char*) getTargetChargingInfoData(gun_index)->StopDateTime);
  6168. DB_Update_Record_Buf(localDb, gun_index);
  6169. }
  6170. }
  6171. break;
  6172. case SYS_MODE_TERMINATING:
  6173. if (isModeChange(gun_index))
  6174. {
  6175. DEBUG_INFO("========== SYS_MODE_TERMINATING (%d)========== \n", gun_index);
  6176. if (!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  6177. {
  6178. setLedMotion(gun_index, LED_ACTION_STOP);
  6179. }
  6180. else
  6181. {
  6182. }
  6183. getDateTimeString((char*) getTargetChargingInfoData(gun_index)->StopDateTime);
  6184. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  6185. if (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)
  6186. setRelay(gun_index, OFF);
  6187. }
  6188. refreshStartTimer(&endChargingTime[gun_index]);
  6189. getTargetChargingInfoData(gun_index)->PresentChargedDuration = getDiffSecBetween(
  6190. startChargingTime[gun_index], endChargingTime[gun_index]);
  6191. // End authorize pass
  6192. if (((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser())
  6193. || ((ShmCharger->gun_info[gun_index].rfidReq == ON) && ocpp_get_auth_result(YES, gun_index))
  6194. || (!ocpp_get_connection_status()
  6195. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)
  6196. && (ShmCharger->gun_info[gun_index].rfidReq == ON))
  6197. || (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS)
  6198. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)
  6199. && (ShmCharger->gun_info[gun_index].rfidReq == ON))
  6200. || (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON)
  6201. || ocpp_get_remotestop(gun_index)
  6202. || (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index))
  6203. || ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE)
  6204. && ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A)
  6205. || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  6206. && ocpp_get_StopTransactionOnEVSideDisconnect())
  6207. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6208. && ((!ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)
  6209. || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)))
  6210. || ocpp_get_reset_req() || ocpp_get_unlocker_req(gun_index)
  6211. || (getTargetChargingInfoData(gun_index)->schedule.isTriggerStop == ON)
  6212. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6213. && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  6214. {
  6215. setLedMotion(gun_index, LED_ACTION_STOP);
  6216. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6217. {
  6218. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  6219. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  6220. sleep(1);
  6221. checkStopReason(gun_index);
  6222. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6223. }
  6224. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6225. {
  6226. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6227. //Cancel CCS task negotiating
  6228. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  6229. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  6230. setRelay(gun_index, OFF);
  6231. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  6232. }
  6233. else
  6234. {
  6235. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6236. }
  6237. }
  6238. else
  6239. {
  6240. // If the charger stops charging, the led should be stoped blink
  6241. if (!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  6242. {
  6243. setLedMotion(gun_index, LED_ACTION_STOP);
  6244. }
  6245. // Charging profile preparation
  6246. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  6247. || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  6248. {
  6249. if (!ocpp_get_profile_conf(gun_index))
  6250. {
  6251. ocpp_set_profile_req(gun_index, ON);
  6252. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  6253. }
  6254. else
  6255. {
  6256. ocpp_set_profile_conf(gun_index, OFF);
  6257. }
  6258. }
  6259. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  6260. checkChargingProfileLimit(gun_index, getTargetChargingInfoData(gun_index)->SystemStatus);
  6261. setRelay(gun_index, OFF);
  6262. // Debug information
  6263. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO])
  6264. > TIMEOUT_SPEC_LOGPPRINTOUT)
  6265. || (getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
  6266. {
  6267. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  6268. }
  6269. if (!ocpp_get_auth_result(YES, gun_index))
  6270. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  6271. if ((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
  6272. && (((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE)
  6273. && (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C))
  6274. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6275. && ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn))
  6276. && (ShmCharger->gun_info[gun_index].rfidReq != ON)
  6277. && (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON)
  6278. && !ocpp_get_remotestop(gun_index)
  6279. && (getTargetChargingInfoData(gun_index)->schedule.isTriggerStop != ON)
  6280. && !ocpp_get_reset_req() && ocpp_get_starttransaction_result(gun_index)
  6281. && !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  6282. && (getTargetChargingInfoData(gun_index)->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60)))
  6283. && !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  6284. && (getTargetChargingInfoData(gun_index)->PresentChargedEnergy >= ((float) ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  6285. && !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)
  6286. && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  6287. ? (getTargetChargingInfoData(gun_index)->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  6288. : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  6289. && (getTargetChargingInfoData(gun_index)->PresentChargedDuration
  6290. >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))))
  6291. && !(!ocpp_get_connection_status()
  6292. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)
  6293. && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  6294. ? (getTargetChargingInfoData(gun_index)->PresentChargedEnergy >= ((float) ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  6295. : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  6296. && (getTargetChargingInfoData(gun_index)->PresentChargedEnergy >= ((float) ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
  6297. && (((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6298. && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE))
  6299. || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6300. || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)))
  6301. {
  6302. sleep(1);
  6303. setChargerMode(gun_index, SYS_MODE_CHARGING);
  6304. }
  6305. }
  6306. if ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6307. && (ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON)
  6308. && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  6309. {
  6310. DEBUG_INFO("Set PWM duty 100%% go to SYS_MODE_TERMINATING.\n");
  6311. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  6312. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  6313. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  6314. sleep(1);
  6315. checkStopReason(gun_index);
  6316. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6317. }
  6318. break;
  6319. case SYS_MODE_COMPLETE:
  6320. if (isModeChange(gun_index))
  6321. {
  6322. DEBUG_INFO("========== SYS_MODE_COMPLETE (%d)========== \n", gun_index);
  6323. setLedMotion(gun_index, LED_ACTION_STOP);
  6324. sleep(6);
  6325. setRelay(gun_index, OFF);
  6326. setRequest(gun_index, OFF);
  6327. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_A;
  6328. }
  6329. if (((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE)
  6330. && (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A))
  6331. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6332. && (!ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn))
  6333. || ocpp_get_reset_req())
  6334. {
  6335. sleep(2);
  6336. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  6337. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  6338. ocpp_set_remotestop(gun_index, OFF);
  6339. getTargetChargingInfoData(gun_index)->schedule.isTriggerStop = OFF;
  6340. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  6341. DB_Insert_Record(localDb, gun_index);
  6342. setChargerMode(gun_index, SYS_MODE_IDLE);
  6343. }
  6344. break;
  6345. case SYS_MODE_ALARM:
  6346. setLedMotion(gun_index, LED_ACTION_ALARM);
  6347. setRelay(gun_index, OFF);
  6348. DEBUG_INFO("========== SYS_MODE_ALARM ========== \n");
  6349. if ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6350. || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC))
  6351. {
  6352. if ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A))
  6353. {
  6354. setRequest(gun_index, OFF);
  6355. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  6356. }
  6357. }
  6358. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6359. {
  6360. if (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn != ON)
  6361. {
  6362. setRequest(gun_index, OFF);
  6363. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  6364. }
  6365. }
  6366. else
  6367. {
  6368. }
  6369. /*
  6370. * 1. Customize emergency stop and report stop transaction then return to idle mode if error recover.
  6371. * 2. Customer: Bambang
  6372. */
  6373. if (((ShmSysConfigAndInfo->SysConfig.ModelName[VENDOR_CODE] == 'B')
  6374. && (ShmSysConfigAndInfo->SysConfig.ModelName[VENDOR_CODE + 1] == 'B'))
  6375. && (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == ALARM_EMERGENCY_STOP))
  6376. {
  6377. if (((getTargetChargingInfoData(gun_index)->PreviousSystemStatus == SYS_MODE_CHARGING)
  6378. || (getTargetChargingInfoData(gun_index)->PreviousSystemStatus == SYS_MODE_TERMINATING))
  6379. && !ShmCharger->gun_info[gun_index].isEmergencyStopReport)
  6380. {
  6381. checkStopReason(gun_index);
  6382. ShmCharger->gun_info[gun_index].isEmergencyStopReport = ON;
  6383. }
  6384. }
  6385. if ((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0)
  6386. && !ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
  6387. {
  6388. if (((getTargetChargingInfoData(gun_index)->PreviousSystemStatus == SYS_MODE_CHARGING)
  6389. || (getTargetChargingInfoData(gun_index)->PreviousSystemStatus == SYS_MODE_TERMINATING))
  6390. && (!ShmCharger->gun_info[gun_index].isEmergencyStopReport))
  6391. {
  6392. setChargerMode(gun_index, getTargetChargingInfoData(gun_index)->PreviousSystemStatus);
  6393. }
  6394. else
  6395. {
  6396. setChargerMode(gun_index, SYS_MODE_IDLE);
  6397. }
  6398. }
  6399. break;
  6400. case SYS_MODE_FAULT:
  6401. if (isModeChange(gun_index))
  6402. {
  6403. DEBUG_INFO("========== SYS_MODE_FAULT (%d) ========== \n", gun_index);
  6404. }
  6405. break;
  6406. case SYS_MODE_MAINTAIN:
  6407. if (isModeChange(gun_index))
  6408. {
  6409. setLedMotion(gun_index, LED_ACTION_MAINTAIN);
  6410. DEBUG_INFO("========== SYS_MODE_MAINTAIN (%d) ========== \n", gun_index);
  6411. }
  6412. break;
  6413. case SYS_MODE_UPDATE:
  6414. if (isModeChange(gun_index))
  6415. {
  6416. DEBUG_INFO("========== SYS_MODE_UPDATE (%d) ========== \n", gun_index);
  6417. setLedMotion(gun_index, LED_ACTION_MAINTAIN);
  6418. }
  6419. //======================================
  6420. // Check local upgrade firmware request
  6421. //======================================
  6422. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  6423. {
  6424. DEBUG_INFO("Firmware local upgrading...\n");
  6425. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  6426. int value = upgrade_check();
  6427. DEBUG_INFO("Local update Value: %s... \n", ((value == PASS)?"Pass": "Fail"));
  6428. if (value == PASS)
  6429. {
  6430. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  6431. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  6432. ShmCharger->isUpdateSuccess = YES;
  6433. DEBUG_INFO("Local update success...\n");
  6434. }
  6435. else
  6436. {
  6437. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  6438. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  6439. ShmCharger->isUpdateSuccess = NO;
  6440. DEBUG_INFO("Local update unsuccess...\n");
  6441. }
  6442. }
  6443. else if (ocpp_get_update_firmware_req())
  6444. {
  6445. //======================================
  6446. // Check remote upgrade firmware request
  6447. //======================================
  6448. checkRemoteUpgradeStatus();
  6449. }
  6450. else
  6451. {
  6452. //======================================
  6453. // Upgrade complete reboot system
  6454. //======================================
  6455. if (!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq
  6456. && ((modelnameInfo.GetGunCount > 1) ? !ShmCharger->gun_info[gun_index ^ 1].mcuFlag.isMcuUpgradeReq : YES)
  6457. && !ShmCharger->isUpgradeLcmReq)
  6458. {
  6459. if (ShmCharger->isUpdateSuccess == YES)
  6460. {
  6461. sprintf((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  6462. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6463. sprintf((char*) ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  6464. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6465. DEBUG_WARN("Firmware upgrade success.\n");
  6466. sleep(5);
  6467. DEBUG_INFO("Firmware upgraded, reboot...\n");
  6468. system("reboot -f");
  6469. sleep(5);
  6470. system("reboot -f");
  6471. }
  6472. else
  6473. {
  6474. sprintf((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  6475. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6476. sprintf((char*) ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  6477. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6478. DEBUG_WARN("Firmware upgrade fail.\n");
  6479. sleep(5);
  6480. system("rm -rvf /mnt/* ");
  6481. close(wtdFd);
  6482. system("/usr/bin/run_evse_restart.sh");
  6483. }
  6484. }
  6485. }
  6486. break;
  6487. case SYS_MODE_RESERVATION:
  6488. if (isModeChange(gun_index))
  6489. {
  6490. DEBUG_INFO("========== SYS_MODE_RESERVATION (%d)========== \n", gun_index);
  6491. }
  6492. setLedMotion(gun_index, LED_ACTION_RESERVATION_MODE);
  6493. if (isReservationExpired(gun_index))
  6494. {
  6495. DEBUG_INFO("Reservation: Time's up...\n");
  6496. setChargerMode(gun_index, SYS_MODE_IDLE);
  6497. }
  6498. else if (ocpp_get_cancelreservation_req(gun_index))
  6499. {
  6500. DEBUG_INFO("Reservation: Cancel reservation...\n");
  6501. setChargerMode(gun_index, SYS_MODE_IDLE);
  6502. ocpp_set_cancelreservation_req(gun_index, OFF);
  6503. }
  6504. else
  6505. {
  6506. // Check is there RFID or back end request start
  6507. if ((ShmCharger->gun_info[gun_index].rfidReq == ON) || ocpp_get_remotestart(gun_index))
  6508. {
  6509. if ((ShmCharger->gun_info[gun_index].rfidReq == ON))
  6510. {
  6511. if (ocpp_compare_reserve_id_with_user(gun_index))
  6512. {
  6513. DEBUG_INFO("Start Method in reservation : RFID...\n");
  6514. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  6515. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_RFID;
  6516. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_ISO14443;
  6517. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  6518. }
  6519. else
  6520. {
  6521. DEBUG_INFO("It's not reserve user id.\n");
  6522. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  6523. }
  6524. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  6525. }
  6526. else if (ocpp_get_remotestart(gun_index))
  6527. {
  6528. if (ocpp_compare_reserve_id_with_remote_user(gun_index))
  6529. {
  6530. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  6531. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_BACKEND;
  6532. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_Central;
  6533. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  6534. }
  6535. ocpp_set_remotestop(gun_index, OFF);
  6536. }
  6537. }
  6538. }
  6539. break;
  6540. case SYS_MODE_BOOKING:
  6541. if (isModeChange(gun_index))
  6542. {
  6543. DEBUG_INFO("========== SYS_MODE_BOOKING (%d)========== \n", gun_index);
  6544. }
  6545. break;
  6546. case SYS_MODE_DEBUG:
  6547. if (isModeChange(gun_index))
  6548. {
  6549. setLedMotion(gun_index, LED_ACTION_DEBUG);
  6550. DEBUG_INFO("========== SYS_MODE_DEBUG (%d)========== \n", gun_index);
  6551. }
  6552. break;
  6553. }
  6554. }
  6555. // System watch dog reset
  6556. write(wtdFd, "a", 1);
  6557. usleep(50000);
  6558. }
  6559. return FAIL;
  6560. }