main.c 255 KB

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