main.c 274 KB

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