main.c 235 KB

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