main.c 260 KB

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