main.c 247 KB

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