main.c 273 KB

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