main.c 247 KB

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