main.c 256 KB

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