main.c 226 KB

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