main.c 275 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243
  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. sleep(5);
  2390. }
  2391. }
  2392. DEBUG_INFO("Initial Ethernet OK\n");
  2393. }
  2394. int SpawnTask(uint8_t gun_index)
  2395. {
  2396. if(gun_index == 0)
  2397. {
  2398. system ("pkill Module_");
  2399. system ("pkill OcppBackend");
  2400. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2401. {
  2402. system("/root/Module_4g &");
  2403. }
  2404. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2405. {
  2406. system("/root/Module_Wifi &");
  2407. }
  2408. system ("/root/Module_EventLogging &");
  2409. system ("/root/Module_AlarmDetect &");
  2410. system ("/root/Module_InternalComm &");
  2411. system ("/root/Module_Speaker &");
  2412. system ("/root/Module_ProduceUtils &");
  2413. system ("/root/Module_LcmControl &");
  2414. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  2415. {
  2416. ocpp_process_start();
  2417. }
  2418. else
  2419. {
  2420. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  2421. ocpp_process_start();
  2422. }
  2423. }
  2424. return PASS;
  2425. }
  2426. int InitQca7000()
  2427. {
  2428. int result = PASS;
  2429. system("/sbin/rmmod qcaspi");
  2430. if(isKernelSupportNAT() == YES)
  2431. system("/sbin/insmod /lib/qcaspi_nat.ko");
  2432. else
  2433. system("/sbin/insmod /lib/qcaspi.ko");
  2434. sleep(2);
  2435. /*+++ vern, for CCS +++*/
  2436. //system("/sbin/ifconfig eth1 192.168.253.11 netmask 255.255.255.0 up");
  2437. system("/sbin/ifconfig eth1 192.168.0.11 netmask 255.255.255.0 up");
  2438. /*--- vern, for CCS ---*/
  2439. sleep(1);
  2440. return result;
  2441. }
  2442. int Initialization(uint8_t gun_index)
  2443. {
  2444. int result = PASS;
  2445. if(gun_index == 0)
  2446. {
  2447. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2448. InitGPIO();
  2449. InitEthernet();
  2450. if(InitQca7000() != PASS)
  2451. {
  2452. DEBUG_ERROR("QCA7000 initial fail.\n");
  2453. result = FAIL;
  2454. }
  2455. if(DB_Open(localDb) != PASS)
  2456. {
  2457. DEBUG_ERROR("Local database initial fail.\n");
  2458. result = FAIL;
  2459. }
  2460. if((rfidFd = InitRfidPort()) == FAIL)
  2461. {
  2462. DEBUG_ERROR("RFID port initial fail.\n");
  2463. result = FAIL;
  2464. }
  2465. if((wtdFd = InitWatchDog()) == FAIL)
  2466. {
  2467. DEBUG_ERROR("Watchdog initial fail.\n");
  2468. result = FAIL;
  2469. }
  2470. if(result == PASS)
  2471. DEBUG_INFO("Initialization OK.\n");
  2472. else
  2473. DEBUG_INFO("Initialization Fail.\n");
  2474. }
  2475. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2476. return result;
  2477. }
  2478. //=====================================================
  2479. // Common routine
  2480. //=====================================================
  2481. char* getSystemModeName(unsigned char mode)
  2482. {
  2483. char* result;
  2484. switch(mode)
  2485. {
  2486. case SYS_MODE_BOOTING:
  2487. result = "booting";
  2488. break;
  2489. case SYS_MODE_IDLE:
  2490. result = "idle";
  2491. break;
  2492. case SYS_MODE_AUTHORIZING:
  2493. result = "authorizing";
  2494. break;
  2495. case SYS_MODE_PREPARING:
  2496. result = "preparing";
  2497. break;
  2498. case SYS_MODE_CHARGING:
  2499. result = "charging";
  2500. break;
  2501. case SYS_MODE_TERMINATING:
  2502. result = "terminating";
  2503. break;
  2504. case SYS_MODE_COMPLETE:
  2505. result = "complete";
  2506. break;
  2507. case SYS_MODE_ALARM:
  2508. result = "alarm";
  2509. break;
  2510. case SYS_MODE_FAULT:
  2511. result = "fault";
  2512. break;
  2513. case SYS_MODE_MAINTAIN:
  2514. result = "maintain";
  2515. break;
  2516. case SYS_MODE_RESERVATION:
  2517. result = "reservation";
  2518. break;
  2519. case SYS_MODE_BOOKING:
  2520. result = "booking";
  2521. break;
  2522. case SYS_MODE_DEBUG:
  2523. result = "debug";
  2524. break;
  2525. case SYS_MODE_UPDATE:
  2526. result = "upgrade";
  2527. break;
  2528. default:
  2529. result = "unknown";
  2530. break;
  2531. }
  2532. return result;
  2533. }
  2534. void setChargerMode(unsigned char gun_index, unsigned char mode)
  2535. {
  2536. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  2537. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  2538. 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));
  2539. }
  2540. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  2541. {
  2542. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  2543. }
  2544. unsigned char isModeChange(unsigned char gun_index)
  2545. {
  2546. unsigned char result = NO;
  2547. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  2548. {
  2549. result = YES;
  2550. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  2551. }
  2552. return result;
  2553. }
  2554. void gpio_set_value(unsigned int gpio, unsigned int value)
  2555. {
  2556. int fd;
  2557. char buf[256];
  2558. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2559. fd = open(buf, O_WRONLY);
  2560. if (fd < 0)
  2561. {
  2562. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  2563. return;
  2564. }
  2565. if (value)
  2566. write(fd, "1", 2);
  2567. else
  2568. write(fd, "0", 2);
  2569. close(fd);
  2570. }
  2571. int gpio_get_value(unsigned int gpio)
  2572. {
  2573. int fd;
  2574. char buf[256];
  2575. char ch;
  2576. int8_t result = FAIL;
  2577. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2578. fd = open(buf, O_RDONLY);
  2579. if (fd < 0)
  2580. {
  2581. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  2582. return result;
  2583. }
  2584. read(fd, &ch, 1);
  2585. if (ch != '0')
  2586. result = 1;
  2587. else
  2588. result = 0;
  2589. close(fd);
  2590. return result;
  2591. }
  2592. int presentChargedEnergyClear(unsigned char gun_index)
  2593. {
  2594. int result = FAIL;
  2595. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2596. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod)*sizeof(float));
  2597. result = PASS;
  2598. return result;
  2599. }
  2600. float presentChargedEnergyTotal(unsigned char gun_index)
  2601. {
  2602. float result = 0.0f;
  2603. for(int idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod);idx++)
  2604. {
  2605. //result += ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idx];
  2606. result += (((int)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idx]*10000))/(float)10000);
  2607. }
  2608. return result;
  2609. }
  2610. int presentChargedEnergyUpdate(unsigned char gun_index)
  2611. {
  2612. int result = FAIL;
  2613. time_t CurrentTime;
  2614. struct tm *tm;
  2615. CurrentTime = time(NULL);
  2616. tm=localtime(&CurrentTime);
  2617. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  2618. {
  2619. // Resolution: 0.0001 kwh
  2620. 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);
  2621. }
  2622. else
  2623. {
  2624. // Resolution: 0.0001 kwh
  2625. 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) +
  2626. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start))/10000.0) +
  2627. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start))/10000.0))
  2628. - presentChargedEnergyTotal(gun_index);
  2629. }
  2630. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = presentChargedEnergyTotal(gun_index);
  2631. return result;
  2632. }
  2633. //===============================================
  2634. // Get firmware version
  2635. //===============================================
  2636. void get_firmware_version(unsigned char gun_index)
  2637. {
  2638. FILE *fp;
  2639. char cmd[512];
  2640. char buf[512];
  2641. // Get CSU hardware version
  2642. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  2643. // Get CSU boot loader version
  2644. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  2645. // Get CSU kernel version
  2646. sprintf(cmd, "/bin/uname -r");
  2647. fp = popen(cmd, "r");
  2648. if(fp == NULL)
  2649. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  2650. else
  2651. {
  2652. while(fgets(buf, sizeof(buf), fp) != NULL)
  2653. {
  2654. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  2655. }
  2656. }
  2657. // Get MCU firmware version
  2658. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2659. // Get CSU root file system version
  2660. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "B0.62.00.0000.00");
  2661. // Get AC connector type from model name
  2662. for(uint8_t idx=0;idx<3;idx++)
  2663. {
  2664. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  2665. {
  2666. case '1':
  2667. // J1772 Plug
  2668. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  2669. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  2670. break;
  2671. case '2':
  2672. // J1772 Socket
  2673. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  2674. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  2675. break;
  2676. case '3':
  2677. // CE Plug
  2678. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  2679. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  2680. break;
  2681. case '4':
  2682. // CE Socket
  2683. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  2684. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  2685. break;
  2686. case '5':
  2687. // GB Plug
  2688. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  2689. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  2690. break;
  2691. case '6':
  2692. // GB Socket
  2693. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  2694. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  2695. break;
  2696. }
  2697. }
  2698. // Get network option from model name
  2699. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  2700. {
  2701. case 'B':
  2702. case 'U':
  2703. //Blue tooth
  2704. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  2705. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  2706. break;
  2707. case 'W':
  2708. // WIFI
  2709. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  2710. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  2711. break;
  2712. case 'T':
  2713. // 3G/4G
  2714. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  2715. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  2716. break;
  2717. case 'D':
  2718. // WIFI + 4G
  2719. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  2720. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  2721. break;
  2722. default:
  2723. // LAN
  2724. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  2725. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  2726. break;
  2727. }
  2728. // Get rating power from model name
  2729. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2730. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2731. // Get vender code from model name
  2732. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2733. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2734. DEBUG_INFO("========================================\n");
  2735. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2736. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  2737. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  2738. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  2739. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2740. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2741. DEBUG_INFO("========================================\n");
  2742. }
  2743. //===============================================
  2744. // Upgrade firmware
  2745. //===============================================
  2746. int upgrade_check()
  2747. {
  2748. int result = PASS;
  2749. int fd;
  2750. DIR *dir;
  2751. struct dirent *file;
  2752. char cmd[512];
  2753. long int MaxLen=48*1024*1024, ImageLen=0, wrd;
  2754. if ((dir = opendir ("/mnt")) != NULL)
  2755. {
  2756. /* print all the files and directories within directory */
  2757. while ((file = readdir (dir)) != NULL)
  2758. {
  2759. if((strlen(file->d_name)>2))
  2760. {
  2761. // Wait for MCU upgrade finish.
  2762. while(ShmCharger->gun_info[0].mcuFlag.isMcuUpgradeReq || (AC_QUANTITY>1?ShmCharger->gun_info[1].mcuFlag.isMcuUpgradeReq:FALSE))sleep(1);
  2763. // Wait for LCM upgrade finish.
  2764. while(ShmCharger->isUpgradeLcmReq)sleep(1);
  2765. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  2766. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  2767. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  2768. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  2769. {
  2770. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2771. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2772. ImageLen = read(fd, ptr, MaxLen);
  2773. close(fd);
  2774. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  2775. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  2776. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  2777. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  2778. if((ShmCharger->fwUpgradeInfo.modelName[0] == ShmSysConfigAndInfo->SysConfig.ModelName[0]) &&
  2779. (ShmCharger->fwUpgradeInfo.modelName[1] == ShmSysConfigAndInfo->SysConfig.ModelName[1]) &&
  2780. (ShmCharger->fwUpgradeInfo.modelName[7] == ShmSysConfigAndInfo->SysConfig.ModelName[7]) &&
  2781. (ShmCharger->fwUpgradeInfo.modelName[8] == ShmSysConfigAndInfo->SysConfig.ModelName[8]) &&
  2782. (ShmCharger->fwUpgradeInfo.modelName[9] == ShmSysConfigAndInfo->SysConfig.ModelName[9]) &&
  2783. (ShmCharger->fwUpgradeInfo.modelName[11] == ShmSysConfigAndInfo->SysConfig.ModelName[11]) &&
  2784. (ShmCharger->fwUpgradeInfo.modelName[12] == ShmSysConfigAndInfo->SysConfig.ModelName[12]) &&
  2785. (ShmCharger->fwUpgradeInfo.modelName[13] == ShmSysConfigAndInfo->SysConfig.ModelName[13]) &&
  2786. (ShmCharger->fwUpgradeInfo.fwType>0))
  2787. {
  2788. switch(ShmCharger->fwUpgradeInfo.fwType)
  2789. {
  2790. case CSU_MLO:
  2791. case CSU_BOOTLOADER:
  2792. case CSU_KERNEL_CONFIGURATION:
  2793. case CSU_KERNEL_IMAGE:
  2794. case CSU_ROOT_FILE_SYSTEM:
  2795. case CSU_USER_CONFIGURATION:
  2796. case CSU_PRIMARY_CONTROLLER:
  2797. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  2798. {
  2799. result = FAIL;
  2800. }
  2801. else
  2802. {
  2803. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  2804. {
  2805. DEBUG_INFO("Restore model name & serial number.\n");
  2806. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2807. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2808. {
  2809. DEBUG_INFO("Re-load configuration fail.\n");
  2810. result = FAIL;
  2811. }
  2812. else
  2813. {
  2814. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  2815. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  2816. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  2817. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2818. {
  2819. DEBUG_INFO("Re-write configuration fail.\n");
  2820. result = FAIL;
  2821. }
  2822. else
  2823. DEBUG_INFO("Re-write configuration OK.\n");
  2824. }
  2825. }
  2826. }
  2827. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2828. system(cmd);
  2829. break;
  2830. case AC_WALLMOUNT_CONTROLLER:
  2831. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2832. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  2833. sleep(10);
  2834. break;
  2835. case LCM:
  2836. fd = open("/mnt/lcm.zip", O_RDWR | O_CREAT | O_EXCL);
  2837. if (fd < 0)
  2838. {
  2839. DEBUG_ERROR("Can not create lcm.zip file.\n");
  2840. result = FAIL;
  2841. }
  2842. else
  2843. {
  2844. // Write image to flash
  2845. DEBUG_INFO("Writing data to lcm.zip file...\n");
  2846. wrd=write(fd, ptr+48, ImageLen-48);
  2847. close(fd);
  2848. DEBUG_INFO(">> lcm.zip Written length: 0x%x\n", wrd);
  2849. if(wrd != (ImageLen-48))
  2850. {
  2851. result = FAIL;
  2852. }
  2853. else
  2854. {
  2855. runShellCmd("mkdir -p /mnt/lcd");
  2856. runShellCmd("unzip /mnt/lcm.zip -d /mnt/lcd");
  2857. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2858. system(cmd);
  2859. system("rm -f /mnt/lcm.zip");
  2860. result = PASS;
  2861. }
  2862. }
  2863. ShmCharger->isUpgradeLcmReq = ON;
  2864. sleep(10);
  2865. break;
  2866. default:
  2867. result = FAIL;
  2868. DEBUG_WARN("Image file is unknown type.\n");
  2869. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2870. system(cmd);
  2871. break;
  2872. }
  2873. }
  2874. else
  2875. {
  2876. result = FAIL;
  2877. DEBUG_ERROR("Model name and Firmware type error.\n");
  2878. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2879. system(cmd);
  2880. }
  2881. free(ptr);
  2882. }
  2883. else
  2884. {
  2885. result = FAIL;
  2886. DEBUG_ERROR("New firmware open error.\n");
  2887. }
  2888. }
  2889. else
  2890. {
  2891. if(strlen(file->d_name) >= 3)
  2892. {
  2893. result = FAIL;
  2894. DEBUG_ERROR("File name error.\n");
  2895. }
  2896. else
  2897. {
  2898. DEBUG_ERROR("Searching file.\n");
  2899. }
  2900. }
  2901. }
  2902. closedir (dir);
  2903. }
  2904. else
  2905. {
  2906. result = FAIL;
  2907. DEBUG_ERROR("/mnt does not valid.\n");
  2908. }
  2909. return result;
  2910. }
  2911. //===============================================
  2912. // Check RFID is match with start user
  2913. //===============================================
  2914. int isMatchStartUser(unsigned char gun_index)
  2915. {
  2916. uint8_t tmpUser[32];
  2917. uint8_t isPrintLogOut = OFF;
  2918. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2919. {
  2920. // Big endian
  2921. switch(rfid.snType)
  2922. {
  2923. case RFID_SN_TYPE_6BYTE:
  2924. 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]);
  2925. break;
  2926. case RFID_SN_TYPE_7BYTE:
  2927. 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]);
  2928. break;
  2929. case RFID_SN_TYPE_10BYTE:
  2930. 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]);
  2931. break;
  2932. case RFID_SN_TYPE_4BYTE:
  2933. default:
  2934. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2935. break;
  2936. }
  2937. }
  2938. else
  2939. {
  2940. // Little endian
  2941. switch(rfid.snType)
  2942. {
  2943. case RFID_SN_TYPE_6BYTE:
  2944. 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]);
  2945. break;
  2946. case RFID_SN_TYPE_7BYTE:
  2947. 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]);
  2948. break;
  2949. case RFID_SN_TYPE_10BYTE:
  2950. 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]);
  2951. break;
  2952. case RFID_SN_TYPE_4BYTE:
  2953. default:
  2954. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2955. break;
  2956. }
  2957. }
  2958. if(isPrintLogOut == OFF)
  2959. {
  2960. DEBUG_INFO("==== isMatchStartUser ==== \n");
  2961. DEBUG_INFO("tmpUser : %s \n", tmpUser);
  2962. DEBUG_INFO("StartUserId : %s \n", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
  2963. DEBUG_INFO("========================== \n");
  2964. isPrintLogOut = ON;
  2965. }
  2966. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2967. }
  2968. int isMatchPresentUser()
  2969. {
  2970. uint8_t tmpUser[32];
  2971. uint8_t isPrintLogOut = OFF;
  2972. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2973. {
  2974. // Big endian
  2975. switch(rfid.snType)
  2976. {
  2977. case RFID_SN_TYPE_6BYTE:
  2978. 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]);
  2979. break;
  2980. case RFID_SN_TYPE_7BYTE:
  2981. 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]);
  2982. break;
  2983. case RFID_SN_TYPE_10BYTE:
  2984. 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]);
  2985. break;
  2986. case RFID_SN_TYPE_4BYTE:
  2987. default:
  2988. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2989. break;
  2990. }
  2991. }
  2992. else
  2993. {
  2994. // Little endian
  2995. switch(rfid.snType)
  2996. {
  2997. case RFID_SN_TYPE_6BYTE:
  2998. 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]);
  2999. break;
  3000. case RFID_SN_TYPE_7BYTE:
  3001. 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]);
  3002. break;
  3003. case RFID_SN_TYPE_10BYTE:
  3004. 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]);
  3005. break;
  3006. case RFID_SN_TYPE_4BYTE:
  3007. default:
  3008. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3009. break;
  3010. }
  3011. }
  3012. if(isPrintLogOut == OFF)
  3013. {
  3014. DEBUG_INFO("==== isMatchPresentUser ==== \n");
  3015. DEBUG_INFO("tmpUser : %s \n", tmpUser);
  3016. DEBUG_INFO("UserId : %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3017. DEBUG_INFO("============================ \n");
  3018. isPrintLogOut = ON;
  3019. }
  3020. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysConfig.UserId)==0)?YES:NO);
  3021. }
  3022. //===============================================
  3023. // Read RFID Serial Number
  3024. //===============================================
  3025. int GetCardSerialNumber()
  3026. {
  3027. int isSuccess = FAIL;
  3028. int module_type = MODULE_EWT;
  3029. if(getRequestCardSN(rfidFd,module_type,&rfid))
  3030. {
  3031. if(rfid.cardType == ISO14443A)
  3032. {
  3033. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  3034. {
  3035. isSuccess = PASS;
  3036. }
  3037. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  3038. {
  3039. isSuccess = PASS;
  3040. }
  3041. sethaltCard(rfidFd,module_type);
  3042. }
  3043. else if(rfid.cardType == IS014443B)
  3044. {
  3045. isSuccess = PASS;
  3046. }
  3047. else if(rfid.cardType == FELICA)
  3048. {
  3049. isSuccess = PASS;
  3050. }
  3051. else
  3052. {}
  3053. }
  3054. else
  3055. {}
  3056. return isSuccess;
  3057. }
  3058. //===============================================
  3059. // Set led motion
  3060. //===============================================
  3061. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  3062. {
  3063. switch(led_mode)
  3064. {
  3065. case LED_ACTION_INIT:
  3066. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  3067. break;
  3068. case LED_ACTION_IDLE:
  3069. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  3070. break;
  3071. case LED_ACTION_AUTHED:
  3072. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  3073. break;
  3074. case LED_ACTION_CONNECTED:
  3075. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  3076. break;
  3077. case LED_ACTION_CHARGING:
  3078. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  3079. break;
  3080. case LED_ACTION_STOP:
  3081. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  3082. break;
  3083. case LED_ACTION_ALARM:
  3084. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  3085. break;
  3086. case LED_ACTION_MAINTAIN:
  3087. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  3088. break;
  3089. case LED_ACTION_RFID_PASS:
  3090. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  3091. break;
  3092. case LED_ACTION_RFID_FAIL:
  3093. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  3094. break;
  3095. case LED_ACTION_BLE_CONNECT:
  3096. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  3097. break;
  3098. case LED_ACTION_BLE_DISABLE:
  3099. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  3100. break;
  3101. case LED_ACTION_DEBUG:
  3102. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  3103. break;
  3104. case LED_ACTION_ALL_OFF:
  3105. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  3106. break;
  3107. case LED_RELAY_ON:
  3108. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  3109. break;
  3110. case LED_RELAY_OFF:
  3111. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  3112. break;
  3113. case LED_ACTION_HANDSHAKE_FAIL:
  3114. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  3115. break;
  3116. case LED_ACTION_INTERNET_DISCONNECT:
  3117. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  3118. break;
  3119. case LED_ACTION_RESTORE_SETTING:
  3120. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RESTORE_SETTING;
  3121. break;
  3122. case LED_ACTION_IDLE_BACKEND_CONNECTED:
  3123. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_CONNECTED;
  3124. break;
  3125. case LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP:
  3126. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP;
  3127. break;
  3128. case LED_ACTION_IDLE_BACKEND_DISCONNECTED:
  3129. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_DISCONNECTED;
  3130. break;
  3131. case LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP:
  3132. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP;
  3133. break;
  3134. case LED_ACTION_RESERVATION_MODE:
  3135. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RESERVATION_MODE;
  3136. break;
  3137. }
  3138. }
  3139. //===============================================
  3140. // Request on/off set
  3141. //===============================================
  3142. void setRequest(unsigned char gun_index,unsigned char isOn)
  3143. {
  3144. if(isOn == ON)
  3145. {
  3146. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  3147. {
  3148. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  3149. DEBUG_INFO("Gun-%d permission request: ON \n",gun_index);
  3150. }
  3151. }
  3152. else
  3153. {
  3154. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  3155. {
  3156. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  3157. DEBUG_INFO("Gun-%d permission request: OFF \n",gun_index);
  3158. }
  3159. }
  3160. }
  3161. //===============================================
  3162. // Request on/off get
  3163. //===============================================
  3164. int getRequest(unsigned char gun_index)
  3165. {
  3166. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  3167. }
  3168. //===============================================
  3169. // Relay on/off set
  3170. //===============================================
  3171. void setRelay(unsigned char gun_index,unsigned char isOn)
  3172. {
  3173. if(isOn == ON)
  3174. {
  3175. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  3176. {
  3177. switch(ShmCharger->gun_info[gun_index].chargingMode)
  3178. {
  3179. case CHARGING_MODE_BS:
  3180. case CHARGING_MODE_HLC:
  3181. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3182. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  3183. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  3184. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  3185. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3186. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_BS] \n",gun_index);
  3187. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3188. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_HLC] \n",gun_index);
  3189. break;
  3190. case CHARGING_MODE_SOCKETE:
  3191. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3192. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = 0x01;
  3193. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_SOCKETE] \n",gun_index);
  3194. break;
  3195. default:
  3196. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3197. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  3198. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  3199. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  3200. DEBUG_INFO("Gun-%d Output relay status: ON. [DEFFAULT] \n",gun_index);
  3201. break;
  3202. }
  3203. }
  3204. }
  3205. else
  3206. {
  3207. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  3208. {
  3209. switch(ShmCharger->gun_info[gun_index].chargingMode)
  3210. {
  3211. case CHARGING_MODE_BS:
  3212. case CHARGING_MODE_HLC:
  3213. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3214. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  3215. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  3216. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  3217. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3218. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_BS] \n",gun_index);
  3219. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3220. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_HLC] \n",gun_index);
  3221. break;
  3222. case CHARGING_MODE_SOCKETE:
  3223. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3224. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = 0;
  3225. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_SOCKETE] \n",gun_index);
  3226. break;
  3227. default:
  3228. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3229. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  3230. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  3231. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  3232. DEBUG_INFO("Gun-%d Output relay status: OFF. [DEFFAULT] \n",gun_index);
  3233. break;
  3234. }
  3235. }
  3236. }
  3237. }
  3238. //===============================================
  3239. // Relay on/off get
  3240. //===============================================
  3241. int getRelay(unsigned char gun_index)
  3242. {
  3243. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  3244. }
  3245. //===============================================
  3246. // Set speaker on/off request
  3247. //===============================================
  3248. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  3249. {
  3250. if(isOn == ON)
  3251. {
  3252. ShmCharger->isSpeakerOn = ON;
  3253. ShmCharger->speaker_type = speaker_mode;
  3254. }
  3255. }
  3256. //===============================================
  3257. // Initialization RFID communication port
  3258. //===============================================
  3259. int InitRfidPort()
  3260. {
  3261. int uartO2 = open(rfidPortName, O_RDWR);
  3262. struct termios tios;
  3263. if (uartO2 != FAIL)
  3264. {
  3265. ioctl (uartO2, TCGETS, &tios);
  3266. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  3267. tios.c_lflag = 0;
  3268. tios.c_iflag = 0;
  3269. tios.c_oflag = 0;
  3270. tios.c_cc[VMIN] = 0;
  3271. tios.c_cc[VTIME] = (unsigned char) 1;
  3272. tios.c_lflag = 0;
  3273. tcflush(uartO2, TCIFLUSH);
  3274. ioctl(uartO2, TCSETS, &tios);
  3275. }
  3276. if (uartO2 < 0)
  3277. {
  3278. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  3279. }
  3280. return uartO2;
  3281. }
  3282. //===============================================
  3283. // Check internet access status
  3284. //===============================================
  3285. int isReachableInternet()
  3286. {
  3287. int result = FAIL;
  3288. FILE *fp;
  3289. char cmd[256];
  3290. char buf[512];
  3291. char tmp[512];
  3292. // Get ip address & net mask
  3293. strcpy(cmd, "ifconfig eth0");
  3294. fp = popen(cmd, "r");
  3295. if (fp != NULL)
  3296. {
  3297. while(fgets(buf, sizeof(buf), fp) != NULL)
  3298. {
  3299. if (strstr(buf, "inet addr:") > 0)
  3300. {
  3301. sscanf(buf, "%*s%s", tmp);
  3302. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  3303. sscanf(buf, "%*s%*s%*s%s", tmp);
  3304. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp, strspn(tmp, "Mask:"), strlen(tmp)-strspn(tmp, "Mask:"));
  3305. }
  3306. }
  3307. }
  3308. pclose(fp);
  3309. memset(buf, 0x00, sizeof(buf));
  3310. // Get gateway
  3311. fp = popen("ip route", "r");
  3312. if(fp == NULL)
  3313. result = FAIL;
  3314. else
  3315. {
  3316. while(fgets(buf, sizeof(buf), fp) != NULL)
  3317. {
  3318. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  3319. break;
  3320. }
  3321. if(strstr(buf, "default") != NULL)
  3322. {
  3323. sscanf(buf, "%*s%*s%s", tmp);
  3324. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  3325. }
  3326. }
  3327. pclose(fp);
  3328. memset(buf, 0x00, sizeof(buf));
  3329. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  3330. {
  3331. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  3332. fp = popen(cmd, "r");
  3333. if(fp != NULL)
  3334. {
  3335. while(fgets(buf, sizeof(buf), fp) != NULL)
  3336. {
  3337. if(strstr(buf, "transmitted") > 0)
  3338. {
  3339. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  3340. if(strstr(buf,"100%") != NULL)
  3341. {
  3342. }
  3343. else
  3344. {
  3345. result = PASS;
  3346. }
  3347. //DEBUG_INFO("%s",buf);
  3348. //DEBUG_INFO("%s\n",tmp);
  3349. }
  3350. }
  3351. }
  3352. pclose(fp);
  3353. }
  3354. return result;
  3355. }
  3356. int isRouteFail()
  3357. {
  3358. int result = YES;
  3359. FILE *fp;
  3360. char buf[512];
  3361. fp = popen("route -n", "r");
  3362. if(fp != NULL)
  3363. {
  3364. while(fgets(buf, sizeof(buf), fp) != NULL)
  3365. {
  3366. if(strstr(buf, "eth0") != NULL)
  3367. result = NO;
  3368. }
  3369. }
  3370. pclose(fp);
  3371. return result;
  3372. }
  3373. //===============================================
  3374. // Check reservation date is expired
  3375. //===============================================
  3376. int isReservationExpired(unsigned char gun_index)
  3377. {
  3378. int result = NO;
  3379. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3380. {
  3381. if(isOvertNow(ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate))
  3382. result = YES;
  3383. }
  3384. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3385. {
  3386. if(isOvertNow(ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime))
  3387. result = YES;
  3388. }
  3389. return result;
  3390. }
  3391. //===============================================
  3392. // Check charging profile related date routine
  3393. //===============================================
  3394. int isProfileValid(uint8_t gun_index)
  3395. {
  3396. int result = NO;
  3397. struct tm tmFrom, tmTo;
  3398. struct timeb tbFrom, tbTo;
  3399. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3400. {
  3401. 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) &&
  3402. (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))
  3403. {
  3404. tmFrom.tm_year -= 1900;
  3405. tmFrom.tm_mon -= 1;
  3406. tbFrom.time = mktime(&tmFrom);
  3407. tmTo.tm_year -= 1900;
  3408. tmTo.tm_mon -= 1;
  3409. tbTo.time = mktime(&tmTo);
  3410. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3411. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3412. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  3413. {
  3414. result = YES;
  3415. }
  3416. }
  3417. else
  3418. {
  3419. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3420. }
  3421. }
  3422. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3423. {
  3424. 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) &&
  3425. (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))
  3426. {
  3427. tmFrom.tm_year -= 1900;
  3428. tmFrom.tm_mon -= 1;
  3429. tbFrom.time = mktime(&tmFrom);
  3430. tmTo.tm_year -= 1900;
  3431. tmTo.tm_mon -= 1;
  3432. tbTo.time = mktime(&tmTo);
  3433. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3434. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3435. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  3436. {
  3437. result = YES;
  3438. }
  3439. }
  3440. else
  3441. {
  3442. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3443. }
  3444. }
  3445. return result;
  3446. }
  3447. int getMaxScheduleStart()
  3448. {
  3449. int result = -1;
  3450. struct tm tmScheduleStart;;
  3451. struct timeb tbScheduleStart;
  3452. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3453. {
  3454. 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))
  3455. {
  3456. tmScheduleStart.tm_year -= 1900;
  3457. tmScheduleStart.tm_mon -= 1;
  3458. tbScheduleStart.time = mktime(&tmScheduleStart);
  3459. tbScheduleStart.millitm = 0;
  3460. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3461. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3462. }
  3463. else
  3464. {
  3465. DEBUG_WARN("Max schedule start date parsing error.\n");
  3466. }
  3467. }
  3468. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3469. {
  3470. 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))
  3471. {
  3472. tmScheduleStart.tm_year -= 1900;
  3473. tmScheduleStart.tm_mon -= 1;
  3474. tbScheduleStart.time = mktime(&tmScheduleStart);
  3475. tbScheduleStart.millitm = 0;
  3476. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3477. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3478. }
  3479. else
  3480. {
  3481. DEBUG_WARN("Max schedule start date parsing error.\n");
  3482. }
  3483. }
  3484. return result;
  3485. }
  3486. int getScheduleStart(int gun_index)
  3487. {
  3488. int result = -1;
  3489. struct tm tmScheduleStart;;
  3490. struct timeb tbScheduleStart;
  3491. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3492. {
  3493. 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))
  3494. {
  3495. tmScheduleStart.tm_year -= 1900;
  3496. tmScheduleStart.tm_mon -= 1;
  3497. tbScheduleStart.time = mktime(&tmScheduleStart);
  3498. tbScheduleStart.millitm = 0;
  3499. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3500. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3501. }
  3502. else
  3503. {
  3504. DEBUG_WARN("Schedule start date parsing error.\n");
  3505. }
  3506. }
  3507. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3508. {
  3509. 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))
  3510. {
  3511. tmScheduleStart.tm_year -= 1900;
  3512. tmScheduleStart.tm_mon -= 1;
  3513. tbScheduleStart.time = mktime(&tmScheduleStart);
  3514. tbScheduleStart.millitm = 0;
  3515. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3516. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3517. }
  3518. else
  3519. {
  3520. DEBUG_WARN("Schedule start date parsing error.\n");
  3521. }
  3522. }
  3523. return result;
  3524. }
  3525. int getStartSinceToday()
  3526. {
  3527. int result = -1;
  3528. time_t t;
  3529. struct tm *tmStartToday;
  3530. struct timeb tbStartToday;
  3531. t=time(NULL);
  3532. tmStartToday=localtime(&t);
  3533. tmStartToday->tm_hour = 0;
  3534. tmStartToday->tm_min = 0;
  3535. tmStartToday->tm_sec = 0;
  3536. tbStartToday.time = mktime(tmStartToday);
  3537. result = DiffTimebWithNow(tbStartToday)/1000;
  3538. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  3539. return result;
  3540. }
  3541. int getStartSinceWeek()
  3542. {
  3543. int result = -1;
  3544. time_t t;
  3545. struct tm *tmStartWeek;
  3546. struct timeb tbStartWeek;
  3547. t=time(NULL);
  3548. tmStartWeek=localtime(&t);
  3549. t-=86400*tmStartWeek->tm_wday;
  3550. tmStartWeek=localtime(&t);
  3551. tmStartWeek->tm_hour = 0;
  3552. tmStartWeek->tm_min = 0;
  3553. tmStartWeek->tm_sec = 0;
  3554. tbStartWeek.time = mktime(tmStartWeek);
  3555. result = DiffTimebWithNow(tbStartWeek)/1000;
  3556. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  3557. return result;
  3558. }
  3559. //===============================================
  3560. // Valid from local white list
  3561. //===============================================
  3562. int isValidLocalWhiteCard()
  3563. {
  3564. uint8_t result = FAIL;
  3565. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  3566. {
  3567. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  3568. {
  3569. result = PASS;
  3570. }
  3571. }
  3572. return result;
  3573. }
  3574. //==========================================
  3575. // Check routine
  3576. //==========================================
  3577. void checkTask()
  3578. {
  3579. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3580. {
  3581. if(system("pidof -s Module_4g > /dev/null") != 0)
  3582. {
  3583. DEBUG_INFO("Module_4g not running, restart it.\n");
  3584. system("/root/Module_4g &");
  3585. }
  3586. }
  3587. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3588. {
  3589. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  3590. {
  3591. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  3592. system("/root/Module_Wifi &");
  3593. }
  3594. }
  3595. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  3596. {
  3597. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  3598. system("/root/Module_EventLogging &");
  3599. }
  3600. if((strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0))
  3601. {
  3602. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3603. {
  3604. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  3605. {
  3606. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  3607. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3608. system("pkill OcppBackend");
  3609. sleep(3);
  3610. ocpp_process_start();
  3611. }
  3612. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3613. {
  3614. DEBUG_INFO("OcppBackend not running, restart it.\n");
  3615. ocpp_process_start();
  3616. }
  3617. }
  3618. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3619. {
  3620. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  3621. {
  3622. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  3623. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3624. system("pkill OcppBackend20");
  3625. sleep(3);
  3626. ocpp_process_start();
  3627. }
  3628. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3629. {
  3630. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  3631. ocpp_process_start();
  3632. }
  3633. }
  3634. }
  3635. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  3636. {
  3637. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3638. {
  3639. DEBUG_INFO("OcppBackendPH not running, restart it.\n");
  3640. system("/root/OcppBackendPH &");
  3641. }
  3642. }
  3643. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  3644. {
  3645. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  3646. system("/root/Module_AlarmDetect &");
  3647. }
  3648. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  3649. {
  3650. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  3651. system("/root/Module_InternalComm &");
  3652. }
  3653. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  3654. {
  3655. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  3656. system("/root/Module_Speaker &");
  3657. }
  3658. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  3659. {
  3660. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  3661. system ("/root/Module_ProduceUtils &");
  3662. }
  3663. if(system("pidof -s Module_LcmControl > /dev/null") != 0)
  3664. {
  3665. DEBUG_INFO("Module_LcmControl not running, restart it.\n");
  3666. system ("/root/Module_LcmControl &");
  3667. }
  3668. if((system("pidof -s Module_PowerSharing > /dev/null") != 0) &&
  3669. ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing)
  3670. {
  3671. DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
  3672. system ("/root/Module_PowerSharing &");
  3673. }
  3674. if(ShmCharger->isCcsEnable == ON)
  3675. {
  3676. if((system("pidof -s CsuComm > /dev/null") != 0))
  3677. {
  3678. DEBUG_INFO("CsuComm not running, restart it.\n");
  3679. system ("/root/CsuComm &");
  3680. }
  3681. if((system("pidof -s SeccComm > /dev/null") != 0))
  3682. {
  3683. sleep(3);
  3684. DEBUG_INFO("SeccComm not running, restart it.\n");
  3685. system ("/root/SeccComm &");
  3686. }
  3687. }
  3688. }
  3689. void checkConnectionTimeout()
  3690. {
  3691. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  3692. {
  3693. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  3694. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING);
  3695. }
  3696. else
  3697. {
  3698. ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
  3699. if((ShmCharger->timeoutSpec.Setting_Timeout_Spec) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  3700. {
  3701. ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE+10);
  3702. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  3703. }
  3704. else
  3705. {
  3706. ShmCharger->timeoutSpec.Present_Timeout_Spec = ShmCharger->timeoutSpec.Setting_Timeout_Spec;
  3707. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  3708. }
  3709. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3710. }
  3711. }
  3712. void checkReset()
  3713. {
  3714. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3715. {
  3716. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3717. {
  3718. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3719. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE))
  3720. {
  3721. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  3722. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3723. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  3724. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  3725. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3726. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3727. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  3728. {
  3729. system("sync");
  3730. sleep(10);
  3731. system("reboot -f");
  3732. sleep(10);
  3733. system("reboot -f");
  3734. }
  3735. else
  3736. {
  3737. sleep(10);
  3738. close(wtdFd);
  3739. system("/usr/bin/run_evse_restart.sh");
  3740. }
  3741. }
  3742. }
  3743. }
  3744. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3745. {
  3746. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  3747. {
  3748. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3749. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE))
  3750. {
  3751. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  3752. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  3753. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  3754. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  3755. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3756. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3757. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  3758. {
  3759. system("sync");
  3760. sleep(10);
  3761. system("reboot -f");
  3762. sleep(10);
  3763. system("reboot -f");
  3764. }
  3765. else
  3766. {
  3767. sleep(10);
  3768. close(wtdFd);
  3769. system("/usr/bin/run_evse_restart.sh");
  3770. }
  3771. }
  3772. }
  3773. }
  3774. }
  3775. void checkReservation(uint8_t gun_index)
  3776. {
  3777. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3778. {
  3779. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  3780. {
  3781. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3782. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3783. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3784. {
  3785. sleep(5);
  3786. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3787. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3788. }
  3789. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3790. }
  3791. }
  3792. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3793. {
  3794. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  3795. {
  3796. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3797. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3798. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3799. {
  3800. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  3801. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3802. }
  3803. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3804. }
  3805. }
  3806. }
  3807. void checkUnlocker(uint8_t gun_index)
  3808. {
  3809. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3810. {
  3811. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3812. {
  3813. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3814. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  3815. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  3816. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  3817. }
  3818. }
  3819. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3820. {
  3821. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3822. {
  3823. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3824. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "UnlockFailed");
  3825. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  3826. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  3827. }
  3828. }
  3829. }
  3830. void checkAvailability(uint8_t gun_index)
  3831. {
  3832. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3833. {
  3834. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3835. {
  3836. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  3837. {
  3838. DB_Update_Operactive(localDb, gun_index, true);
  3839. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3840. }
  3841. else
  3842. {
  3843. DB_Update_Operactive(localDb, gun_index, false);
  3844. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3845. }
  3846. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3847. }
  3848. }
  3849. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3850. {
  3851. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3852. {
  3853. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  3854. {
  3855. DB_Update_Operactive(localDb, gun_index, true);
  3856. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3857. }
  3858. else
  3859. {
  3860. DB_Update_Operactive(localDb, gun_index, false);
  3861. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3862. }
  3863. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3864. }
  3865. }
  3866. }
  3867. void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
  3868. {
  3869. uint16_t MaxChargingProfileChargingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3870. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3871. {
  3872. // Get profile charging profile limit
  3873. if((ocpp_get_smartcharging_profileId(gun_index) > 0))
  3874. {
  3875. // Checking profile kind
  3876. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  3877. {
  3878. // Checking limitation
  3879. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3880. {
  3881. if((getScheduleStart(gun_index) >= ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3882. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3883. )
  3884. {
  3885. 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);
  3886. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3887. }
  3888. else
  3889. break;
  3890. }
  3891. }
  3892. }
  3893. else
  3894. {
  3895. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3896. }
  3897. // Get max charging profile limit
  3898. if((ocpp_get_maxcharging_profileId() > 0))
  3899. {
  3900. // Absolute profile
  3901. if((mystrcmp((char*)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == PASS))
  3902. {
  3903. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3904. {
  3905. if((getMaxScheduleStart() >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3906. ((idx_period == 0) || (ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3907. )
  3908. {
  3909. 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);
  3910. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3911. }
  3912. else
  3913. break;
  3914. }
  3915. }
  3916. }
  3917. // Compare target current is over max current for EVSE limit
  3918. if(gun_index == 0)
  3919. {
  3920. if(ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  3921. {
  3922. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  3923. }
  3924. }
  3925. else
  3926. {
  3927. if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > MaxChargingProfileChargingCurrent)
  3928. {
  3929. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
  3930. }
  3931. }
  3932. }
  3933. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3934. {
  3935. // Get profile charging profile limit
  3936. if((ocpp_get_smartcharging_profileId(gun_index) > 0))
  3937. {
  3938. // Checking profile kind
  3939. if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  3940. {
  3941. // Checking limitation
  3942. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3943. {
  3944. if((getScheduleStart(gun_index) >= ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3945. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3946. )
  3947. {
  3948. 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);
  3949. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3950. }
  3951. else
  3952. break;
  3953. }
  3954. }
  3955. }
  3956. else
  3957. {
  3958. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3959. }
  3960. // Get max charging profile limit
  3961. if((ocpp_get_maxcharging_profileId() > 0))
  3962. {
  3963. // Absolute profile
  3964. if((mystrcmp((char*)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, "Absolute") == PASS))
  3965. {
  3966. // Checking limitation
  3967. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3968. {
  3969. if((getMaxScheduleStart() >= ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3970. ((idx_period == 0) || (ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3971. )
  3972. {
  3973. 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);
  3974. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3975. }
  3976. else
  3977. break;
  3978. }
  3979. }
  3980. }
  3981. // Compare target current is over max current for EVSE limit
  3982. if(gun_index == 0)
  3983. {
  3984. if(ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  3985. {
  3986. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  3987. }
  3988. }
  3989. else
  3990. {
  3991. if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > MaxChargingProfileChargingCurrent)
  3992. {
  3993. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
  3994. }
  3995. }
  3996. }
  3997. // Charging session target current check if OCPP disconnect and power sharing server connected
  3998. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > 0)
  3999. {
  4000. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  4001. 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];
  4002. else
  4003. {
  4004. if(ShmCharger->gun_info[gun_index].targetCurrent != 0)
  4005. DEBUG_WARN("Disconnect from power sharing server, target current set to 0.\n");
  4006. ShmCharger->gun_info[gun_index].targetCurrent = 0;
  4007. }
  4008. }
  4009. switch(system_mode)
  4010. {
  4011. case SYS_MODE_IDLE:
  4012. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4013. {
  4014. 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);
  4015. if(ShmCharger->isCcsEnable)
  4016. {
  4017. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  4018. }
  4019. else
  4020. {
  4021. 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);
  4022. 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));
  4023. }
  4024. }
  4025. else
  4026. {
  4027. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4028. if(ShmCharger->isCcsEnable)
  4029. {
  4030. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  4031. }
  4032. else
  4033. {
  4034. 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);
  4035. 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));
  4036. }
  4037. }
  4038. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4039. break;
  4040. case SYS_MODE_PREPARING:
  4041. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4042. {
  4043. 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);
  4044. 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);
  4045. 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));
  4046. }
  4047. else
  4048. {
  4049. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4050. 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);
  4051. 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));
  4052. }
  4053. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4054. break;
  4055. case SYS_MODE_CHARGING:
  4056. case SYS_MODE_TERMINATING:
  4057. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4058. {
  4059. 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);
  4060. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  4061. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4062. {
  4063. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4064. {
  4065. if(getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4066. {
  4067. 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);
  4068. 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));
  4069. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4070. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4071. }
  4072. }
  4073. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4074. {
  4075. 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);
  4076. 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);
  4077. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float)ShmCharger->gun_info[gun_index].targetCurrent;
  4078. //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);
  4079. }
  4080. }
  4081. }
  4082. else
  4083. {
  4084. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4085. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  4086. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4087. {
  4088. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4089. {
  4090. if(getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4091. {
  4092. 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);
  4093. 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));
  4094. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4095. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4096. }
  4097. }
  4098. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4099. {
  4100. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4101. 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);
  4102. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float)ShmCharger->gun_info[gun_index].targetCurrent;
  4103. //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);
  4104. }
  4105. }
  4106. }
  4107. if(previousData[gun_index].primaryMcuCp_Pwn_Duty != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  4108. {
  4109. DEBUG_INFO("==================================================\n");
  4110. DEBUG_INFO("gun-%02d primaryMcuCp_Pwn_Duty: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  4111. DEBUG_INFO("==================================================\n");
  4112. previousData[gun_index].primaryMcuCp_Pwn_Duty = ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current;
  4113. }
  4114. if(ShmCharger->gun_info[gun_index].targetCurrent != previousData[gun_index].targetCurrent)
  4115. {
  4116. DEBUG_INFO("==================================================\n");
  4117. DEBUG_INFO("gun-%02d targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4118. DEBUG_INFO("MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4119. DEBUG_INFO("==================================================\n");
  4120. previousData[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent;
  4121. }
  4122. if(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != previousData[gun_index].current_limit)
  4123. {
  4124. DEBUG_INFO("==================================================\n");
  4125. DEBUG_INFO("gun-%02d mcu current_limit: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  4126. DEBUG_INFO("==================================================\n");
  4127. previousData[gun_index].current_limit = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  4128. }
  4129. break;
  4130. }
  4131. }
  4132. void checkStopReason(uint8_t gun_index)
  4133. {
  4134. sleep(2);
  4135. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4136. {
  4137. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4138. if(!ocpp_get_starttransaction_result(gun_index))
  4139. {
  4140. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  4141. }
  4142. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  4143. {
  4144. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  4145. }
  4146. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  4147. {
  4148. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4149. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4150. }
  4151. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  4152. {
  4153. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  4154. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  4155. else
  4156. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  4157. }
  4158. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  4159. {
  4160. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  4161. if(!isMatchStartUser(gun_index))
  4162. {
  4163. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4164. }
  4165. else
  4166. {
  4167. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4168. }
  4169. DEBUG_INFO("Gun-[%d] : IdTag [ %s ].\n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  4170. }
  4171. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  4172. {
  4173. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  4174. }
  4175. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  4176. {
  4177. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  4178. }
  4179. else
  4180. {
  4181. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  4182. }
  4183. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4184. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  4185. presentChargedEnergyUpdate(gun_index);
  4186. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  4187. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  4188. }
  4189. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4190. {
  4191. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  4192. if(!ocpp_get_starttransaction_result(gun_index))
  4193. {
  4194. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  4195. }
  4196. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  4197. {
  4198. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  4199. }
  4200. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  4201. {
  4202. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  4203. }
  4204. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  4205. {
  4206. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  4207. }
  4208. else if((ShmOCPP20Data->MsMsg.bits.ResetReq) && (strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0))
  4209. {
  4210. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  4211. }
  4212. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  4213. {
  4214. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  4215. if(!isMatchStartUser(gun_index))
  4216. {
  4217. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  4218. }
  4219. else
  4220. {
  4221. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  4222. }
  4223. DEBUG_INFO("Gun-[%d] : idToken [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  4224. }
  4225. else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L1_OVER_CURRENT) ||
  4226. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L2_OVER_CURRENT) ||
  4227. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L3_OVER_CURRENT))
  4228. {
  4229. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  4230. }
  4231. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  4232. {
  4233. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  4234. }
  4235. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  4236. {
  4237. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  4238. }
  4239. else
  4240. {
  4241. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  4242. }
  4243. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4244. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  4245. presentChargedEnergyUpdate(gun_index);
  4246. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  4247. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  4248. }
  4249. // Maintain server
  4250. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  4251. {
  4252. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  4253. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].IdTag, ShmOCPP16Data->StopTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4254. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = ON;
  4255. }
  4256. }
  4257. void checkRemoteUpgradeStatus()
  4258. {
  4259. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4260. {
  4261. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  4262. {
  4263. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4264. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4265. ShmCharger->isUpdateSuccess = NO;
  4266. }
  4267. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  4268. {
  4269. DEBUG_INFO("Firmware remote upgrading...\n");
  4270. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4271. sleep(1);
  4272. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4273. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4274. int result = upgrade_check();
  4275. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  4276. if(result == PASS)
  4277. {
  4278. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4279. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4280. ShmCharger->isUpdateSuccess = YES;
  4281. DEBUG_INFO("Remote update success...\n");
  4282. }
  4283. else
  4284. {
  4285. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4286. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4287. ShmCharger->isUpdateSuccess = NO;
  4288. DEBUG_INFO("Remote update unsuccess...\n");
  4289. }
  4290. }
  4291. else
  4292. {}
  4293. }
  4294. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4295. {
  4296. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  4297. {
  4298. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4299. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4300. ShmCharger->isUpdateSuccess = NO;
  4301. }
  4302. else if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0)
  4303. {
  4304. DEBUG_INFO("Firmware remote upgrading...\n");
  4305. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4306. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4307. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4308. int result = upgrade_check();
  4309. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  4310. if(result == PASS)
  4311. {
  4312. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4313. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4314. ShmCharger->isUpdateSuccess = YES;
  4315. DEBUG_INFO("Remote update success...\n");
  4316. }
  4317. else
  4318. {
  4319. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4320. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4321. ShmCharger->isUpdateSuccess = NO;
  4322. DEBUG_INFO("Remote update unsuccess...\n");
  4323. }
  4324. }
  4325. else
  4326. {}
  4327. }
  4328. }
  4329. void checkHandshakeCountdown(uint8_t gun_index)
  4330. {
  4331. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  4332. {
  4333. case SYS_MODE_IDLE:
  4334. if(ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
  4335. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4336. else
  4337. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4338. if(!ShmCharger->isAuthrizing)
  4339. {
  4340. if(ocpp_get_isRemoteStartWait())
  4341. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4342. else
  4343. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4344. }
  4345. break;
  4346. case SYS_MODE_PREPARING:
  4347. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]));
  4348. break;
  4349. case SYS_MODE_RESERVATION:
  4350. if(ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
  4351. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4352. else
  4353. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4354. break;
  4355. }
  4356. }
  4357. void checkRfidAuthrize()
  4358. {
  4359. static uint8_t isCheckdResult = FALSE;
  4360. static uint8_t isReadable = TRUE;
  4361. uint8_t isStartByWrongReservation = NO;
  4362. if(!ShmCharger->isAuthrizing)
  4363. {
  4364. // Read RFID
  4365. if(GetCardSerialNumber() != FAIL)
  4366. {
  4367. if(isReadable == TRUE)
  4368. {
  4369. uint8_t isSnStart = FALSE;
  4370. uint8_t bufferRFID[ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId)];
  4371. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4372. {
  4373. // Big endian
  4374. switch(rfid.snType)
  4375. {
  4376. case RFID_SN_TYPE_6BYTE:
  4377. 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]);
  4378. break;
  4379. case RFID_SN_TYPE_7BYTE:
  4380. 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]);
  4381. break;
  4382. case RFID_SN_TYPE_10BYTE:
  4383. 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]);
  4384. break;
  4385. case RFID_SN_TYPE_4BYTE:
  4386. default:
  4387. sprintf((char*)bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4388. break;
  4389. }
  4390. }
  4391. else
  4392. {
  4393. // Little endian
  4394. switch(rfid.snType)
  4395. {
  4396. case RFID_SN_TYPE_6BYTE:
  4397. 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]);
  4398. break;
  4399. case RFID_SN_TYPE_7BYTE:
  4400. 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]);
  4401. break;
  4402. case RFID_SN_TYPE_10BYTE:
  4403. 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]);
  4404. break;
  4405. case RFID_SN_TYPE_4BYTE:
  4406. default:
  4407. sprintf((char*)bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4408. break;
  4409. }
  4410. }
  4411. DEBUG_INFO("Authorize request User Id : %s\n", bufferRFID);
  4412. //DEBUG_INFO("Authorize request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4413. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4414. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  4415. // Check SN already start
  4416. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4417. {
  4418. 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)))
  4419. {
  4420. DEBUG_INFO("%s running on connector-%02d.\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, gun_index);
  4421. isSnStart = TRUE;
  4422. ShmCharger->gun_info[gun_index].rfidReq = ON;
  4423. ocpp_set_auth_conf(ON);
  4424. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4425. setSpeaker(ON, SPEAKER_SHORT);
  4426. sleep(3);
  4427. break;
  4428. }
  4429. }
  4430. // Request authorize if isSnStart is false
  4431. if(!isSnStart)
  4432. {
  4433. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, bufferRFID, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4434. refreshStartTimer(&startTime[0][TMR_IDX_AUTH]);
  4435. ocpp_set_auth_conf(OFF);
  4436. ocpp_set_auth_req(ON, "ISO14443");
  4437. setLedMotion(0,LED_ACTION_AUTHED);
  4438. ShmCharger->isAuthrizing = TRUE;
  4439. ShmCharger->isGetAuthResult = FALSE;
  4440. isCheckdResult = FALSE;
  4441. }
  4442. isReadable = FALSE;
  4443. }
  4444. }
  4445. else
  4446. {
  4447. isReadable = TRUE;
  4448. }
  4449. }
  4450. else
  4451. {
  4452. // Wait authorize result
  4453. if(!ocpp_get_auth_conf() && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH))
  4454. {
  4455. // Authorization timeout process.
  4456. DEBUG_WARN("Authorize timeout !!!\n");
  4457. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4458. {
  4459. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4460. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4461. }
  4462. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4463. sleep(3);
  4464. ShmCharger->isAuthrizing = FALSE;
  4465. }
  4466. else
  4467. {
  4468. if(ocpp_get_auth_conf() ||
  4469. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  4470. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > 2))
  4471. )
  4472. {
  4473. if(ocpp_get_auth_result(NO) ||
  4474. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  4475. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  4476. {
  4477. if(!isCheckdResult)
  4478. {
  4479. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4480. ShmCharger->isGetAuthResult = TRUE;
  4481. isCheckdResult = TRUE;
  4482. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  4483. {
  4484. ocpp_set_auth_conf(ON);
  4485. }
  4486. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4487. {
  4488. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_RESERVATION) &&
  4489. (strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) != 0))
  4490. {
  4491. sleep(1);
  4492. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4493. isStartByWrongReservation = YES;
  4494. DEBUG_INFO("Not match: UserId = [%s] / Reserve IdTag = [%s] \n", ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->ReserveNow[ShmCharger->gun_selectd].IdTag);
  4495. DEBUG_INFO("Authorize fail.\n");
  4496. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4497. }
  4498. else
  4499. {
  4500. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  4501. DEBUG_INFO("Authorize pass.\n");
  4502. setSpeaker(ON, SPEAKER_SHORT);
  4503. }
  4504. }
  4505. }
  4506. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4507. checkHandshakeCountdown(gun_index);
  4508. if((getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < (ocpp_get_connection_timeout())) && (isStartByWrongReservation != YES))
  4509. {
  4510. if(GetCardSerialNumber() != FAIL)
  4511. {
  4512. if(isReadable == TRUE)
  4513. {
  4514. if(isMatchPresentUser())
  4515. {
  4516. DEBUG_INFO("Cancel present user.\n");
  4517. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4518. {
  4519. setLedMotion(gun_index,LED_ACTION_IDLE);
  4520. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4521. }
  4522. setSpeaker(ON, SPEAKER_SHORT);
  4523. ShmCharger->isAuthrizing = FALSE;
  4524. }
  4525. isReadable = FALSE;
  4526. }
  4527. }
  4528. else
  4529. {
  4530. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4531. {
  4532. if(getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < 3)
  4533. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4534. else
  4535. setLedMotion(gun_index,LED_ACTION_AUTHED);
  4536. 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))) ||
  4537. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) && (ShmCharger->gun_selectd == gun_index)) ||
  4538. (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING)) && (ShmCharger->gun_selectd == gun_index))
  4539. )
  4540. {
  4541. DEBUG_INFO("Connector-%02d action detect.\n", gun_index);
  4542. ShmCharger->isAuthrizing = FALSE;
  4543. ShmCharger->gun_info[gun_index].rfidReq = ON;
  4544. break;
  4545. }
  4546. }
  4547. isReadable = TRUE;
  4548. }
  4549. }
  4550. else
  4551. {
  4552. if(isStartByWrongReservation != YES)
  4553. DEBUG_INFO("Connector action detect timeout. \n");
  4554. else
  4555. DEBUG_INFO("User Id and reservation IdTag is not match. \n");
  4556. ShmCharger->isAuthrizing = FALSE;
  4557. ShmCharger->isGetAuthResult = TRUE;
  4558. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4559. {
  4560. if(isStartByWrongReservation != YES)
  4561. {
  4562. setLedMotion(gun_index,LED_ACTION_HANDSHAKE_FAIL);
  4563. sleep(6);
  4564. }
  4565. else
  4566. {
  4567. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4568. sleep(3);
  4569. }
  4570. setLedMotion(gun_index,LED_ACTION_IDLE);
  4571. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4572. }
  4573. }
  4574. }
  4575. else
  4576. {
  4577. DEBUG_INFO("Authorize fail.\n");
  4578. ShmCharger->isAuthrizing = FALSE;
  4579. ShmCharger->isGetAuthResult = TRUE;
  4580. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4581. {
  4582. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4583. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4584. }
  4585. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4586. sleep(3);
  4587. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4588. {
  4589. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING))
  4590. {
  4591. setLedMotion(gun_index,LED_ACTION_AUTHED);
  4592. }
  4593. else
  4594. {
  4595. setLedMotion(gun_index,LED_ACTION_IDLE);
  4596. }
  4597. }
  4598. }
  4599. }
  4600. }
  4601. }
  4602. }
  4603. //======================================================
  4604. // Main process
  4605. //======================================================
  4606. int main(void)
  4607. {
  4608. //==================================================
  4609. // Create all share memory
  4610. //==================================================
  4611. if(CreatShareMemory()==0)
  4612. {
  4613. DEBUG_ERROR("CreatShareMemory NG\n");
  4614. if(ShmStatusCodeData!=NULL)
  4615. {
  4616. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  4617. }
  4618. sleep(5);
  4619. system("reboot -f");
  4620. sleep(5);
  4621. system("reboot -f");
  4622. }
  4623. else
  4624. {
  4625. DEBUG_INFO("CreatShareMemory OK\n");
  4626. }
  4627. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4628. {
  4629. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  4630. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  4631. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  4632. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  4633. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  4634. }
  4635. //==================================================
  4636. // Main loop
  4637. //==================================================
  4638. for(;;)
  4639. {
  4640. //==============================================
  4641. // Synchronize share memory from OCPP struct
  4642. //==============================================
  4643. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  4644. //==============================================
  4645. // Period check for 10 seconds
  4646. //==============================================
  4647. if((getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10) || (getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
  4648. {
  4649. //==============================================
  4650. // Check task processing
  4651. //==============================================
  4652. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4653. checkTask();
  4654. //==============================================
  4655. // Check connection timeout specification
  4656. //==============================================
  4657. checkConnectionTimeout();
  4658. refreshStartTimer(&startTime[0][TMR_IDX_CHECK_TASK]);
  4659. }
  4660. //==============================================
  4661. // Something need run in Idle mode
  4662. //==============================================
  4663. 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)) &&
  4664. (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))
  4665. {
  4666. // Check restore factory setting request
  4667. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4668. {
  4669. // Set led to initial
  4670. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4671. {
  4672. setLedMotion(gun_index,LED_ACTION_INIT);
  4673. }
  4674. sleep(5);
  4675. system("cd /root;./Module_FactoryConfig -m");
  4676. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4677. system("sync");
  4678. sleep(5);
  4679. system("reboot -f");
  4680. sleep(5);
  4681. system("reboot -f");
  4682. }
  4683. // Check upgrade firmware request
  4684. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  4685. ocpp_get_update_firmware_req())
  4686. {
  4687. // If available memory too small, free memory cache first
  4688. if(getAvailableMemory() < (200*1024*1024))
  4689. {
  4690. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  4691. system("echo 3 > /proc/sys/vm/drop_caches");
  4692. }
  4693. ShmCharger->isUpdateSuccess = NO;
  4694. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4695. {
  4696. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4697. setChargerMode(gun_index, SYS_MODE_UPDATE);
  4698. }
  4699. }
  4700. }
  4701. //==============================================
  4702. // Check remote reset request
  4703. //==============================================
  4704. checkReset();
  4705. //==============================================
  4706. // Check RFID authorization
  4707. //==============================================
  4708. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4709. {
  4710. // The user is not allowed to use RFID when the system change to under those modes.
  4711. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM) ||
  4712. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_FAULT) ||
  4713. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_DEBUG) ||
  4714. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE) ||
  4715. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_BOOTING) ||
  4716. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_COMPLETE) ||
  4717. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_MAINTAIN) ||
  4718. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE))
  4719. {
  4720. }
  4721. else
  4722. {
  4723. checkRfidAuthrize();
  4724. }
  4725. }
  4726. //==============================================
  4727. // Check Local Power Sharing Communication
  4728. //==============================================
  4729. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > 0)
  4730. {
  4731. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  4732. {
  4733. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == ON)
  4734. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  4735. }
  4736. else
  4737. {
  4738. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == OFF)
  4739. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = ON;
  4740. }
  4741. }
  4742. //==============================================
  4743. // Connector loop
  4744. //==============================================
  4745. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4746. {
  4747. //==========================================
  4748. // Power saving logic
  4749. //==========================================
  4750. /*
  4751. * TODO:
  4752. * 1. Power saving logic
  4753. */
  4754. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == OFF))
  4755. {
  4756. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  4757. }
  4758. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM) ||
  4759. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_FAULT) ||
  4760. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE) ||
  4761. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_MAINTAIN) ||
  4762. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) ||
  4763. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING) ||
  4764. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_COMPLETE))
  4765. {
  4766. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4767. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4768. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4769. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4770. }
  4771. else
  4772. {
  4773. if((ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES) ||
  4774. (gpio_get_value(GPIO_IN_WAKEUP) == OFF) ||
  4775. (ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON) ||
  4776. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus != ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus) ||
  4777. (((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)) &&
  4778. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)) &&
  4779. (getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]) < 10)))
  4780. {
  4781. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4782. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4783. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4784. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4785. if(((gpio_get_value(GPIO_IN_WAKEUP) == OFF) || (ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON)) &&
  4786. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) &&
  4787. (getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_CHECK_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
  4788. {
  4789. ShmCharger->gun_info[gun_index].isCheckPowerConsumption = YES;
  4790. refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
  4791. }
  4792. }
  4793. else
  4794. {
  4795. refreshStartTimer(&startTime[gun_index][TMR_IDX_CHECK_POWER_CONSUMPTION]);
  4796. if((getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_LCM_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
  4797. {
  4798. ShmCharger->gun_info[gun_index].isCheckPowerConsumption = NO;
  4799. }
  4800. }
  4801. }
  4802. if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) > TIMEOUT_SPEC_POWERSAVING_LCD)
  4803. {
  4804. if(ShmCharger->isLcdOn == ON)
  4805. {
  4806. DEBUG_INFO("LCD into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
  4807. ShmCharger->isLcdOn = OFF;
  4808. }
  4809. }
  4810. else
  4811. {
  4812. if(ShmCharger->isLcdOn == OFF)
  4813. {
  4814. DEBUG_INFO("LCD exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
  4815. ShmCharger->isLcdOn = ON;
  4816. }
  4817. }
  4818. if(getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]) > TIMEOUT_SPEC_POWERSAVING_RFID)
  4819. {
  4820. if(gpio_get_value(GPIO_OUT_RST_RFID) == ON)
  4821. {
  4822. DEBUG_INFO("RFID into power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  4823. gpio_set_value(GPIO_OUT_RST_RFID, OFF);
  4824. }
  4825. }
  4826. else
  4827. {
  4828. if(gpio_get_value(GPIO_OUT_RST_RFID) == OFF)
  4829. {
  4830. DEBUG_INFO("RFID exit power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  4831. gpio_set_value(GPIO_OUT_RST_RFID, ON);
  4832. }
  4833. }
  4834. if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]) > TIMEOUT_SPEC_POWERSAVING_METER)
  4835. {
  4836. if(ShmCharger->gun_info[gun_index].isMeterOn)
  4837. {
  4838. DEBUG_INFO("Meter into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  4839. ShmCharger->gun_info[gun_index].isMeterOn = OFF;
  4840. }
  4841. }
  4842. else
  4843. {
  4844. if(!ShmCharger->gun_info[gun_index].isMeterOn)
  4845. {
  4846. DEBUG_INFO("Meter exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  4847. ShmCharger->gun_info[gun_index].isMeterOn = ON;
  4848. }
  4849. }
  4850. if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]) > TIMEOUT_SPEC_POWERSAVING_LED_STATUS)
  4851. {
  4852. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  4853. {
  4854. DEBUG_INFO("LED status into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
  4855. ShmCharger->gun_info[gun_index].isSleepOn = YES;
  4856. }
  4857. }
  4858. else
  4859. {
  4860. if(ShmCharger->gun_info[gun_index].isSleepOn == YES)
  4861. {
  4862. DEBUG_INFO("LED status exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
  4863. ShmCharger->gun_info[gun_index].isSleepOn = NO;
  4864. }
  4865. }
  4866. //==========================================
  4867. // Synchronize current rating value from MCU
  4868. //==========================================
  4869. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  4870. //==========================================
  4871. // Assign connector location index for OCPP
  4872. //==========================================
  4873. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  4874. //==========================================
  4875. // Synchronize present charging power
  4876. //==========================================
  4877. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  4878. {
  4879. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  4880. {
  4881. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  4882. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower <= 1.0)
  4883. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4884. }
  4885. else
  4886. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4887. }
  4888. else
  4889. {
  4890. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  4891. {
  4892. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000) +
  4893. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2)/1000) +
  4894. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3)/1000));
  4895. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower <= 1.0)
  4896. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4897. }
  4898. else
  4899. {
  4900. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4901. }
  4902. }
  4903. //==========================================
  4904. // Check initialization "PASS" or "FAIL"
  4905. //==========================================
  4906. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4907. {
  4908. // Alarm event check
  4909. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0) || (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode>0) || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
  4910. {
  4911. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  4912. {
  4913. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  4914. {
  4915. setChargerMode(gun_index, SYS_MODE_ALARM);
  4916. }
  4917. }
  4918. }
  4919. }
  4920. //==========================================
  4921. // Reservation request check
  4922. //==========================================
  4923. checkReservation(gun_index);
  4924. //==========================================
  4925. // Change availability check
  4926. //==========================================
  4927. checkAvailability(gun_index);
  4928. if(ShmCharger->gun_info[gun_index].isOperactive)
  4929. {
  4930. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  4931. {
  4932. setChargerMode(gun_index, SYS_MODE_IDLE);
  4933. }
  4934. }
  4935. else
  4936. {
  4937. if(isMode(gun_index, SYS_MODE_IDLE))
  4938. {
  4939. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  4940. }
  4941. }
  4942. //==========================================
  4943. // Unlock Connector signal check
  4944. //==========================================
  4945. checkUnlocker(gun_index);
  4946. //==========================================
  4947. // Clean EVCCID timeout flag
  4948. //==========================================
  4949. if((ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID && (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)) &&
  4950. (ShmCharger->isCcsEnable) &&
  4951. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A))
  4952. {
  4953. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4954. {
  4955. ShmCharger->gun_info[gun_index].isGetEvCCIDTimeout = OFF;
  4956. ShmCharger->gun_info[gun_index].isEvCCIDAuthorizeFail = NO;
  4957. }
  4958. }
  4959. //==========================================
  4960. // Check EVCCID communcation error
  4961. //==========================================
  4962. if(ShmCharger->gun_info[gun_index].isGetEvCCIDTimeout == ON)
  4963. {
  4964. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEvCommFail == OFF)
  4965. ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEvCommFail = ON;
  4966. }
  4967. else
  4968. {
  4969. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEvCommFail == ON)
  4970. ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEvCommFail = OFF;
  4971. }
  4972. //==========================================
  4973. // Clean isRemoteStartWait flag
  4974. //==========================================
  4975. if(ocpp_get_isRemoteStartWait())
  4976. {
  4977. if(ShmCharger->gun_info[gun_index].isRemoteStartWait == OFF)
  4978. {
  4979. ShmCharger->gun_info[gun_index].isRemoteStartWait = ON;
  4980. DEBUG_INFO("Remote start without connector id... \n");
  4981. }
  4982. refreshStartTimer(&startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]);
  4983. }
  4984. else
  4985. {
  4986. if((getDiffSecNow(startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]) > 5))
  4987. {
  4988. if(ShmCharger->gun_info[gun_index].isRemoteStartWait == ON)
  4989. {
  4990. ShmCharger->gun_info[gun_index].isRemoteStartWait = OFF;
  4991. DEBUG_INFO("Clean isRemoteStartWait flag... \n");
  4992. }
  4993. }
  4994. if((getDiffSecNow(startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]) > 10))
  4995. {
  4996. refreshStartTimer(&startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]);
  4997. }
  4998. }
  4999. //==========================================
  5000. // Connector process
  5001. //==========================================
  5002. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  5003. {
  5004. case SYS_MODE_BOOTING:
  5005. if(isModeChange(gun_index))
  5006. {
  5007. setLedMotion(gun_index,LED_ACTION_INIT);
  5008. //CSU Initialization & task spawn
  5009. if((Initialization(gun_index) != PASS) ||
  5010. (SpawnTask(gun_index) != PASS))
  5011. {
  5012. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  5013. }
  5014. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  5015. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  5016. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  5017. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  5018. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  5019. refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
  5020. }
  5021. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  5022. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  5023. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  5024. {
  5025. ShmCharger->gun_selectd = 0;
  5026. // Firmware version
  5027. get_firmware_version(gun_index);
  5028. // OCPP BootNotification info set
  5029. DEBUG_INFO("==========================================\n");
  5030. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  5031. DEBUG_INFO("==========================================\n");
  5032. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  5033. ocpp_boot_info_sync();
  5034. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  5035. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  5036. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  5037. DEBUG_INFO("==========================================\n");
  5038. DEBUG_INFO("========== Set Wifi information ==========\n");
  5039. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  5040. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  5041. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  5042. DEBUG_INFO("==========================================\n");
  5043. // Set max current to rating current
  5044. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  5045. // Default Ethernet / Wifi / 4G to 1:disconnected
  5046. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  5047. {
  5048. case 'E':
  5049. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5050. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  5051. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  5052. break;
  5053. case 'W':
  5054. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5055. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  5056. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  5057. break;
  5058. case 'T':
  5059. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5060. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  5061. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  5062. break;
  5063. case 'D':
  5064. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5065. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  5066. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  5067. break;
  5068. }
  5069. // Default LCM brightness to 100
  5070. ShmCharger->isLcdOn = ON;
  5071. // Defaule led status to not sleep mode
  5072. ShmCharger->gun_info[gun_index].isSleepOn = NO;
  5073. // If Web Server OPCC URL is empty kill Module_OcppBackend
  5074. if((strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0))
  5075. {
  5076. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  5077. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  5078. system ("pkill OcppBackend");
  5079. }
  5080. // If rotate switch equal zero, the system needs to change Debug mode
  5081. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  5082. setChargerMode(gun_index, SYS_MODE_DEBUG);
  5083. else
  5084. setChargerMode(gun_index, SYS_MODE_IDLE);
  5085. // The system identifies 1 phase or 3 phases depending on the model name
  5086. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = ((ShmSysConfigAndInfo->SysConfig.ModelName[2]=='Y') ||
  5087. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='D') ||
  5088. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='W')
  5089. ?3:1);
  5090. }
  5091. break;
  5092. case SYS_MODE_IDLE:
  5093. if(isModeChange(gun_index))
  5094. {
  5095. setLedMotion(gun_index,LED_ACTION_IDLE);
  5096. setRelay(gun_index, OFF);
  5097. setRequest(gun_index,OFF);
  5098. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  5099. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5100. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5101. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  5102. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  5103. ShmCharger->gun_info[gun_index].isGetEvCCID = OFF;
  5104. ocpp_set_remotestart(gun_index, OFF);
  5105. ocpp_set_remotestop(gun_index, OFF);
  5106. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  5107. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  5108. presentChargedEnergyClear(gun_index);
  5109. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  5110. ocpp_set_unlocker_req(gun_index, OFF);
  5111. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  5112. // Response StopTransactionConf
  5113. ocpp_set_stoptransaction_conf(gun_index, OFF);
  5114. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  5115. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5116. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_IDLE;
  5117. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5118. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5119. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5120. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = OFF;
  5121. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5122. //if(ShmCharger->isCcsEnable)system("pkill Module_CCS");
  5123. DB_Check_Record_Buf(localDb, gun_index);
  5124. ShmCharger->gun_info[gun_index].isEmergencyStopReport = OFF;
  5125. // Default previousData every time
  5126. previousData[gun_index].primaryMcuCp_Pwn_Duty = 0;
  5127. previousData[gun_index].targetCurrent = 0;
  5128. previousData[gun_index].current_limit = 0;
  5129. }
  5130. checkHandshakeCountdown(gun_index);
  5131. // LED status in Idle mode
  5132. if(!ShmCharger->isAuthrizing)
  5133. {
  5134. if(ocpp_get_isRemoteStartWait())
  5135. {
  5136. ShmCharger->gun_info[gun_index].isSleepOn = OFF;
  5137. setLedMotion(gun_index, LED_ACTION_AUTHED);
  5138. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  5139. //DEBUG_INFO("Remote Start Transaction without connector Id.\n");
  5140. }
  5141. else
  5142. {
  5143. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus == ON)
  5144. {
  5145. if(ShmCharger->gun_info[gun_index].rfidReq == OFF)
  5146. {
  5147. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  5148. setLedMotion(gun_index,LED_ACTION_IDLE_BACKEND_CONNECTED);
  5149. else
  5150. setLedMotion(gun_index,LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP);
  5151. }
  5152. }
  5153. else
  5154. {
  5155. if(ShmCharger->gun_info[gun_index].rfidReq == OFF)
  5156. {
  5157. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  5158. setLedMotion(gun_index,LED_ACTION_IDLE_BACKEND_DISCONNECTED);
  5159. else
  5160. setLedMotion(gun_index,LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP);
  5161. }
  5162. }
  5163. }
  5164. }
  5165. /*
  5166. * TODO:
  5167. * 1. Try to get EVCCID if plug first
  5168. */
  5169. if((ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID && (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)) &&
  5170. (ShmCharger->isCcsEnable) &&
  5171. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) &&
  5172. (ShmCharger->gun_info[gun_index].isGetEvCCIDTimeout == OFF) &&
  5173. (ShmCharger->gun_info[gun_index].isRemoteStartWait != ON) &&
  5174. (ShmCharger->gun_info[gun_index].isEvCCIDAuthorizeFail != YES))
  5175. {
  5176. switch(ShmCharger->gun_info[gun_index].ccsHandshakeState)
  5177. {
  5178. case HANDSHAKE_DUTY_5:
  5179. if(!getRequest(gun_index))
  5180. {
  5181. setRequest(gun_index, ON);
  5182. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5183. DEBUG_INFO("Try to get EVCCID, Set Request On.\n");
  5184. //Let CCS task start to negotiate
  5185. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = ON;
  5186. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_NONE;
  5187. }
  5188. // Set CCS 5% PWM duty
  5189. if(ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_5) //set 5% by SeccComm.c
  5190. {
  5191. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  5192. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
  5193. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5194. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK\n");
  5195. }
  5196. break;
  5197. case HANDSHAKE_DUTY_5_CHECK:
  5198. if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF))
  5199. {
  5200. //2 secs timeout
  5201. refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  5202. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CCS;
  5203. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_CCS\n");
  5204. }
  5205. break;
  5206. case HANDSHAKE_CCS:
  5207. if(getDiffSecNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  5208. {
  5209. ShmCharger->gun_info[gun_index].isGetEvCCIDTimeout = ON;
  5210. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5211. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5212. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  5213. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  5214. DEBUG_INFO("Get EVCCID timeout.\n");
  5215. sleep(3);
  5216. }
  5217. if((19 <= ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 253))
  5218. {
  5219. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5220. {
  5221. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%s%02X%02X%02X%02X%02X%02X", ShmOCPP16Data->ConfigurationTable.CoreProfile[EVCCID_PREFIX].ItemData,
  5222. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[0],
  5223. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[1],
  5224. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[2],
  5225. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[3],
  5226. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[4],
  5227. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[5]);
  5228. }
  5229. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5230. {
  5231. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[0],
  5232. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[1],
  5233. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[2],
  5234. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[3],
  5235. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[4],
  5236. ShmCharger->gun_info[gun_index].acCcsInfo.EVCCID[5]);
  5237. }
  5238. //sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "F5902677");
  5239. ShmCharger->gun_info[gun_index].isGetEvCCID = ON;
  5240. DEBUG_INFO("Got EVCCID: %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  5241. }
  5242. break;
  5243. default:
  5244. break;
  5245. }
  5246. }
  5247. else
  5248. {
  5249. if(getRequest(gun_index))
  5250. {
  5251. setRequest(gun_index, OFF);
  5252. }
  5253. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5;
  5254. }
  5255. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  5256. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  5257. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)) ||
  5258. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  5259. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  5260. (ocpp_get_remotestart(gun_index) == ON) ||
  5261. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON) ||
  5262. (ShmCharger->gun_info[gun_index].isGetEvCCID == ON))
  5263. {
  5264. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  5265. {
  5266. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  5267. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  5268. DEBUG_INFO("Start Method : RFID...\n");
  5269. }
  5270. else if(ocpp_get_remotestart(gun_index))
  5271. {
  5272. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  5273. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  5274. ocpp_copy_userid_from_remotestart(gun_index);
  5275. setSpeaker(ON, SPEAKER_SHORT);
  5276. DEBUG_INFO("Start Method : BACKEND...\n");
  5277. }
  5278. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  5279. {
  5280. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  5281. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Local;
  5282. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  5283. setSpeaker(ON, SPEAKER_SHORT);
  5284. DEBUG_INFO("Start Method : BLE...\n");
  5285. }
  5286. else if(ShmCharger->gun_info[gun_index].isGetEvCCID == ON)
  5287. {
  5288. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_EVCCID;
  5289. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_MacAddress;
  5290. setSpeaker(ON, SPEAKER_SHORT);
  5291. DEBUG_INFO("Start Method : EVCCID...\n");
  5292. }
  5293. else
  5294. {
  5295. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  5296. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_NoAuthorization;
  5297. //memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  5298. ocpp_get_freevend_idtag(ShmSysConfigAndInfo->SysConfig.UserId);
  5299. setSpeaker(ON, SPEAKER_SHORT);
  5300. DEBUG_INFO("Start Method : FREE...\n");
  5301. }
  5302. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5303. ocpp_set_remotestart(gun_index, OFF);
  5304. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  5305. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  5306. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5307. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  5308. ShmCharger->gun_info[gun_index].isGetEvCCID = OFF;
  5309. // Get target current
  5310. ocpp_set_profile_req(gun_index, ON);
  5311. sleep(1);
  5312. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  5313. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5314. }
  5315. else
  5316. {}
  5317. break;
  5318. case SYS_MODE_AUTHORIZING:
  5319. if(isModeChange(gun_index))
  5320. {
  5321. refreshStartTimer(&startTime[gun_index][TMR_IDX_AUTH]);
  5322. if(ocpp_isAuthorizeRemoteStart() && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND))
  5323. {
  5324. DEBUG_INFO("Remote start request authorize.\n");
  5325. ocpp_set_auth_req(ON, "ISO14443");
  5326. }
  5327. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_EVCCID))
  5328. {
  5329. DEBUG_INFO("EVCCID request authorize.\n");
  5330. ocpp_set_auth_req(ON, "MacAddress");
  5331. }
  5332. }
  5333. if(getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  5334. {
  5335. // Authorization timeout process.
  5336. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  5337. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  5338. sleep(3);
  5339. setChargerMode(gun_index, SYS_MODE_IDLE);
  5340. DEBUG_WARN("Authorize timeout !!!\n");
  5341. }
  5342. else
  5343. {
  5344. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  5345. {
  5346. case START_METHOD_BACKEND:
  5347. if(ocpp_isAuthorizeRemoteStart())
  5348. {
  5349. if(ocpp_get_auth_conf())
  5350. {
  5351. if(ocpp_get_auth_result(gun_index))
  5352. {
  5353. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5354. DEBUG_INFO("Authorize pass.\n");
  5355. setSpeaker(ON,SPEAKER_SHORT);
  5356. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  5357. #if 0
  5358. if(ShmCharger->isCcsEnable)
  5359. {
  5360. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5361. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5362. sleep(4);
  5363. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5364. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5365. //system("/root/Module_CCS &");
  5366. }
  5367. else
  5368. #endif
  5369. sleep(4);
  5370. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5371. }
  5372. else
  5373. {
  5374. DEBUG_INFO("Authorize fail.\n");
  5375. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  5376. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  5377. sleep(3);
  5378. setChargerMode(gun_index, SYS_MODE_IDLE);
  5379. }
  5380. ocpp_set_auth_conf(OFF);
  5381. }
  5382. }
  5383. else
  5384. {
  5385. if(ShmCharger->isCcsEnable)
  5386. {
  5387. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5388. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5389. //sleep(4);
  5390. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5391. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5392. //system("/root/Module_CCS &");
  5393. }
  5394. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5395. }
  5396. break;
  5397. case START_METHOD_EVCCID:
  5398. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  5399. if(ocpp_get_auth_conf() ||
  5400. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  5401. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > 2)))
  5402. {
  5403. if(ocpp_get_auth_result(gun_index) ||
  5404. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  5405. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  5406. {
  5407. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5408. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  5409. DEBUG_INFO("Authorize pass [EVCCID].\n");
  5410. setSpeaker(ON, SPEAKER_SHORT);
  5411. setLedMotion(gun_index, LED_ACTION_RFID_PASS);
  5412. sleep(1);
  5413. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5414. ShmCharger->gun_info[gun_index].isEvCCIDAuthorizeFail = NO;
  5415. }
  5416. else
  5417. {
  5418. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  5419. DEBUG_INFO("Authorize fail [EVCCID].\n");
  5420. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  5421. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  5422. sleep(3);
  5423. setChargerMode(gun_index, SYS_MODE_IDLE);
  5424. ShmCharger->gun_info[gun_index].isEvCCIDAuthorizeFail = YES;
  5425. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5426. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5427. }
  5428. ocpp_set_auth_conf(OFF);
  5429. }
  5430. break;
  5431. case START_METHOD_RFID:
  5432. case START_METHOD_BLE:
  5433. case START_METHOD_FREE:
  5434. default:
  5435. if(ShmCharger->isCcsEnable)
  5436. {
  5437. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5438. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5439. //sleep(4);
  5440. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5441. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5442. //system("/root/Module_CCS &");
  5443. }
  5444. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5445. break;
  5446. }
  5447. }
  5448. break;
  5449. case SYS_MODE_PREPARING:
  5450. if(isModeChange(gun_index))
  5451. {
  5452. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5453. setLedMotion(gun_index,LED_ACTION_AUTHED);
  5454. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5455. if(ShmCharger->isCcsEnable)
  5456. {
  5457. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod != START_METHOD_EVCCID)
  5458. {
  5459. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5;
  5460. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_DUTY_5\n");
  5461. }
  5462. }
  5463. else
  5464. {
  5465. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5466. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5467. setRequest(gun_index, ON);
  5468. }
  5469. }
  5470. // If control pilot detect Bx, skip watch dog time out.
  5471. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) ||
  5472. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  5473. {
  5474. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  5475. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5476. switch(ShmCharger->gun_info[gun_index].ccsHandshakeState)
  5477. {
  5478. case HANDSHAKE_DUTY_5:
  5479. if(!getRequest(gun_index))
  5480. {
  5481. setRequest(gun_index, ON);
  5482. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  5483. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5484. DEBUG_INFO("Set Request On.\n");
  5485. //Let CCS task start to negotiate
  5486. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = ON;
  5487. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_NONE;
  5488. }
  5489. // Set CCS 5% PWM duty
  5490. if(ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_5)//set 5% by SeccComm.c
  5491. {
  5492. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  5493. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
  5494. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5495. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK\n");
  5496. }
  5497. break;
  5498. case HANDSHAKE_DUTY_5_CHECK:
  5499. if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF))
  5500. {
  5501. //2 secs timeout
  5502. refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  5503. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CCS;
  5504. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_CCS\n");
  5505. }
  5506. break;
  5507. case HANDSHAKE_CCS:
  5508. //CCS handshake timeout
  5509. if(getDiffSecNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  5510. {
  5511. if(ShmCharger->isCcsEnable)
  5512. {
  5513. DEBUG_INFO("TIMEOUT_SPEC_BS_HLC_HANDSHAKE.\n");
  5514. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5515. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5516. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5517. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5518. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5519. break;
  5520. }
  5521. else
  5522. {
  5523. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5524. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5525. DEBUG_INFO("BS/HLC %d secs handshake timeout.\n", TIMEOUT_SPEC_BS_HLC_HANDSHAKE);
  5526. }
  5527. }
  5528. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  5529. {
  5530. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  5531. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5532. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5533. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5534. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5535. }
  5536. //CCS status check
  5537. #if 0
  5538. if((16 < ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 254))
  5539. {
  5540. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  5541. DEBUG_INFO("Enter HLC Mode charging.\n");
  5542. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  5543. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5544. }
  5545. #else
  5546. if((16 <= ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 253))
  5547. {
  5548. //chang PWM duty to BS
  5549. if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)&&(ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current!=CCS_PWM_DUTY_5))
  5550. {
  5551. 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);
  5552. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5553. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_CCS (set 5% duty)\n");
  5554. }
  5555. //ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  5556. //ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  5557. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5558. }
  5559. if((37 <= ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 49))
  5560. {
  5561. //chang PWM duty to BS
  5562. if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)&&(ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current!=CCS_PWM_DUTY_5))
  5563. {
  5564. 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);
  5565. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5566. }
  5567. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  5568. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  5569. //refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5570. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_HLC_MODE\n");
  5571. }
  5572. else if((54 < ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus <= 255))
  5573. {
  5574. DEBUG_INFO("ccsHandshakeState = CHARGING_MODE_BS, CCS terminated\n");
  5575. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5576. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5577. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5578. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5579. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5580. }
  5581. #endif
  5582. break;
  5583. case HANDSHAKE_CP_STATE_E:
  5584. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5585. {
  5586. if(ShmCharger->isCcsEnable)
  5587. {
  5588. DEBUG_INFO("Change to CP STATE E for 4 secs.\n");
  5589. //CP STATE E for 4 secs
  5590. sleep(4);
  5591. }
  5592. //restore normal CP PWM duty
  5593. // Determine max charging current to MCU
  5594. DEBUG_INFO("Determine max charging current to MCU.\n");
  5595. ocpp_set_profile_req(gun_index, ON);
  5596. sleep(1);
  5597. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  5598. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  5599. refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  5600. }
  5601. break;
  5602. case HANDSHAKE_SET_MAX_CURRENT:
  5603. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5604. {
  5605. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_BS_MODE;
  5606. DEBUG_INFO("Enter BS Mode charging.\n");
  5607. //for EV READY 30 secs didn't start charging to STATE E
  5608. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5609. }
  5610. break;
  5611. case HANDSHAKE_BS_MODE:
  5612. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5613. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  5614. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) && ((ShmCharger->gun_info[gun_index].targetCurrent != 0)))
  5615. {
  5616. setRelay(gun_index,ON);
  5617. }
  5618. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) || (ShmCharger->gun_info[gun_index].targetCurrent == 0))
  5619. {
  5620. ocpp_set_unlocker_req(gun_index, OFF);
  5621. presentChargedEnergyClear(gun_index);
  5622. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  5623. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  5624. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  5625. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  5626. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  5627. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  5628. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5629. else
  5630. {
  5631. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5632. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  5633. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  5634. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  5635. }
  5636. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5637. ocpp_copy_userid_to_starttransaction(gun_index);
  5638. ocpp_set_starttransaction_req(gun_index, ON);
  5639. refreshStartTimer(&startChargingTime[gun_index]);
  5640. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5641. }
  5642. //EV READY CHECK
  5643. /*
  5644. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_EV_READY) && (ShmCharger->gun_info[gun_index].isEvReady2StateE == OFF))
  5645. {
  5646. if(ShmCharger->gun_info[gun_index].isDoEvReadyOnce == OFF)
  5647. {
  5648. DEBUG_INFO("EV READY STATE E 4sec.\n");
  5649. ShmCharger->gun_info[gun_index].isEvReady2StateE = ON;
  5650. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_E;
  5651. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5652. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_E;
  5653. }
  5654. }
  5655. if(ShmCharger->gun_info[gun_index].isEvReady2StateE == ON)
  5656. {
  5657. switch(ShmCharger->gun_info[gun_index].evReadyState)
  5658. {
  5659. case EV_READY_STAT_E:
  5660. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5661. {
  5662. sleep(4);
  5663. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5664. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5665. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_C;
  5666. }
  5667. break;
  5668. case EV_READY_STAT_C:
  5669. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5670. {
  5671. usleep(500000);
  5672. DEBUG_INFO("EV READY Determine max charging current to MCU.\n");
  5673. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  5674. {
  5675. 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);
  5676. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  5677. {
  5678. 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);
  5679. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5680. }
  5681. }
  5682. else
  5683. {
  5684. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  5685. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  5686. {
  5687. 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);
  5688. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5689. }
  5690. }
  5691. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5692. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_SET_MAX_CURRENT;
  5693. }
  5694. break;
  5695. case EV_READY_SET_MAX_CURRENT:
  5696. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5697. {
  5698. ShmCharger->gun_info[gun_index].isEvReady2StateE = OFF;
  5699. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = ON;
  5700. }
  5701. break;
  5702. default:
  5703. break;
  5704. }
  5705. }*/
  5706. break;
  5707. case HANDSHAKE_HLC_MODE:
  5708. if(ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)//powerDelivery
  5709. {
  5710. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  5711. {
  5712. setRelay(gun_index,ON);
  5713. ShmCharger->gun_info[gun_index].isCCSStartTransation = ON;
  5714. DEBUG_INFO("HANDSHAKE_HLC_MODE:setRelay ON\n");
  5715. }
  5716. }
  5717. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) &&
  5718. (ShmCharger->gun_info[gun_index].isCCSStartTransation == ON))
  5719. {
  5720. DEBUG_INFO("HANDSHAKE_HLC_MODE:Relay ON done\n");
  5721. ocpp_set_unlocker_req(gun_index, OFF);
  5722. presentChargedEnergyClear(gun_index);
  5723. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  5724. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  5725. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  5726. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  5727. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  5728. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  5729. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5730. else
  5731. {
  5732. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5733. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  5734. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  5735. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  5736. }
  5737. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5738. ocpp_copy_userid_to_starttransaction(gun_index);
  5739. ocpp_set_starttransaction_req(gun_index, ON);
  5740. ShmCharger->gun_info[gun_index].isCCSStartTransation = OFF;
  5741. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5742. DEBUG_INFO("set SYS_MODE_CHARGING\n");
  5743. refreshStartTimer(&startChargingTime[gun_index]);
  5744. refreshStartTimer(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  5745. }
  5746. //120 sec timeout
  5747. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  5748. {
  5749. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  5750. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5)
  5751. {
  5752. DEBUG_INFO("CCS 120 secs handshake timeout, change to BS Mode...\n");
  5753. //Cancel CCS task negotiating
  5754. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5755. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5756. //ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5757. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  5758. {
  5759. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  5760. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5761. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5762. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5763. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5764. }
  5765. }
  5766. }
  5767. break;
  5768. default:
  5769. break;
  5770. }
  5771. }
  5772. else if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode)
  5773. {
  5774. DEBUG_INFO("Enter Socket-E Mode charging.\n");
  5775. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_SOCKETE;
  5776. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  5777. setRelay(gun_index,ON);
  5778. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5779. ocpp_copy_userid_to_starttransaction(gun_index);
  5780. ocpp_set_starttransaction_req(gun_index, ON);
  5781. refreshStartTimer(&startChargingTime[gun_index]);
  5782. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5783. }
  5784. else if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  5785. {
  5786. setRequest(gun_index, ON);
  5787. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5788. }
  5789. // Unplug charging gun to Idle mode
  5790. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  5791. {
  5792. DEBUG_INFO("The connector was connected to the EV before.\n");
  5793. //Cancel CCS task negotiating
  5794. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5795. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5796. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5797. }
  5798. // Use RFID card to stop handshaking
  5799. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchPresentUser(gun_index))
  5800. {
  5801. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  5802. //Cancel CCS task negotiating
  5803. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5804. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5805. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5806. }
  5807. else
  5808. {
  5809. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5810. }
  5811. checkHandshakeCountdown(gun_index);
  5812. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  5813. {
  5814. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  5815. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5))
  5816. {
  5817. DEBUG_INFO("Handshaking timeout...\n");
  5818. //Cancel CCS task negotiating
  5819. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5820. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5821. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5822. }
  5823. }
  5824. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5)
  5825. {
  5826. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5827. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5828. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  5829. setChargerMode(gun_index, SYS_MODE_IDLE);
  5830. }
  5831. break;
  5832. case SYS_MODE_CHARGING:
  5833. if(isModeChange(gun_index))
  5834. {
  5835. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5836. //ftime(&startChargingTime[gun_index]);
  5837. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  5838. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5839. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  5840. ocpp_set_auth_req(OFF);
  5841. ocpp_reset_smartcharging_profileId(gun_index);
  5842. ocpp_set_profile_req(gun_index, ON);
  5843. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5844. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5845. }
  5846. //if time up, clear CCS MSG count
  5847. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  5848. (getDiffSecNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
  5849. {
  5850. refreshStartTimer(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  5851. /*if(ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat > 0)
  5852. {
  5853. ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat = 0;
  5854. }
  5855. else
  5856. {
  5857. DEBUG_INFO("CCS could not get MSG from car.\n");
  5858. //setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5859. //setRelay(gun_index, OFF);
  5860. }*/
  5861. }
  5862. // If unplug the connector during a session, the system must stop charging and stop session.
  5863. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS) || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC))
  5864. {
  5865. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A))
  5866. {
  5867. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  5868. DEBUG_INFO("The connector was unplugged under charging mode. [CHARGING_MODE_BS / CHARGING_MODE_HLC]. \n");
  5869. }
  5870. }
  5871. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5872. {
  5873. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn != ON)
  5874. {
  5875. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  5876. DEBUG_INFO("The socket e was unplugged under charging mode. [CHARGING_MODE_SOCKETE]. \n");
  5877. }
  5878. }
  5879. else
  5880. {}
  5881. if((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES) ||
  5882. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  5883. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  5884. ocpp_get_remotestop(gun_index) ||
  5885. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  5886. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  5887. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  5888. ocpp_get_reset_req() ||
  5889. ocpp_get_unlocker_req(gun_index) ||
  5890. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  5891. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)) ||
  5892. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE)) ||
  5893. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE)) ||
  5894. ((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)))
  5895. )
  5896. {
  5897. //printf("EVChargeProgress=0x%x, PresentMsgFlowStatus=0x%x\n",ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress,ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus);
  5898. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5899. }
  5900. else
  5901. {
  5902. // Charging session info calculation
  5903. setLedMotion(gun_index,LED_ACTION_CHARGING);
  5904. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  5905. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5906. else
  5907. {
  5908. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5909. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  5910. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  5911. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  5912. }
  5913. refreshStartTimer(&endChargingTime[gun_index]);
  5914. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  5915. presentChargedEnergyUpdate(gun_index);
  5916. // Response StartTransactionConf
  5917. ocpp_set_starttransaction_conf(gun_index, OFF);
  5918. // Charging profile preparation
  5919. if((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  5920. {
  5921. if(!ocpp_get_profile_conf(gun_index))
  5922. {
  5923. ocpp_set_profile_req(gun_index, ON);
  5924. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5925. }
  5926. else
  5927. {
  5928. ocpp_set_profile_conf(gun_index, OFF);
  5929. }
  5930. }
  5931. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  5932. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  5933. // Charging session local limit condition check
  5934. if(ocpp_get_connection_status())
  5935. {
  5936. // On-line max condition check
  5937. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  5938. {
  5939. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5940. {
  5941. setRelay(gun_index, OFF);
  5942. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5943. }
  5944. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5945. {
  5946. setRelay(gun_index, OFF);
  5947. }
  5948. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5949. {
  5950. setRelay(gun_index, OFF);
  5951. }
  5952. else
  5953. {}
  5954. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5955. 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));
  5956. }
  5957. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  5958. {
  5959. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5960. {
  5961. setRelay(gun_index, OFF);
  5962. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5963. }
  5964. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5965. {
  5966. setRelay(gun_index, OFF);
  5967. }
  5968. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5969. {
  5970. setRelay(gun_index, OFF);
  5971. }
  5972. else
  5973. {}
  5974. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5975. 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));
  5976. }
  5977. else
  5978. {
  5979. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5980. {
  5981. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5982. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  5983. setRelay(gun_index, ON);
  5984. else
  5985. setRelay(gun_index, OFF);
  5986. }
  5987. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5988. {
  5989. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  5990. setRelay(gun_index, ON);
  5991. }
  5992. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5993. {
  5994. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  5995. setRelay(gun_index, ON);
  5996. else
  5997. setRelay(gun_index, OFF);
  5998. }
  5999. else
  6000. {}
  6001. }
  6002. }
  6003. else
  6004. {
  6005. // Off-line max condition check
  6006. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  6007. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  6008. {
  6009. 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))))
  6010. {
  6011. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6012. {
  6013. setRelay(gun_index, OFF);
  6014. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  6015. }
  6016. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6017. {
  6018. setRelay(gun_index, OFF);
  6019. }
  6020. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6021. {
  6022. setRelay(gun_index, OFF);
  6023. }
  6024. else
  6025. {}
  6026. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  6027. 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));
  6028. }
  6029. 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))))
  6030. {
  6031. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6032. {
  6033. setRelay(gun_index, OFF);
  6034. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  6035. }
  6036. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6037. {
  6038. setRelay(gun_index, OFF);
  6039. }
  6040. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6041. {
  6042. setRelay(gun_index, OFF);
  6043. }
  6044. else
  6045. {}
  6046. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  6047. 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));
  6048. }
  6049. else
  6050. {
  6051. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6052. {
  6053. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  6054. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  6055. setRelay(gun_index, ON);
  6056. else
  6057. setRelay(gun_index, OFF);
  6058. }
  6059. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6060. {
  6061. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  6062. setRelay(gun_index, ON);
  6063. }
  6064. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6065. {
  6066. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  6067. setRelay(gun_index, ON);
  6068. else
  6069. setRelay(gun_index, OFF);
  6070. }
  6071. else
  6072. {}
  6073. }
  6074. }
  6075. else
  6076. {
  6077. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state > 0)
  6078. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  6079. setRelay(gun_index, OFF);
  6080. }
  6081. }
  6082. // Debug information
  6083. if((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
  6084. {
  6085. /*
  6086. DEBUG_INFO("==============================================================\n");
  6087. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  6088. 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);
  6089. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  6090. DEBUG_INFO("==============================================================\n");
  6091. */
  6092. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  6093. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  6094. DB_Update_Record_Buf(localDb, gun_index);
  6095. }
  6096. }
  6097. break;
  6098. case SYS_MODE_TERMINATING:
  6099. if(isModeChange(gun_index))
  6100. {
  6101. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  6102. {
  6103. setLedMotion(gun_index, LED_ACTION_STOP);
  6104. }
  6105. else
  6106. {}
  6107. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  6108. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  6109. if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)
  6110. setRelay(gun_index, OFF);
  6111. }
  6112. refreshStartTimer(&endChargingTime[gun_index]);
  6113. //if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  6114. //{
  6115. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  6116. //}
  6117. // End authorize pass
  6118. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  6119. ((ShmCharger->gun_info[gun_index].rfidReq == ON) && ocpp_get_auth_result(YES, gun_index)) ||
  6120. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) && (ShmCharger->gun_info[gun_index].rfidReq == ON)) ||
  6121. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (ShmCharger->gun_info[gun_index].rfidReq == ON)) ||
  6122. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  6123. ocpp_get_remotestop(gun_index) ||
  6124. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  6125. ((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()) ||
  6126. ((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))) ||
  6127. ocpp_get_reset_req() ||
  6128. ocpp_get_unlocker_req(gun_index) ||
  6129. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  6130. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  6131. {
  6132. setLedMotion(gun_index, LED_ACTION_STOP);
  6133. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6134. {
  6135. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  6136. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  6137. sleep(1);
  6138. checkStopReason(gun_index);
  6139. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6140. }
  6141. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6142. {
  6143. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6144. //Cancel CCS task negotiating
  6145. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  6146. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  6147. setRelay(gun_index,OFF);
  6148. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  6149. }
  6150. else
  6151. {
  6152. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6153. }
  6154. }
  6155. else
  6156. {
  6157. // If the charger stops charging, the led should be stoped blink
  6158. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  6159. {
  6160. setLedMotion(gun_index, LED_ACTION_STOP);
  6161. }
  6162. // Charging profile preparation
  6163. if((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  6164. {
  6165. if(!ocpp_get_profile_conf(gun_index))
  6166. {
  6167. ocpp_set_profile_req(gun_index, ON);
  6168. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  6169. }
  6170. else
  6171. {
  6172. ocpp_set_profile_conf(gun_index, OFF);
  6173. }
  6174. }
  6175. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  6176. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  6177. setRelay(gun_index, OFF);
  6178. // Debug information
  6179. if((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
  6180. {
  6181. /*
  6182. DEBUG_INFO("=============================================================\n");
  6183. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  6184. 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);
  6185. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  6186. DEBUG_INFO("=============================================================\n");
  6187. */
  6188. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  6189. }
  6190. if(!ocpp_get_auth_result(YES, gun_index))
  6191. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  6192. if((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES) &&
  6193. (((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)) &&
  6194. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  6195. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  6196. !ocpp_get_remotestop(gun_index) &&
  6197. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  6198. !ocpp_get_reset_req() &&
  6199. ocpp_get_starttransaction_result(gun_index) &&
  6200. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  6201. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  6202. !(!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)))) &&
  6203. !(!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)))) &&
  6204. (((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))
  6205. )
  6206. {
  6207. sleep(1);
  6208. setChargerMode(gun_index, SYS_MODE_CHARGING);
  6209. }
  6210. }
  6211. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  6212. (ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) &&
  6213. (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  6214. {
  6215. DEBUG_INFO("Set PWM duty 100%% go to SYS_MODE_TERMINATING.\n");
  6216. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  6217. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  6218. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  6219. sleep(1);
  6220. checkStopReason(gun_index);
  6221. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6222. }
  6223. break;
  6224. case SYS_MODE_COMPLETE:
  6225. if(isModeChange(gun_index))
  6226. {
  6227. setLedMotion(gun_index, LED_ACTION_STOP);
  6228. sleep(6);
  6229. setRelay(gun_index, OFF);
  6230. setRequest(gun_index, OFF);
  6231. sleep(13);
  6232. }
  6233. if(((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)) ||
  6234. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && (!ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)) ||
  6235. ocpp_get_reset_req())
  6236. {
  6237. sleep(2);
  6238. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  6239. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  6240. ocpp_set_remotestop(gun_index, OFF);
  6241. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  6242. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  6243. DB_Insert_Record(localDb, gun_index);
  6244. setChargerMode(gun_index, SYS_MODE_IDLE);
  6245. }
  6246. break;
  6247. case SYS_MODE_ALARM:
  6248. setLedMotion(gun_index,LED_ACTION_ALARM);
  6249. setRelay(gun_index, OFF);
  6250. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS) || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC))
  6251. {
  6252. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A))
  6253. {
  6254. setRequest(gun_index,OFF);
  6255. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  6256. }
  6257. }
  6258. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6259. {
  6260. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn != ON)
  6261. {
  6262. setRequest(gun_index,OFF);
  6263. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  6264. }
  6265. }
  6266. else
  6267. {}
  6268. /*
  6269. * TODO:
  6270. * 1. Customize emergency stop and report stop transaction then return to idle mode if error recover.
  6271. * 2. Customer: Bambang
  6272. */
  6273. if(((ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'B') && (ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'B')) &&
  6274. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == ALARM_EMERGENCY_STOP))
  6275. {
  6276. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING)) &&
  6277. !ShmCharger->gun_info[gun_index].isEmergencyStopReport)
  6278. {
  6279. checkStopReason(gun_index);
  6280. ShmCharger->gun_info[gun_index].isEmergencyStopReport = ON;
  6281. }
  6282. }
  6283. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0) && !ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
  6284. {
  6285. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING)) &&
  6286. (!ShmCharger->gun_info[gun_index].isEmergencyStopReport))
  6287. {
  6288. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  6289. }
  6290. else
  6291. {
  6292. setChargerMode(gun_index, SYS_MODE_IDLE);
  6293. }
  6294. }
  6295. break;
  6296. case SYS_MODE_FAULT:
  6297. if(isModeChange(gun_index))
  6298. {}
  6299. break;
  6300. case SYS_MODE_MAINTAIN:
  6301. if(isModeChange(gun_index))
  6302. {
  6303. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  6304. }
  6305. break;
  6306. case SYS_MODE_UPDATE:
  6307. if(isModeChange(gun_index))
  6308. {
  6309. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  6310. }
  6311. //======================================
  6312. // Check local upgrade firmware request
  6313. //======================================
  6314. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  6315. {
  6316. DEBUG_INFO("Firmware local upgrading...\n");
  6317. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  6318. int value = upgrade_check();
  6319. DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
  6320. if(value == PASS)
  6321. {
  6322. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  6323. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  6324. ShmCharger->isUpdateSuccess = YES;
  6325. DEBUG_INFO("Local update success...\n");
  6326. }
  6327. else
  6328. {
  6329. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  6330. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  6331. ShmCharger->isUpdateSuccess = NO;
  6332. DEBUG_INFO("Local update unsuccess...\n");
  6333. }
  6334. }
  6335. else if(ocpp_get_update_firmware_req())
  6336. {
  6337. //======================================
  6338. // Check remote upgrade firmware request
  6339. //======================================
  6340. checkRemoteUpgradeStatus();
  6341. }
  6342. else
  6343. {
  6344. //======================================
  6345. // Upgrade complete reboot system
  6346. //======================================
  6347. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES) && !ShmCharger->isUpgradeLcmReq)
  6348. {
  6349. if(ShmCharger->isUpdateSuccess == YES)
  6350. {
  6351. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  6352. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6353. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  6354. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6355. DEBUG_WARN("Firmware upgrade success.\n");
  6356. sleep(5);
  6357. DEBUG_INFO("Firmware upgraded, reboot...\n");
  6358. system("reboot -f");
  6359. sleep(5);
  6360. system("reboot -f");
  6361. }
  6362. else
  6363. {
  6364. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  6365. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6366. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  6367. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6368. DEBUG_WARN("Firmware upgrade fail.\n");
  6369. sleep(5);
  6370. system("rm -rvf /mnt/* ");
  6371. close(wtdFd);
  6372. system("/usr/bin/run_evse_restart.sh");
  6373. }
  6374. }
  6375. }
  6376. break;
  6377. case SYS_MODE_RESERVATION:
  6378. if(isModeChange(gun_index))
  6379. {}
  6380. setLedMotion(gun_index,LED_ACTION_RESERVATION_MODE);
  6381. if(isReservationExpired(gun_index))
  6382. {
  6383. DEBUG_INFO("Reservation: Time's up...\n");
  6384. setChargerMode(gun_index, SYS_MODE_IDLE);
  6385. }
  6386. else if(ocpp_get_cancelreservation_req(gun_index))
  6387. {
  6388. DEBUG_INFO("Reservation: Cancel reservation...\n");
  6389. setChargerMode(gun_index, SYS_MODE_IDLE);
  6390. ocpp_set_cancelreservation_req(gun_index, OFF);
  6391. }
  6392. else
  6393. {
  6394. // Check is there RFID or back end request start
  6395. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  6396. ocpp_get_remotestart(gun_index))
  6397. {
  6398. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  6399. {
  6400. if(ocpp_compare_reserve_id_with_user(gun_index))
  6401. {
  6402. DEBUG_INFO("Start Method in reservation : RFID...\n");
  6403. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  6404. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  6405. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  6406. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  6407. }
  6408. else
  6409. {
  6410. DEBUG_INFO("It's not reserve user id.\n");
  6411. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  6412. }
  6413. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  6414. }
  6415. else if(ocpp_get_remotestart(gun_index))
  6416. {
  6417. if(ocpp_compare_reserve_id_with_remote_user(gun_index))
  6418. {
  6419. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  6420. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  6421. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  6422. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  6423. }
  6424. ocpp_set_remotestop(gun_index, OFF);
  6425. }
  6426. }
  6427. }
  6428. break;
  6429. case SYS_MODE_BOOKING:
  6430. if(isModeChange(gun_index))
  6431. {}
  6432. break;
  6433. case SYS_MODE_DEBUG:
  6434. if(isModeChange(gun_index))
  6435. {
  6436. setLedMotion(gun_index,LED_ACTION_DEBUG);
  6437. }
  6438. break;
  6439. }
  6440. }
  6441. // System watch dog reset
  6442. write(wtdFd, "a", 1);
  6443. usleep(50000);
  6444. }
  6445. return FAIL;
  6446. }