main.c 249 KB

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