main.c 275 KB

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