main.c 251 KB

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