main.c 236 KB

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