main.c 253 KB

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