main.c 227 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244
  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(ShmSysConfigAndInfo->SysInfo.AcChargingData[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(ShmSysConfigAndInfo->SysInfo.AcChargingData[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(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idxHour]*ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour]));
  1232. totalEnergy += ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idxHour];
  1233. totalPrice += ShmSysConfigAndInfo->SysInfo.AcChargingData[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. sleep(1);
  1724. DEBUG_INFO("Initial GPIO OK\n");
  1725. }
  1726. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1727. {
  1728. int fd,wrd;
  1729. unsigned char *buf;
  1730. unsigned int ChkSum,ChkSumOrg;
  1731. if((buf=malloc(MtdBlockSize))==NULL)
  1732. {
  1733. DEBUG_ERROR("malloc buffer NG,rebooting..\n");
  1734. if(ShmStatusCodeData!=NULL)
  1735. {
  1736. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1737. }
  1738. sleep(5);
  1739. system("reboot -f");
  1740. sleep(5);
  1741. system("reboot -f");
  1742. }
  1743. memset(buf, 0, MtdBlockSize);
  1744. //================================================
  1745. // Load configuration from mtdblock10
  1746. //================================================
  1747. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1748. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1749. if (fd < 0)
  1750. {
  1751. free(buf);
  1752. DEBUG_ERROR("open mtdblock10 NG,rebooting..\n");
  1753. if(ShmStatusCodeData!=NULL)
  1754. {
  1755. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1756. }
  1757. sleep(5);
  1758. system("reboot -f");
  1759. sleep(5);
  1760. system("reboot -f");
  1761. }
  1762. wrd=read(fd, buf, MtdBlockSize);
  1763. close(fd);
  1764. if(wrd<MtdBlockSize)
  1765. {
  1766. free(buf);
  1767. DEBUG_ERROR("read SysConfigData data NG,rebooting..\n");
  1768. if(ShmStatusCodeData!=NULL)
  1769. {
  1770. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1771. }
  1772. sleep(5);
  1773. system("reboot -f");
  1774. sleep(5);
  1775. system("reboot -f");
  1776. }
  1777. ChkSum=0;
  1778. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1779. {
  1780. ChkSum+=buf[wrd];
  1781. }
  1782. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1783. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1784. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1785. //================================================
  1786. // Load configuration from mtdblock11
  1787. //================================================
  1788. if(ChkSum!=ChkSumOrg)
  1789. {
  1790. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  1791. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1792. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1793. if (fd < 0)
  1794. {
  1795. free(buf);
  1796. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\n");
  1797. if(ShmStatusCodeData!=NULL)
  1798. {
  1799. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1800. }
  1801. sleep(5);
  1802. system("reboot -f");
  1803. sleep(5);
  1804. system("reboot -f");
  1805. }
  1806. memset(buf, 0, MtdBlockSize);
  1807. wrd=read(fd, buf,MtdBlockSize);
  1808. close(fd);
  1809. if(wrd<MtdBlockSize)
  1810. {
  1811. free(buf);
  1812. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
  1813. if(ShmStatusCodeData!=NULL)
  1814. {
  1815. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1816. }
  1817. sleep(5);
  1818. system("reboot -f");
  1819. sleep(5);
  1820. system("reboot -f");
  1821. }
  1822. ChkSum=0;
  1823. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1824. {
  1825. ChkSum+=buf[wrd];
  1826. }
  1827. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1828. //================================================
  1829. // Load configuration from mtdblock12 (Factory default)
  1830. //================================================
  1831. if(ChkSum!=ChkSumOrg)
  1832. {
  1833. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
  1834. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1835. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1836. if (fd < 0)
  1837. {
  1838. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
  1839. free(buf);
  1840. if(ShmStatusCodeData!=NULL)
  1841. {
  1842. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1843. }
  1844. sleep(5);
  1845. system("reboot -f");
  1846. sleep(5);
  1847. system("reboot -f");
  1848. }
  1849. memset(buf, 0, MtdBlockSize);
  1850. wrd=read(fd, buf,MtdBlockSize);
  1851. close(fd);
  1852. if(wrd<MtdBlockSize)
  1853. {
  1854. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\n");
  1855. free(buf);
  1856. if(ShmStatusCodeData!=NULL)
  1857. {
  1858. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1859. }
  1860. sleep(5);
  1861. system("reboot -f");
  1862. sleep(5);
  1863. system("reboot -f");
  1864. }
  1865. ChkSum=0;
  1866. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1867. {
  1868. ChkSum+=buf[wrd];
  1869. }
  1870. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1871. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1872. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1873. if(ChkSum!=ChkSumOrg)
  1874. {
  1875. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
  1876. free(buf);
  1877. system("cd /root;./Module_FactoryConfig -m");
  1878. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1879. system("sync");
  1880. sleep(5);
  1881. system("reboot -f");
  1882. sleep(5);
  1883. system("reboot -f");
  1884. return FAIL;
  1885. }
  1886. }
  1887. }
  1888. //load OK
  1889. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1890. free(buf);
  1891. system("rm -f /mnt/EvseConfig.bin");
  1892. // SysConfig in flash is empty (0xffffffff)
  1893. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1894. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1895. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1896. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1897. {
  1898. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1899. {
  1900. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1901. }
  1902. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1903. {
  1904. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1905. }
  1906. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1907. {
  1908. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1909. }
  1910. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1911. {
  1912. DEBUG_INFO("Ethernet dhcp config is null.\n");
  1913. }
  1914. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
  1915. {
  1916. DEBUG_INFO("Model name over length.\n");
  1917. }
  1918. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
  1919. {
  1920. DEBUG_INFO("Model serial number over length.\n");
  1921. }
  1922. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
  1923. {
  1924. DEBUG_INFO("SystemId over length.\n");
  1925. }
  1926. system("cd /root;./Module_FactoryConfig -m");
  1927. sleep(3);
  1928. system("/usr/bin/run_evse_restart.sh");
  1929. }
  1930. DEBUG_INFO("Load SysConfigData OK\n");
  1931. ShmCharger->isCcsEnable = OFF;
  1932. for(uint8_t idx=0;idx<3;idx++)
  1933. {
  1934. if(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == '7')
  1935. ShmCharger->isCcsEnable = ON;
  1936. }
  1937. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = ((ShmSysConfigAndInfo->SysConfig.ModelName[2]=='Y') ||
  1938. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='D') ||
  1939. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='W')
  1940. ?3:1);
  1941. RatedCurrentParsing((char*)ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo);
  1942. if(modelnameInfo.GetGunCount == 0)
  1943. {
  1944. modelnameInfo.GetGunCount = 1;
  1945. DEBUG_WARN("Model name parsing error, force assign gun count to 1.\n");
  1946. }
  1947. DEBUG_INFO("Model name parsing gun count: %d\n", modelnameInfo.GetGunCount);
  1948. return PASS;
  1949. }
  1950. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1951. {
  1952. int result = PASS;
  1953. int fd,wrd;
  1954. unsigned int i,Chk;
  1955. unsigned char *ptr, *BufTmp;
  1956. Chk=0;
  1957. ptr=(unsigned char *)UsrData;
  1958. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  1959. {
  1960. memset(BufTmp,0,MtdBlockSize);
  1961. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  1962. for(i=0;i<MtdBlockSize-4;i++)
  1963. Chk+=*(BufTmp+i);
  1964. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  1965. // Output configuration to file.
  1966. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  1967. if (fd < 0)
  1968. {
  1969. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  1970. free(BufTmp);
  1971. return 0;
  1972. }
  1973. wrd=write(fd, BufTmp, MtdBlockSize);
  1974. close(fd);
  1975. if(wrd<MtdBlockSize)
  1976. {
  1977. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  1978. free(BufTmp);
  1979. return 0;
  1980. }
  1981. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  1982. DEBUG_INFO("Erase /dev/mtd10.\n");
  1983. runShellCmd("flash_erase /dev/mtd10 0 0");
  1984. DEBUG_INFO("Write /dev/mtd10.\n");
  1985. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  1986. DEBUG_INFO("Erase /dev/mtd11.\n");
  1987. runShellCmd("flash_erase /dev/mtd11 0 0");
  1988. DEBUG_INFO("Write /dev/mtd11.\n");
  1989. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  1990. system("rm -f /mnt/EvseConfig.bin");
  1991. DEBUG_INFO("EvseConfig write to flash OK\n");
  1992. }
  1993. else
  1994. {
  1995. DEBUG_ERROR("alloc BlockSize NG\r\n");
  1996. result = FAIL;
  1997. }
  1998. if(BufTmp!=NULL)
  1999. free(BufTmp);
  2000. return result;
  2001. }
  2002. void InitEthernet()
  2003. {
  2004. pid_t pid;
  2005. uint8_t cnt_pingDNS_Fail;
  2006. char tmpbuf[256];
  2007. unsigned int natInterface = 0;
  2008. //Init Eth0 for internet
  2009. memset(tmpbuf,0,256);
  2010. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  2011. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2012. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2013. DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2014. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2015. system(tmpbuf);
  2016. memset(tmpbuf,0,256);
  2017. sprintf(tmpbuf,"route add default gw %s eth0 &",
  2018. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2019. system(tmpbuf);
  2020. system("ifconfig lo up &");
  2021. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  2022. //system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
  2023. //Run DHCP client if enabled
  2024. system("killall udhcpc");
  2025. system("rm -rf /etc/resolv.conf");
  2026. system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
  2027. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  2028. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  2029. {
  2030. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  2031. system(tmpbuf);
  2032. }
  2033. // Upgrade system id to /etc/hostname
  2034. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  2035. system(tmpbuf);
  2036. // Ethernet MAC address
  2037. getEth0MacAddress();
  2038. //check internet status
  2039. pid = fork();
  2040. if(pid == 0)
  2041. {
  2042. for(;;)
  2043. {
  2044. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch != ROTARY_SWITCH_E_DISPENSER)
  2045. {
  2046. if(isRouteFail())
  2047. {
  2048. //DEBUG_ERROR("eth0 not in route, restart eth0.\n");
  2049. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  2050. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  2051. {
  2052. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2053. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  2054. system(tmpbuf);
  2055. }
  2056. else
  2057. {
  2058. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2059. memset(tmpbuf,0,256);
  2060. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  2061. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2062. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2063. system(tmpbuf);
  2064. memset(tmpbuf,0,256);
  2065. sprintf(tmpbuf,"route add default gw %s eth0 &",
  2066. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2067. system(tmpbuf);
  2068. }
  2069. }
  2070. if(isReachableInternet() == PASS)
  2071. {
  2072. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  2073. cnt_pingDNS_Fail = 0;
  2074. }
  2075. else
  2076. {
  2077. if(cnt_pingDNS_Fail >= 3)
  2078. {
  2079. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  2080. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  2081. {
  2082. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2083. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  2084. system(tmpbuf);
  2085. }
  2086. else
  2087. {
  2088. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2089. memset(tmpbuf,0,256);
  2090. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  2091. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2092. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2093. system(tmpbuf);
  2094. memset(tmpbuf,0,256);
  2095. sprintf(tmpbuf,"route add default gw %s eth0 &",
  2096. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2097. system(tmpbuf);
  2098. }
  2099. cnt_pingDNS_Fail = 0;
  2100. }
  2101. else
  2102. {
  2103. cnt_pingDNS_Fail++;
  2104. }
  2105. }
  2106. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
  2107. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  2108. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi))
  2109. {
  2110. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  2111. }
  2112. else
  2113. {
  2114. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  2115. }
  2116. //============================================================
  2117. // Priority for internet 0 : First / 1 : Second / 2: Third
  2118. //============================================================
  2119. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  2120. {
  2121. system("/sbin/ifmetric eth0 0");
  2122. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2123. {
  2124. system("/sbin/ifmetric mlan0 1");
  2125. }
  2126. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2127. {
  2128. system("/sbin/ifmetric ppp0 2");
  2129. }
  2130. if(isKernelSupportNAT() == YES)
  2131. {
  2132. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2133. {
  2134. if(natInterface != 1)
  2135. {
  2136. system("/sbin/iptables -t nat -F");
  2137. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o eth0 -j MASQUERADE");
  2138. natInterface = 1;
  2139. }
  2140. }
  2141. else
  2142. {
  2143. system("/sbin/iptables -t nat -F");
  2144. }
  2145. }
  2146. }
  2147. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  2148. {
  2149. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2150. {
  2151. system("/sbin/ifmetric eth0 1");
  2152. system("/sbin/ifmetric mlan0 0");
  2153. }
  2154. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2155. {
  2156. system("/sbin/ifmetric ppp0 2");
  2157. }
  2158. }
  2159. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  2160. {
  2161. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2162. {
  2163. system("/sbin/ifmetric mlan0 2");
  2164. }
  2165. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2166. {
  2167. system("/sbin/ifmetric eth0 1");
  2168. system("/sbin/ifmetric ppp0 0");
  2169. }
  2170. if(isKernelSupportNAT() == YES)
  2171. {
  2172. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2173. {
  2174. if(natInterface != 2)
  2175. {
  2176. system("/sbin/iptables -t nat -F");
  2177. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o ppp0 -j MASQUERADE");
  2178. natInterface = 2;
  2179. }
  2180. }
  2181. else
  2182. {
  2183. system("/sbin/iptables -t nat -F");
  2184. }
  2185. }
  2186. }
  2187. else
  2188. {
  2189. if(isKernelSupportNAT() == YES)
  2190. {
  2191. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2192. {
  2193. system("/sbin/iptables -t nat -F");
  2194. natInterface = 0;
  2195. }
  2196. }
  2197. }
  2198. }
  2199. else
  2200. {
  2201. if(ShmDispenser->gun_info.mcuFlag.isReadFwVerPass ||
  2202. ShmDispenser->gun_info.mcuFlag.isSetModelNamePass ||
  2203. ShmDispenser->gun_info.mcuFlag.isSetSerialNumberPass)
  2204. {
  2205. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2206. system("/sbin/ifconfig eth0:1 192.168.201.202 netmask 255.255.255.248 up &");
  2207. }
  2208. }
  2209. sleep(5);
  2210. }
  2211. }
  2212. DEBUG_INFO("Initial Ethernet OK\n");
  2213. }
  2214. int SpawnTask()
  2215. {
  2216. system ("pkill Module_");
  2217. system ("pkill OcppBackend");
  2218. /*
  2219. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2220. {
  2221. system("/root/Module_4g &");
  2222. }
  2223. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2224. {
  2225. system("/root/Module_Wifi &");
  2226. }*/
  2227. system("/root/Module_EventLogging &");
  2228. ocpp_process_start();
  2229. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  2230. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  2231. //system ("/root/Module_AlarmDetect &");
  2232. system ("/root/Module_InternalComm &");
  2233. //system ("/root/Module_Speaker &");
  2234. system ("/root/Module_ProduceUtils &");
  2235. //system ("/root/Module_LcmControl &");
  2236. system ("/root/Module_Cabinet &");
  2237. system ("/root/Module_Dispenser &");
  2238. return PASS;
  2239. }
  2240. int InitQca7000()
  2241. {
  2242. int result = PASS;
  2243. system("/sbin/rmmod qcaspi");
  2244. if(isKernelSupportNAT() == YES)
  2245. system("/sbin/insmod /lib/qcaspi_nat.ko");
  2246. else
  2247. system("/sbin/insmod /lib/qcaspi.ko");
  2248. sleep(2);
  2249. system("/sbin/ifconfig eth1 192.168.253.11 netmask 255.255.255.0 up");
  2250. sleep(1);
  2251. return result;
  2252. }
  2253. int Initialization(uint8_t gun_index)
  2254. {
  2255. int result = PASS;
  2256. InitGPIO();
  2257. InitEthernet();
  2258. if(InitQca7000() != PASS)
  2259. {
  2260. DEBUG_ERROR("QCA7000 initial fail.\n");
  2261. result = FAIL;
  2262. }
  2263. if(DB_Open(localDb) != PASS)
  2264. {
  2265. DEBUG_ERROR("Local database initial fail.\n");
  2266. result = FAIL;
  2267. }
  2268. if((rfidFd = InitRfidPort()) == FAIL)
  2269. {
  2270. DEBUG_ERROR("RFID port initial fail.\n");
  2271. result = FAIL;
  2272. }
  2273. if((wtdFd = InitWatchDog()) == FAIL)
  2274. {
  2275. DEBUG_ERROR("Watchdog initial fail.\n");
  2276. result = FAIL;
  2277. }
  2278. if(result == PASS)
  2279. DEBUG_INFO("Initialization OK.\n");
  2280. else
  2281. DEBUG_INFO("Initialization Fail.\n");
  2282. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2283. return result;
  2284. }
  2285. //=====================================================
  2286. // Common routine
  2287. //=====================================================
  2288. char* getSystemModeName(unsigned char mode)
  2289. {
  2290. char* result;
  2291. switch(mode)
  2292. {
  2293. case SYS_MODE_BOOTING:
  2294. result = "booting";
  2295. break;
  2296. case SYS_MODE_IDLE:
  2297. result = "idle";
  2298. break;
  2299. case SYS_MODE_AUTHORIZING:
  2300. result = "authorizing";
  2301. break;
  2302. case SYS_MODE_PREPARING:
  2303. result = "preparing";
  2304. break;
  2305. case SYS_MODE_CHARGING:
  2306. result = "charging";
  2307. break;
  2308. case SYS_MODE_TERMINATING:
  2309. result = "terminating";
  2310. break;
  2311. case SYS_MODE_COMPLETE:
  2312. result = "complete";
  2313. break;
  2314. case SYS_MODE_ALARM:
  2315. result = "alarm";
  2316. break;
  2317. case SYS_MODE_FAULT:
  2318. result = "fault";
  2319. break;
  2320. case SYS_MODE_MAINTAIN:
  2321. result = "maintain";
  2322. break;
  2323. case SYS_MODE_RESERVATION:
  2324. result = "reservation";
  2325. break;
  2326. case SYS_MODE_BOOKING:
  2327. result = "booking";
  2328. break;
  2329. case SYS_MODE_DEBUG:
  2330. result = "debug";
  2331. break;
  2332. case SYS_MODE_UPDATE:
  2333. result = "upgrade";
  2334. break;
  2335. default:
  2336. result = "unknown";
  2337. break;
  2338. }
  2339. return result;
  2340. }
  2341. void setChargerMode(unsigned char gun_index, unsigned char mode)
  2342. {
  2343. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  2344. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  2345. 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));
  2346. }
  2347. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  2348. {
  2349. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  2350. }
  2351. unsigned char isModeChange(unsigned char gun_index)
  2352. {
  2353. unsigned char result = NO;
  2354. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  2355. {
  2356. result = YES;
  2357. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  2358. }
  2359. return result;
  2360. }
  2361. int isGpioInitialized(unsigned int gpio)
  2362. {
  2363. int result = FAIL;
  2364. char gpioPath[64];
  2365. sprintf(gpioPath, "/sys/class/gpio/gpio%d/value", gpio);
  2366. if((access(gpioPath, F_OK)) != -1)
  2367. {
  2368. result = PASS;
  2369. }
  2370. return result;
  2371. }
  2372. void gpio_set_value(unsigned int gpio, unsigned int value)
  2373. {
  2374. int fd;
  2375. char buf[256];
  2376. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2377. fd = open(buf, O_WRONLY);
  2378. if (fd < 0)
  2379. {
  2380. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  2381. return;
  2382. }
  2383. if (value)
  2384. write(fd, "1", 2);
  2385. else
  2386. write(fd, "0", 2);
  2387. close(fd);
  2388. }
  2389. int gpio_get_value(unsigned int gpio)
  2390. {
  2391. int fd;
  2392. char buf[256];
  2393. char ch;
  2394. int8_t result = FAIL;
  2395. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2396. fd = open(buf, O_RDONLY);
  2397. if (fd < 0)
  2398. {
  2399. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  2400. return result;
  2401. }
  2402. read(fd, &ch, 1);
  2403. if (ch != '0')
  2404. result = 1;
  2405. else
  2406. result = 0;
  2407. close(fd);
  2408. return result;
  2409. }
  2410. int presentChargedEnergyClear(unsigned char gun_index)
  2411. {
  2412. int result = FAIL;
  2413. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2414. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod)*sizeof(float));
  2415. result = PASS;
  2416. return result;
  2417. }
  2418. float presentChargedEnergyTotal(unsigned char gun_index)
  2419. {
  2420. float result = 0.0f;
  2421. for(int idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod);idx++)
  2422. {
  2423. result += ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idx];
  2424. }
  2425. return result;
  2426. }
  2427. int presentChargedEnergyUpdate(unsigned char gun_index)
  2428. {
  2429. int result = FAIL;
  2430. time_t CurrentTime;
  2431. struct tm *tm;
  2432. CurrentTime = time(NULL);
  2433. tm=localtime(&CurrentTime);
  2434. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  2435. {
  2436. // Resolution: 0.0001 kwh
  2437. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[tm->tm_hour] += (((float)(ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption - ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start))/10000.0) - presentChargedEnergyTotal(gun_index);
  2438. }
  2439. else
  2440. {
  2441. // Resolution: 0.0001 kwh
  2442. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[tm->tm_hour] += ((((float)(ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start))/10000.0) +
  2443. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start))/10000.0) +
  2444. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start))/10000.0))
  2445. - presentChargedEnergyTotal(gun_index);
  2446. }
  2447. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = presentChargedEnergyTotal(gun_index);
  2448. return result;
  2449. }
  2450. //===============================================
  2451. // Get firmware version
  2452. //===============================================
  2453. void get_firmware_version(unsigned char gun_index)
  2454. {
  2455. FILE *fp;
  2456. char cmd[512];
  2457. char buf[512];
  2458. if(gun_index == 0)
  2459. {
  2460. // Get CSU hardware version
  2461. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  2462. // Get CSU boot loader version
  2463. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  2464. // Get CSU kernel version
  2465. sprintf(cmd, "/bin/uname -r");
  2466. fp = popen(cmd, "r");
  2467. if(fp == NULL)
  2468. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  2469. else
  2470. {
  2471. while(fgets(buf, sizeof(buf), fp) != NULL)
  2472. {
  2473. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  2474. }
  2475. }
  2476. // Get MCU firmware version
  2477. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2478. // Get CSU root file system version
  2479. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.01.00.0000.00");
  2480. // Get AC connector type from model name
  2481. for(uint8_t idx=0;idx<3;idx++)
  2482. {
  2483. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  2484. {
  2485. case '1':
  2486. // J1772 Plug
  2487. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  2488. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  2489. break;
  2490. case '2':
  2491. // J1772 Socket
  2492. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  2493. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  2494. break;
  2495. case '3':
  2496. // CE Plug
  2497. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  2498. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  2499. break;
  2500. case '4':
  2501. // CE Socket
  2502. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  2503. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  2504. break;
  2505. case '5':
  2506. // GB Plug
  2507. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  2508. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  2509. break;
  2510. case '6':
  2511. // GB Socket
  2512. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  2513. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  2514. break;
  2515. }
  2516. }
  2517. // Get network option from model name
  2518. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  2519. {
  2520. case 'B':
  2521. case 'U':
  2522. //Blue tooth
  2523. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  2524. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  2525. break;
  2526. case 'W':
  2527. // WIFI
  2528. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  2529. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  2530. break;
  2531. case 'T':
  2532. // 3G/4G
  2533. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  2534. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  2535. break;
  2536. case 'D':
  2537. // WIFI + 4G
  2538. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  2539. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  2540. break;
  2541. default:
  2542. // LAN
  2543. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  2544. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  2545. break;
  2546. }
  2547. // Get rating power from model name
  2548. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2549. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2550. // Get vender code from model name
  2551. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2552. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2553. DEBUG_INFO("========================================\n");
  2554. DEBUG_INFO("------------- Cabinet info -------------\n");
  2555. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2556. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  2557. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  2558. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  2559. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2560. DEBUG_INFO("========================================\n");
  2561. }
  2562. //===========================================
  2563. // Put info to dispenser share memory
  2564. //===========================================
  2565. // Get CSU boot loader version
  2566. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  2567. // Get CSU kernel version
  2568. sprintf(cmd, "/bin/uname -r");
  2569. fp = popen(cmd, "r");
  2570. if(fp == NULL)
  2571. sprintf((char*)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuKernelFwRev, "Unknown version");
  2572. else
  2573. {
  2574. while(fgets(buf, sizeof(buf), fp) != NULL)
  2575. {
  2576. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuKernelFwRev, buf, strlen(buf)-1);
  2577. }
  2578. }
  2579. // Get MCU firmware version
  2580. strcpy((char*)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2581. // Get CSU root file system version
  2582. sprintf((char*)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev, "D0.01.00.0000.00");
  2583. // Get AC connector type from model name
  2584. for(uint8_t idx=0;idx<3;idx++)
  2585. {
  2586. switch(ShmDispenser->ConfigData.ModelName[7+idx])
  2587. {
  2588. case '1':
  2589. // J1772 Plug
  2590. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '4';
  2591. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '4';
  2592. break;
  2593. case '2':
  2594. // J1772 Socket
  2595. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '1';
  2596. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '1';
  2597. break;
  2598. case '3':
  2599. // CE Plug
  2600. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '5';
  2601. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '5';
  2602. break;
  2603. case '4':
  2604. // CE Socket
  2605. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '2';
  2606. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '2';
  2607. break;
  2608. case '5':
  2609. // GB Plug
  2610. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '6';
  2611. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '6';
  2612. break;
  2613. case '6':
  2614. // GB Socket
  2615. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[6] = '3';
  2616. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[6] = '3';
  2617. break;
  2618. }
  2619. }
  2620. // Get network option from model name
  2621. switch(ShmDispenser->ConfigData.ModelName[10])
  2622. {
  2623. case 'B':
  2624. case 'U':
  2625. //Blue tooth
  2626. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[9] = '3';
  2627. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[9] = '3';
  2628. break;
  2629. case 'W':
  2630. // WIFI
  2631. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[9] = '1';
  2632. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[9] = '1';
  2633. break;
  2634. case 'T':
  2635. // 3G/4G
  2636. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[9] = '2';
  2637. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[9] = '2';
  2638. break;
  2639. case 'D':
  2640. // WIFI + 4G
  2641. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[9] = '5';
  2642. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[9] = '5';
  2643. break;
  2644. default:
  2645. // LAN
  2646. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[9] = '0';
  2647. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[9] = '0';
  2648. break;
  2649. }
  2650. // Get rating power from model name
  2651. memcpy(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[10], &ShmDispenser->ConfigData.ModelName[4], 0x03);
  2652. memcpy(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[10], &ShmDispenser->ConfigData.ModelName[4], 0x03);
  2653. // Get vender code from model name
  2654. memcpy(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev[14], &ShmDispenser->ConfigData.ModelName[12], 0x02);
  2655. memcpy(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev[14], &ShmDispenser->ConfigData.ModelName[12], 0x02);
  2656. DEBUG_INFO("========================================\n");
  2657. DEBUG_INFO("----------- Dispenser-%01d info -----------\n", (gun_index+1));
  2658. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuBootLoadFwRev);
  2659. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuKernelFwRev);
  2660. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuRootFsFwRev);
  2661. DEBUG_INFO("CSU MCU firmware version: %s\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[gun_index].CsuPrimFwRev);
  2662. DEBUG_INFO("========================================\n");
  2663. }
  2664. //===============================================
  2665. // Upgrade firmware
  2666. //===============================================
  2667. int upgrade_check()
  2668. {
  2669. int result = PASS;
  2670. int fd;
  2671. DIR *dir;
  2672. struct dirent *file;
  2673. char cmd[512];
  2674. long int MaxLen=48*1024*1024, ImageLen=0, wrd;
  2675. if ((dir = opendir ("/mnt")) != NULL)
  2676. {
  2677. refreshStartTimer(&startTime[0][TMR_IDX_UPGRADE]);
  2678. /* print all the files and directories within directory */
  2679. while ((file = readdir (dir)) != NULL)
  2680. {
  2681. if((strlen(file->d_name)>2))
  2682. {
  2683. // Wait for MCU upgrade finish.
  2684. while(ShmCharger->gun_info[0].mcuFlag.isMcuUpgradeReq || (modelnameInfo.GetGunCount>1?ShmCharger->gun_info[1].mcuFlag.isMcuUpgradeReq:FALSE))sleep(1);
  2685. // Wait for LCM upgrade finish.
  2686. while(ShmCharger->isUpgradeLcmReq)sleep(1);
  2687. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  2688. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  2689. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  2690. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  2691. {
  2692. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2693. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2694. ImageLen = read(fd, ptr, MaxLen);
  2695. close(fd);
  2696. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  2697. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  2698. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  2699. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  2700. if((ShmCharger->fwUpgradeInfo.modelName[0] == ShmSysConfigAndInfo->SysConfig.ModelName[0]) &&
  2701. (ShmCharger->fwUpgradeInfo.modelName[1] == ShmSysConfigAndInfo->SysConfig.ModelName[1]) &&
  2702. (ShmCharger->fwUpgradeInfo.modelName[7] == ShmSysConfigAndInfo->SysConfig.ModelName[7]) &&
  2703. (ShmCharger->fwUpgradeInfo.modelName[8] == ShmSysConfigAndInfo->SysConfig.ModelName[8]) &&
  2704. (ShmCharger->fwUpgradeInfo.modelName[9] == ShmSysConfigAndInfo->SysConfig.ModelName[9]) &&
  2705. (ShmCharger->fwUpgradeInfo.modelName[11] == ShmSysConfigAndInfo->SysConfig.ModelName[11]) &&
  2706. (ShmCharger->fwUpgradeInfo.modelName[12] == ShmSysConfigAndInfo->SysConfig.ModelName[12]) &&
  2707. (ShmCharger->fwUpgradeInfo.modelName[13] == ShmSysConfigAndInfo->SysConfig.ModelName[13]) &&
  2708. (ShmCharger->fwUpgradeInfo.fwType>0))
  2709. {
  2710. switch(ShmCharger->fwUpgradeInfo.fwType)
  2711. {
  2712. case CSU_MLO:
  2713. case CSU_BOOTLOADER:
  2714. case CSU_KERNEL_CONFIGURATION:
  2715. case CSU_KERNEL_IMAGE:
  2716. case CSU_ROOT_FILE_SYSTEM:
  2717. case CSU_USER_CONFIGURATION:
  2718. case CSU_PRIMARY_CONTROLLER:
  2719. case AC_WALLMOUNT_CONTROLLER:
  2720. // Put image file
  2721. for(uint8_t gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  2722. {
  2723. char cmd[768];
  2724. if(gun_index>0)
  2725. {
  2726. sprintf(cmd, "ftpput -u root -p y42j/4cj84 192.168.201.%d /mnt/%s %s", (201+gun_index), file->d_name, file->d_name);
  2727. if(system(cmd) != 0)
  2728. {
  2729. DEBUG_ERROR("FTP put %s to dispenser-%d fail\n", file->d_name, gun_index);
  2730. result = FAIL;
  2731. }
  2732. else
  2733. {
  2734. DEBUG_INFO("FTP put %s to dispenser-%d finish\n", file->d_name, gun_index);
  2735. }
  2736. }
  2737. }
  2738. break;
  2739. case LCM:
  2740. fd = open("/mnt/lcm.zip", O_RDWR | O_CREAT | O_EXCL);
  2741. if (fd < 0)
  2742. {
  2743. DEBUG_ERROR("Can not create lcm.zip file.\n");
  2744. result = FAIL;
  2745. }
  2746. else
  2747. {
  2748. // Write image to flash
  2749. DEBUG_INFO("Writing data to lcm.zip file...\n");
  2750. wrd=write(fd, ptr+48, ImageLen-48);
  2751. close(fd);
  2752. DEBUG_INFO(">> lcm.zip Written length: 0x%x\n", wrd);
  2753. if(wrd != (ImageLen-48))
  2754. {
  2755. result = FAIL;
  2756. }
  2757. else
  2758. {
  2759. runShellCmd("mkdir -p /mnt/lcd");
  2760. runShellCmd("unzip /mnt/lcm.zip -d /mnt/lcd");
  2761. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2762. system(cmd);
  2763. system("rm -f /mnt/lcm.zip");
  2764. result = PASS;
  2765. }
  2766. }
  2767. ShmCharger->isUpgradeLcmReq = ON;
  2768. sleep(10);
  2769. break;
  2770. default:
  2771. result = FAIL;
  2772. DEBUG_WARN("Image file is unknown type.\n");
  2773. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2774. system(cmd);
  2775. break;
  2776. }
  2777. }
  2778. else
  2779. {
  2780. result = FAIL;
  2781. DEBUG_ERROR("Model name and Firmware type error.\n");
  2782. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2783. system(cmd);
  2784. }
  2785. free(ptr);
  2786. }
  2787. else
  2788. {
  2789. result = FAIL;
  2790. DEBUG_ERROR("New firmware open error.\n");
  2791. }
  2792. }
  2793. else
  2794. {
  2795. if(strlen(file->d_name) >= 3)
  2796. {
  2797. result = FAIL;
  2798. DEBUG_ERROR("File name error.\n");
  2799. }
  2800. else
  2801. {
  2802. DEBUG_ERROR("Searching file.\n");
  2803. }
  2804. }
  2805. }
  2806. closedir (dir);
  2807. // Wait dispenser upgrade result
  2808. if(result != FAIL)
  2809. {
  2810. uint8_t isCompleted = ON;
  2811. for(uint8_t gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  2812. ShmCharger->gun_info[gun_index].isUpgradeReq = ON;
  2813. do
  2814. {
  2815. isCompleted = ON;
  2816. for(uint8_t gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  2817. {
  2818. isCompleted &= ShmCharger->gun_info[gun_index].isUpgradeEnd;
  2819. }
  2820. sleep(1);
  2821. }while((getDiffSecNow(startTime[0][TMR_IDX_UPGRADE]) < TIMEOUT_SPEC_UPGRADE) && !isCompleted);
  2822. for(uint8_t gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  2823. {
  2824. result &= ShmCharger->gun_info[gun_index].isUpgradePASS;
  2825. }
  2826. result &= (getDiffSecNow(startTime[0][TMR_IDX_UPGRADE]) <= TIMEOUT_SPEC_UPGRADE);
  2827. if(getDiffSecNow(startTime[0][TMR_IDX_UPGRADE]) > TIMEOUT_SPEC_UPGRADE)DEBUG_INFO("Upgrade process timeout.\n");
  2828. }
  2829. }
  2830. else
  2831. {
  2832. result = FAIL;
  2833. DEBUG_ERROR("/mnt does not valid.\n");
  2834. }
  2835. return result;
  2836. }
  2837. //===============================================
  2838. // Check RFID is match with start user
  2839. //===============================================
  2840. int isMatchStartUser(unsigned char gun_index)
  2841. {
  2842. uint8_t tmpUser[32];
  2843. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2844. {
  2845. // Big endian
  2846. switch(rfid.snType)
  2847. {
  2848. case RFID_SN_TYPE_6BYTE:
  2849. 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]);
  2850. break;
  2851. case RFID_SN_TYPE_7BYTE:
  2852. 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]);
  2853. break;
  2854. case RFID_SN_TYPE_10BYTE:
  2855. 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]);
  2856. break;
  2857. case RFID_SN_TYPE_4BYTE:
  2858. default:
  2859. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2860. break;
  2861. }
  2862. }
  2863. else
  2864. {
  2865. // Little endian
  2866. switch(rfid.snType)
  2867. {
  2868. case RFID_SN_TYPE_6BYTE:
  2869. 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]);
  2870. break;
  2871. case RFID_SN_TYPE_7BYTE:
  2872. 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]);
  2873. break;
  2874. case RFID_SN_TYPE_10BYTE:
  2875. 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]);
  2876. break;
  2877. case RFID_SN_TYPE_4BYTE:
  2878. default:
  2879. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2880. break;
  2881. }
  2882. }
  2883. DEBUG_INFO("==== isMatchStartUser ==== \n");
  2884. DEBUG_INFO("tmpUser : %s \n", tmpUser);
  2885. DEBUG_INFO("StartUserId : %s \n", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
  2886. DEBUG_INFO("========================== \n");
  2887. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2888. }
  2889. int isMatchPresentUser()
  2890. {
  2891. uint8_t tmpUser[32];
  2892. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2893. {
  2894. // Big endian
  2895. switch(rfid.snType)
  2896. {
  2897. case RFID_SN_TYPE_6BYTE:
  2898. 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]);
  2899. break;
  2900. case RFID_SN_TYPE_7BYTE:
  2901. 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]);
  2902. break;
  2903. case RFID_SN_TYPE_10BYTE:
  2904. 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]);
  2905. break;
  2906. case RFID_SN_TYPE_4BYTE:
  2907. default:
  2908. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2909. break;
  2910. }
  2911. }
  2912. else
  2913. {
  2914. // Little endian
  2915. switch(rfid.snType)
  2916. {
  2917. case RFID_SN_TYPE_6BYTE:
  2918. 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]);
  2919. break;
  2920. case RFID_SN_TYPE_7BYTE:
  2921. 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]);
  2922. break;
  2923. case RFID_SN_TYPE_10BYTE:
  2924. 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]);
  2925. break;
  2926. case RFID_SN_TYPE_4BYTE:
  2927. default:
  2928. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2929. break;
  2930. }
  2931. }
  2932. DEBUG_INFO("==== isMatchPresentUser ==== \n");
  2933. DEBUG_INFO("tmpUser : %s \n", tmpUser);
  2934. DEBUG_INFO("UserId : %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  2935. DEBUG_INFO("============================ \n");
  2936. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysConfig.UserId)==0)?YES:NO);
  2937. }
  2938. //===============================================
  2939. // Read RFID Serial Number
  2940. //===============================================
  2941. int GetCardSerialNumber()
  2942. {
  2943. int isSuccess = FAIL;
  2944. int module_type = MODULE_EWT;
  2945. if(getRequestCardSN(rfidFd,module_type,&rfid))
  2946. {
  2947. if(rfid.cardType == ISO14443A)
  2948. {
  2949. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  2950. {
  2951. isSuccess = PASS;
  2952. }
  2953. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  2954. {
  2955. isSuccess = PASS;
  2956. }
  2957. sethaltCard(rfidFd,module_type);
  2958. }
  2959. else if(rfid.cardType == IS014443B)
  2960. {
  2961. isSuccess = PASS;
  2962. }
  2963. else if(rfid.cardType == FELICA)
  2964. {
  2965. isSuccess = PASS;
  2966. }
  2967. else
  2968. {}
  2969. }
  2970. else
  2971. {}
  2972. return isSuccess;
  2973. }
  2974. //===============================================
  2975. // Set led motion
  2976. //===============================================
  2977. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  2978. {
  2979. switch(led_mode)
  2980. {
  2981. case LED_ACTION_INIT:
  2982. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  2983. break;
  2984. case LED_ACTION_IDLE:
  2985. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  2986. break;
  2987. case LED_ACTION_AUTHED:
  2988. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  2989. break;
  2990. case LED_ACTION_CONNECTED:
  2991. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  2992. break;
  2993. case LED_ACTION_CHARGING:
  2994. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  2995. break;
  2996. case LED_ACTION_STOP:
  2997. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  2998. break;
  2999. case LED_ACTION_ALARM:
  3000. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  3001. break;
  3002. case LED_ACTION_MAINTAIN:
  3003. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  3004. break;
  3005. case LED_ACTION_RFID_PASS:
  3006. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  3007. break;
  3008. case LED_ACTION_RFID_FAIL:
  3009. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  3010. break;
  3011. case LED_ACTION_BLE_CONNECT:
  3012. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  3013. break;
  3014. case LED_ACTION_BLE_DISABLE:
  3015. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  3016. break;
  3017. case LED_ACTION_DEBUG:
  3018. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  3019. break;
  3020. case LED_ACTION_ALL_OFF:
  3021. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  3022. break;
  3023. case LED_RELAY_ON:
  3024. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  3025. break;
  3026. case LED_RELAY_OFF:
  3027. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  3028. break;
  3029. case LED_ACTION_HANDSHAKE_FAIL:
  3030. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  3031. break;
  3032. case LED_ACTION_INTERNET_DISCONNECT:
  3033. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  3034. break;
  3035. case LED_ACTION_RESTORE_SETTING:
  3036. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RESTORE_SETTING;
  3037. break;
  3038. case LED_ACTION_IDLE_BACKEND_CONNECTED:
  3039. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_CONNECTED;
  3040. break;
  3041. case LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP:
  3042. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP;
  3043. break;
  3044. case LED_ACTION_IDLE_BACKEND_DISCONNECTED:
  3045. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_DISCONNECTED;
  3046. break;
  3047. case LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP:
  3048. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP;
  3049. break;
  3050. case LED_ACTION_RESERVATION_MODE:
  3051. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RESERVATION_MODE;
  3052. break;
  3053. }
  3054. }
  3055. //===============================================
  3056. // Request on/off set
  3057. //===============================================
  3058. void setRequest(unsigned char gun_index,unsigned char isOn)
  3059. {
  3060. if(isOn == ON)
  3061. {
  3062. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  3063. {
  3064. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  3065. DEBUG_INFO("Gun-%d permission request: ON \n",gun_index);
  3066. }
  3067. }
  3068. else
  3069. {
  3070. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  3071. {
  3072. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  3073. DEBUG_INFO("Gun-%d permission request: OFF \n",gun_index);
  3074. }
  3075. }
  3076. }
  3077. //===============================================
  3078. // Request on/off get
  3079. //===============================================
  3080. int getRequest(unsigned char gun_index)
  3081. {
  3082. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  3083. }
  3084. //===============================================
  3085. // Relay on/off set
  3086. //===============================================
  3087. void setRelay(unsigned char gun_index,unsigned char isOn)
  3088. {
  3089. if(isOn == ON)
  3090. {
  3091. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  3092. {
  3093. switch(ShmCharger->gun_info[gun_index].chargingMode)
  3094. {
  3095. case CHARGING_MODE_BS:
  3096. case CHARGING_MODE_HLC:
  3097. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3098. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  3099. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  3100. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  3101. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3102. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_BS] \n",gun_index);
  3103. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3104. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_HLC] \n",gun_index);
  3105. break;
  3106. case CHARGING_MODE_SOCKETE:
  3107. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3108. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = 0x01;
  3109. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_SOCKETE] \n",gun_index);
  3110. break;
  3111. default:
  3112. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3113. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  3114. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  3115. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  3116. DEBUG_INFO("Gun-%d Output relay status: ON. [DEFFAULT] \n",gun_index);
  3117. break;
  3118. }
  3119. }
  3120. }
  3121. else
  3122. {
  3123. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  3124. {
  3125. switch(ShmCharger->gun_info[gun_index].chargingMode)
  3126. {
  3127. case CHARGING_MODE_BS:
  3128. case CHARGING_MODE_HLC:
  3129. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3130. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  3131. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  3132. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  3133. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3134. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_BS] \n",gun_index);
  3135. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3136. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_HLC] \n",gun_index);
  3137. break;
  3138. case CHARGING_MODE_SOCKETE:
  3139. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3140. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = 0;
  3141. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_SOCKETE] \n",gun_index);
  3142. break;
  3143. default:
  3144. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3145. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  3146. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  3147. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  3148. DEBUG_INFO("Gun-%d Output relay status: OFF. [DEFFAULT] \n",gun_index);
  3149. break;
  3150. }
  3151. }
  3152. }
  3153. }
  3154. //===============================================
  3155. // Relay on/off get
  3156. //===============================================
  3157. int getRelay(unsigned char gun_index)
  3158. {
  3159. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  3160. }
  3161. //===============================================
  3162. // Set speaker on/off request
  3163. //===============================================
  3164. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  3165. {
  3166. if(isOn == ON)
  3167. {
  3168. ShmCharger->isSpeakerOn = ON;
  3169. ShmCharger->speaker_type = speaker_mode;
  3170. }
  3171. }
  3172. //===============================================
  3173. // Initialization RFID communication port
  3174. //===============================================
  3175. int InitRfidPort()
  3176. {
  3177. int uartO2 = open(rfidPortName, O_RDWR);
  3178. struct termios tios;
  3179. if (uartO2 != FAIL)
  3180. {
  3181. ioctl (uartO2, TCGETS, &tios);
  3182. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  3183. tios.c_lflag = 0;
  3184. tios.c_iflag = 0;
  3185. tios.c_oflag = 0;
  3186. tios.c_cc[VMIN] = 0;
  3187. tios.c_cc[VTIME] = (unsigned char) 1;
  3188. tios.c_lflag = 0;
  3189. tcflush(uartO2, TCIFLUSH);
  3190. ioctl(uartO2, TCSETS, &tios);
  3191. }
  3192. if (uartO2 < 0)
  3193. {
  3194. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  3195. }
  3196. return uartO2;
  3197. }
  3198. //===============================================
  3199. // Check internet access status
  3200. //===============================================
  3201. int isReachableInternet()
  3202. {
  3203. int result = FAIL;
  3204. FILE *fp;
  3205. char cmd[256];
  3206. char buf[512];
  3207. char tmp[512];
  3208. // Get ip address & net mask
  3209. strcpy(cmd, "ifconfig eth0");
  3210. fp = popen(cmd, "r");
  3211. if (fp != NULL)
  3212. {
  3213. while(fgets(buf, sizeof(buf), fp) != NULL)
  3214. {
  3215. if (strstr(buf, "inet addr:") > 0)
  3216. {
  3217. sscanf(buf, "%*s%s", tmp);
  3218. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  3219. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != 0)
  3220. {
  3221. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  3222. }
  3223. sscanf(buf, "%*s%*s%*s%s", tmp);
  3224. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  3225. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  3226. {
  3227. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  3228. }
  3229. }
  3230. }
  3231. }
  3232. pclose(fp);
  3233. memset(buf, 0x00, sizeof(buf));
  3234. // Get gateway
  3235. fp = popen("ip route", "r");
  3236. if(fp == NULL)
  3237. result = FAIL;
  3238. else
  3239. {
  3240. while(fgets(buf, sizeof(buf), fp) != NULL)
  3241. {
  3242. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  3243. break;
  3244. }
  3245. if(strstr(buf, "default") != NULL)
  3246. {
  3247. sscanf(buf, "%*s%*s%s", tmp);
  3248. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  3249. }
  3250. }
  3251. pclose(fp);
  3252. memset(buf, 0x00, sizeof(buf));
  3253. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  3254. {
  3255. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  3256. fp = popen(cmd, "r");
  3257. if(fp != NULL)
  3258. {
  3259. while(fgets(buf, sizeof(buf), fp) != NULL)
  3260. {
  3261. if(strstr(buf, "transmitted") > 0)
  3262. {
  3263. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  3264. if(strstr(buf,"100%") != NULL)
  3265. {
  3266. }
  3267. else
  3268. {
  3269. result = PASS;
  3270. }
  3271. //DEBUG_INFO("%s",buf);
  3272. //DEBUG_INFO("%s\n",tmp);
  3273. }
  3274. }
  3275. }
  3276. pclose(fp);
  3277. }
  3278. return result;
  3279. }
  3280. int isRouteFail()
  3281. {
  3282. int result = YES;
  3283. FILE *fp;
  3284. char buf[512];
  3285. fp = popen("route -n", "r");
  3286. if(fp != NULL)
  3287. {
  3288. while(fgets(buf, sizeof(buf), fp) != NULL)
  3289. {
  3290. if(strstr(buf, "eth0") != NULL)
  3291. result = NO;
  3292. }
  3293. }
  3294. pclose(fp);
  3295. return result;
  3296. }
  3297. //===============================================
  3298. // Check reservation date is expired
  3299. //===============================================
  3300. int isReservationExpired(unsigned char gun_index)
  3301. {
  3302. int result = NO;
  3303. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3304. {
  3305. if(isOvertNow(ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate))
  3306. result = YES;
  3307. }
  3308. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3309. {
  3310. if(isOvertNow(ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime))
  3311. result = YES;
  3312. }
  3313. return result;
  3314. }
  3315. //===============================================
  3316. // Check charging profile related date routine
  3317. //===============================================
  3318. int isProfileValid(uint8_t gun_index)
  3319. {
  3320. int result = NO;
  3321. struct tm tmFrom, tmTo;
  3322. struct timeb tbFrom, tbTo;
  3323. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3324. {
  3325. 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) &&
  3326. (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))
  3327. {
  3328. tmFrom.tm_year -= 1900;
  3329. tmFrom.tm_mon -= 1;
  3330. tbFrom.time = mktime(&tmFrom);
  3331. tmTo.tm_year -= 1900;
  3332. tmTo.tm_mon -= 1;
  3333. tbTo.time = mktime(&tmTo);
  3334. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3335. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3336. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  3337. {
  3338. result = YES;
  3339. }
  3340. }
  3341. else
  3342. {
  3343. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3344. }
  3345. }
  3346. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3347. {
  3348. 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) &&
  3349. (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))
  3350. {
  3351. tmFrom.tm_year -= 1900;
  3352. tmFrom.tm_mon -= 1;
  3353. tbFrom.time = mktime(&tmFrom);
  3354. tmTo.tm_year -= 1900;
  3355. tmTo.tm_mon -= 1;
  3356. tbTo.time = mktime(&tmTo);
  3357. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3358. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3359. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  3360. {
  3361. result = YES;
  3362. }
  3363. }
  3364. else
  3365. {
  3366. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3367. }
  3368. }
  3369. return result;
  3370. }
  3371. int getScheduleStart(int gun_index)
  3372. {
  3373. int result = -1;
  3374. struct tm tmScheduleStart;;
  3375. struct timeb tbScheduleStart;
  3376. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3377. {
  3378. 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))
  3379. {
  3380. tmScheduleStart.tm_year -= 1900;
  3381. tmScheduleStart.tm_mon -= 1;
  3382. tbScheduleStart.time = mktime(&tmScheduleStart);
  3383. tbScheduleStart.millitm = 0;
  3384. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3385. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3386. }
  3387. else
  3388. {
  3389. DEBUG_WARN("Schedule start date parsing error.\n");
  3390. }
  3391. }
  3392. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3393. {
  3394. 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))
  3395. {
  3396. tmScheduleStart.tm_year -= 1900;
  3397. tmScheduleStart.tm_mon -= 1;
  3398. tbScheduleStart.time = mktime(&tmScheduleStart);
  3399. tbScheduleStart.millitm = 0;
  3400. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3401. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3402. }
  3403. else
  3404. {
  3405. DEBUG_WARN("Schedule start date parsing error.\n");
  3406. }
  3407. }
  3408. return result;
  3409. }
  3410. int getStartSinceToday()
  3411. {
  3412. int result = -1;
  3413. time_t t;
  3414. struct tm *tmStartToday;
  3415. struct timeb tbStartToday;
  3416. t=time(NULL);
  3417. tmStartToday=localtime(&t);
  3418. tmStartToday->tm_hour = 0;
  3419. tmStartToday->tm_min = 0;
  3420. tmStartToday->tm_sec = 0;
  3421. tbStartToday.time = mktime(tmStartToday);
  3422. result = DiffTimebWithNow(tbStartToday)/1000;
  3423. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  3424. return result;
  3425. }
  3426. int getStartSinceWeek()
  3427. {
  3428. int result = -1;
  3429. time_t t;
  3430. struct tm *tmStartWeek;
  3431. struct timeb tbStartWeek;
  3432. t=time(NULL);
  3433. tmStartWeek=localtime(&t);
  3434. t-=86400*tmStartWeek->tm_wday;
  3435. tmStartWeek=localtime(&t);
  3436. tmStartWeek->tm_hour = 0;
  3437. tmStartWeek->tm_min = 0;
  3438. tmStartWeek->tm_sec = 0;
  3439. tbStartWeek.time = mktime(tmStartWeek);
  3440. result = DiffTimebWithNow(tbStartWeek)/1000;
  3441. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  3442. return result;
  3443. }
  3444. //===============================================
  3445. // Valid from local white list
  3446. //===============================================
  3447. int isValidLocalWhiteCard()
  3448. {
  3449. uint8_t result = FAIL;
  3450. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  3451. {
  3452. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  3453. {
  3454. result = PASS;
  3455. }
  3456. }
  3457. return result;
  3458. }
  3459. //==========================================
  3460. // Check routine
  3461. //==========================================
  3462. void checkTask()
  3463. {
  3464. /*
  3465. * TODO:
  3466. * 1. Rotary switch define change with system specification
  3467. */
  3468. // Task run only on cabinet
  3469. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch != ROTARY_SWITCH_E_DISPENSER)
  3470. {
  3471. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3472. {
  3473. if(system("pidof -s Module_4g > /dev/null") != 0)
  3474. {
  3475. DEBUG_INFO("Module_4g not running, restart it.\n");
  3476. system("/root/Module_4g &");
  3477. }
  3478. }
  3479. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3480. {
  3481. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  3482. {
  3483. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  3484. system("/root/Module_Wifi &");
  3485. }
  3486. }
  3487. if((strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0) && !ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  3488. {
  3489. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3490. {
  3491. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  3492. {
  3493. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  3494. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3495. system("pkill OcppBackend");
  3496. sleep(3);
  3497. ocpp_process_start();
  3498. }
  3499. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3500. {
  3501. DEBUG_INFO("OcppBackend not running, restart it.\n");
  3502. ocpp_process_start();
  3503. }
  3504. }
  3505. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3506. {
  3507. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  3508. {
  3509. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  3510. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3511. system("pkill OcppBackend20");
  3512. sleep(3);
  3513. ocpp_process_start();
  3514. }
  3515. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3516. {
  3517. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  3518. ocpp_process_start();
  3519. }
  3520. }
  3521. }
  3522. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  3523. {
  3524. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3525. {
  3526. DEBUG_INFO("OcppBackendPH not running, restart it.\n");
  3527. system("/root/OcppBackendPH &");
  3528. }
  3529. }
  3530. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  3531. {
  3532. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  3533. system("/root/Module_Speaker &");
  3534. }
  3535. if(system("pidof -s Module_LcmControl > /dev/null") != 0)
  3536. {
  3537. DEBUG_INFO("Module_LcmControl not running, restart it.\n");
  3538. system ("/root/Module_LcmControl &");
  3539. }
  3540. if((system("pidof -s Module_PowerSharing > /dev/null") != 0) &&
  3541. ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  3542. {
  3543. DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
  3544. system ("/root/Module_PowerSharing &");
  3545. }
  3546. /*
  3547. if((system("pidof -s Module_InitUpgrade > /dev/null") != 0) &&
  3548. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
  3549. {
  3550. DEBUG_INFO("Module_InitUpgrade not running, restart it.\n");
  3551. system ("/root/Module_InitUpgrade &");
  3552. }*/
  3553. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  3554. {
  3555. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  3556. system("/root/Module_AlarmDetect &");
  3557. }
  3558. }
  3559. // Task run both on cabinet and dispenser
  3560. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  3561. {
  3562. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  3563. system("/root/Module_EventLogging &");
  3564. }
  3565. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  3566. {
  3567. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  3568. system("/root/Module_InternalComm &");
  3569. }
  3570. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  3571. {
  3572. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  3573. system ("/root/Module_ProduceUtils &");
  3574. }
  3575. if((system("pidof -s Module_Cabinet > /dev/null") != 0))
  3576. {
  3577. DEBUG_INFO("Module_Cabinet not running, restart it.\n");
  3578. system ("/root/Module_Cabinet &");
  3579. }
  3580. if((system("pidof -s Module_Dispenser > /dev/null") != 0))
  3581. {
  3582. DEBUG_INFO("Module_Dispenser not running, restart it.\n");
  3583. system ("/root/Module_Dispenser &");
  3584. }
  3585. }
  3586. void checkConnectionTimeout()
  3587. {
  3588. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  3589. {
  3590. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  3591. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
  3592. }
  3593. else
  3594. {
  3595. ShmCharger->timeoutSpec.Present_Timeout_Spec = ocpp_get_connection_timeout();
  3596. /*
  3597. if((ShmCharger->timeoutSpec.Setting_Timeout_Spec) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  3598. {
  3599. ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE+10);
  3600. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  3601. }
  3602. else
  3603. {
  3604. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec);
  3605. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  3606. }*/
  3607. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3608. }
  3609. }
  3610. void checkReset()
  3611. {
  3612. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3613. {
  3614. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3615. {
  3616. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3617. ((modelnameInfo.GetGunCount==2)?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE) &&
  3618. ((modelnameInfo.GetGunCount==3)?(!isMode(2, SYS_MODE_CHARGING) && !isMode(2, SYS_MODE_TERMINATING) && !isMode(2, SYS_MODE_COMPLETE)):TRUE))
  3619. {
  3620. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  3621. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3622. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  3623. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  3624. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  3625. {
  3626. for(int gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  3627. {
  3628. ShmCharger->gun_info[gun_index].isResetHardReq = ON;
  3629. setLedMotion(gun_index,LED_ACTION_INIT);
  3630. }
  3631. }
  3632. else
  3633. {
  3634. for(int gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  3635. {
  3636. ShmCharger->gun_info[gun_index].isResetSoftReq = ON;
  3637. setLedMotion(gun_index,LED_ACTION_INIT);
  3638. }
  3639. }
  3640. sleep(2);
  3641. }
  3642. }
  3643. }
  3644. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3645. {
  3646. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  3647. {
  3648. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3649. ((modelnameInfo.GetGunCount==2)?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE) &&
  3650. ((modelnameInfo.GetGunCount==3)?(!isMode(2, SYS_MODE_CHARGING) && !isMode(2, SYS_MODE_TERMINATING) && !isMode(2, SYS_MODE_COMPLETE)):TRUE))
  3651. {
  3652. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  3653. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  3654. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  3655. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  3656. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  3657. {
  3658. for(int gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  3659. {
  3660. ShmCharger->gun_info[gun_index].isResetHardReq = ON;
  3661. setLedMotion(gun_index,LED_ACTION_INIT);
  3662. }
  3663. }
  3664. else
  3665. {
  3666. for(int gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  3667. {
  3668. ShmCharger->gun_info[gun_index].isResetSoftReq = ON;
  3669. setLedMotion(gun_index,LED_ACTION_INIT);
  3670. }
  3671. }
  3672. sleep(2);
  3673. }
  3674. }
  3675. }
  3676. if(ShmDispenser->gun_info.isResetHardReq)
  3677. {
  3678. system("sync");
  3679. sleep(5);
  3680. system("reboot -f");
  3681. sleep(5);
  3682. system("reboot -f");
  3683. }
  3684. else if(ShmDispenser->gun_info.isResetSoftReq)
  3685. {
  3686. sleep(5);
  3687. close(wtdFd);
  3688. system("/usr/bin/run_evse_restart.sh");
  3689. }
  3690. }
  3691. void checkReservation(uint8_t gun_index)
  3692. {
  3693. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3694. {
  3695. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  3696. {
  3697. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3698. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3699. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3700. {
  3701. sleep(5);
  3702. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3703. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3704. }
  3705. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3706. }
  3707. }
  3708. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3709. {
  3710. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  3711. {
  3712. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3713. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3714. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3715. {
  3716. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  3717. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3718. }
  3719. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3720. }
  3721. }
  3722. }
  3723. void checkUnlocker(uint8_t gun_index)
  3724. {
  3725. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3726. {
  3727. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3728. {
  3729. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3730. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  3731. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  3732. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  3733. }
  3734. }
  3735. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3736. {
  3737. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3738. {
  3739. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3740. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "UnlockFailed");
  3741. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  3742. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  3743. }
  3744. }
  3745. }
  3746. void checkAvailability(uint8_t gun_index)
  3747. {
  3748. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3749. {
  3750. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3751. {
  3752. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  3753. {
  3754. DB_Update_Operactive(localDb, gun_index, true);
  3755. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3756. }
  3757. else
  3758. {
  3759. DB_Update_Operactive(localDb, gun_index, false);
  3760. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3761. }
  3762. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3763. }
  3764. }
  3765. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3766. {
  3767. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3768. {
  3769. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  3770. {
  3771. DB_Update_Operactive(localDb, gun_index, true);
  3772. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3773. }
  3774. else
  3775. {
  3776. DB_Update_Operactive(localDb, gun_index, false);
  3777. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3778. }
  3779. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3780. }
  3781. }
  3782. }
  3783. void checkChargingProfileLimit(uint8_t gun_index)
  3784. {
  3785. static struct PreviousData
  3786. {
  3787. uint16_t targetCurrent;
  3788. uint16_t current_limit;
  3789. }previousData[3];
  3790. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3791. {
  3792. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  3793. (ocpp_get_profile_req(gun_index) != ON) &&
  3794. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  3795. {
  3796. // Checking profile kind
  3797. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  3798. {
  3799. // Absolute profile
  3800. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  3801. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  3802. {
  3803. // Checking limitation
  3804. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3805. {
  3806. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3807. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3808. )
  3809. {
  3810. 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);
  3811. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3812. }
  3813. else
  3814. break;
  3815. }
  3816. }
  3817. }
  3818. }
  3819. else if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == 0) && (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  3820. {
  3821. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3822. }
  3823. }
  3824. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3825. {
  3826. if((ocpp_get_smartcharging_profileId(gun_index) > 0) &&
  3827. (ocpp_get_profile_req(gun_index) != ON) &&
  3828. (((strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom)>0) && (strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo)>0)) ? isProfileValid(gun_index) : ON))
  3829. {
  3830. // Checking profile kind
  3831. if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  3832. {
  3833. // Absolute profile
  3834. if(((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP20Data->SmartChargingProfile[gun_index].transactionId == ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId)) ||
  3835. (mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfilePurpose, "TxProfile") == FAIL))
  3836. {
  3837. // Checking limitation
  3838. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3839. {
  3840. if((getScheduleStart(gun_index) > ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3841. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3842. )
  3843. {
  3844. 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);
  3845. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3846. }
  3847. else
  3848. break;
  3849. }
  3850. }
  3851. }
  3852. }
  3853. else if((ShmOCPP20Data->SmartChargingProfile[gun_index].id == 0) && (ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  3854. {
  3855. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3856. }
  3857. }
  3858. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3859. {
  3860. 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);
  3861. 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);
  3862. }
  3863. else
  3864. {
  3865. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3866. 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);
  3867. }
  3868. if(ShmCharger->gun_info[gun_index].targetCurrent != previousData[gun_index].targetCurrent)
  3869. {
  3870. DEBUG_INFO("SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  3871. DEBUG_INFO("gun-%02d target current: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  3872. previousData[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent;
  3873. }
  3874. if(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != previousData[gun_index].current_limit)
  3875. {
  3876. DEBUG_INFO("gun-%02d mcu current_limit: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  3877. previousData[gun_index].current_limit = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  3878. }
  3879. }
  3880. void checkStopReason(uint8_t gun_index)
  3881. {
  3882. sleep(2);
  3883. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3884. {
  3885. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3886. if(!ocpp_get_starttransaction_result(gun_index))
  3887. {
  3888. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  3889. }
  3890. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3891. {
  3892. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  3893. }
  3894. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3895. {
  3896. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3897. }
  3898. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3899. {
  3900. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3901. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3902. else
  3903. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3904. }
  3905. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3906. {
  3907. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3908. if(!isMatchStartUser(gun_index))
  3909. {
  3910. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3911. }
  3912. else
  3913. {
  3914. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3915. }
  3916. DEBUG_INFO("Gun-%d [ IdTag ] : %s \n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  3917. }
  3918. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3919. {
  3920. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3921. }
  3922. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3923. {
  3924. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3925. }
  3926. else
  3927. {
  3928. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3929. }
  3930. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3931. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  3932. presentChargedEnergyUpdate(gun_index);
  3933. DEBUG_INFO("PresentChargedEnergy Gun-[%d] : %.4f \n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  3934. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3935. }
  3936. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3937. {
  3938. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  3939. if(!ocpp_get_starttransaction_result(gun_index))
  3940. {
  3941. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  3942. }
  3943. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3944. {
  3945. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  3946. }
  3947. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3948. {
  3949. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  3950. }
  3951. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  3952. {
  3953. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  3954. }
  3955. else if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  3956. {
  3957. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0)
  3958. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  3959. else
  3960. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OnIdle");
  3961. }
  3962. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3963. {
  3964. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  3965. if(!isMatchStartUser(gun_index))
  3966. {
  3967. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  3968. }
  3969. else
  3970. {
  3971. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3972. }
  3973. DEBUG_INFO("Gun-%d [ idToken ] : %s \n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  3974. }
  3975. else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L1_OVER_CURRENT) ||
  3976. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L2_OVER_CURRENT) ||
  3977. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L3_OVER_CURRENT))
  3978. {
  3979. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  3980. }
  3981. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  3982. {
  3983. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  3984. }
  3985. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  3986. {
  3987. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  3988. }
  3989. else
  3990. {
  3991. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  3992. }
  3993. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  3994. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  3995. presentChargedEnergyUpdate(gun_index);
  3996. DEBUG_INFO("PresentChargedEnergy Gun-[%d] : %.4f \n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  3997. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  3998. }
  3999. }
  4000. void checkRemoteUpgradeStatus()
  4001. {
  4002. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4003. {
  4004. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  4005. {
  4006. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4007. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4008. ShmCharger->isUpdateSuccess = NO;
  4009. }
  4010. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  4011. {
  4012. DEBUG_INFO("Firmware remote upgrading...\n");
  4013. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4014. sleep(1);
  4015. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4016. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4017. if(upgrade_check() == PASS)
  4018. {
  4019. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4020. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4021. ShmCharger->isUpdateSuccess = YES;
  4022. DEBUG_INFO("Remote update success.\n");
  4023. }
  4024. else
  4025. {
  4026. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4027. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4028. ShmCharger->isUpdateSuccess = NO;
  4029. DEBUG_INFO("Remote update fail.\n");
  4030. }
  4031. }
  4032. else
  4033. {}
  4034. }
  4035. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4036. {
  4037. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  4038. {
  4039. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4040. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4041. ShmCharger->isUpdateSuccess = NO;
  4042. }
  4043. else if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0)
  4044. {
  4045. DEBUG_INFO("Firmware remote upgrading...\n");
  4046. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4047. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4048. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4049. if(upgrade_check() == PASS)
  4050. {
  4051. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4052. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4053. ShmCharger->isUpdateSuccess = YES;
  4054. DEBUG_INFO("Remote update success.\n");
  4055. }
  4056. else
  4057. {
  4058. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4059. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4060. ShmCharger->isUpdateSuccess = NO;
  4061. DEBUG_INFO("Remote update fail.\n");
  4062. }
  4063. }
  4064. else
  4065. {}
  4066. }
  4067. }
  4068. void checkHandshakeTime()
  4069. {
  4070. // Handskake timer needs to refresh
  4071. if(ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
  4072. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4073. // Timer for lcm to shows
  4074. ShmCharger->timeoutSpec.Handshake_Timeout = (ocpp_get_connection_timeout() - getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]));
  4075. }
  4076. void checkRfidAuthrize()
  4077. {
  4078. static uint8_t isCheckdResult = FALSE;
  4079. if(!ShmCharger->isAuthrizing)
  4080. {
  4081. // Read RFID
  4082. if(GetCardSerialNumber() != FAIL)
  4083. {
  4084. uint8_t isSnStart = FALSE;
  4085. uint8_t bufferRFID[ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId)];
  4086. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4087. {
  4088. // Big endian
  4089. switch(rfid.snType)
  4090. {
  4091. case RFID_SN_TYPE_6BYTE:
  4092. 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]);
  4093. break;
  4094. case RFID_SN_TYPE_7BYTE:
  4095. 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]);
  4096. break;
  4097. case RFID_SN_TYPE_10BYTE:
  4098. 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]);
  4099. break;
  4100. case RFID_SN_TYPE_4BYTE:
  4101. default:
  4102. sprintf((char*)bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4103. break;
  4104. }
  4105. }
  4106. else
  4107. {
  4108. // Little endian
  4109. switch(rfid.snType)
  4110. {
  4111. case RFID_SN_TYPE_6BYTE:
  4112. 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]);
  4113. break;
  4114. case RFID_SN_TYPE_7BYTE:
  4115. 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]);
  4116. break;
  4117. case RFID_SN_TYPE_10BYTE:
  4118. 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]);
  4119. break;
  4120. case RFID_SN_TYPE_4BYTE:
  4121. default:
  4122. sprintf((char*)bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4123. break;
  4124. }
  4125. }
  4126. DEBUG_INFO("Authorize request User Id : %s\n", bufferRFID);
  4127. //DEBUG_INFO("Authorize request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4128. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4129. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  4130. // Check SN already start
  4131. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4132. {
  4133. if(isMatchStartUser(gun_index) || (isMatchPresentUser(gun_index) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING)))
  4134. {
  4135. DEBUG_INFO("%s running on connector-%02d.\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, gun_index);
  4136. isSnStart = TRUE;
  4137. ShmCharger->gun_info[gun_index].rfidReq = ON;
  4138. ocpp_set_auth_conf(ON);
  4139. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4140. setSpeaker(ON, SPEAKER_SHORT);
  4141. sleep(3);
  4142. break;
  4143. }
  4144. }
  4145. // Request authorize if isSnStart is false
  4146. if(!isSnStart)
  4147. {
  4148. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, bufferRFID, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4149. refreshStartTimer(&startTime[0][TMR_IDX_AUTH]);
  4150. ocpp_set_auth_conf(OFF);
  4151. ocpp_set_auth_req(ON, "ISO14443");
  4152. setLedMotion(0,LED_ACTION_AUTHED);
  4153. ShmCharger->isAuthrizing = TRUE;
  4154. ShmCharger->isGetAuthResult = FALSE;
  4155. isCheckdResult = FALSE;
  4156. //sleep(1);
  4157. }
  4158. }
  4159. }
  4160. else
  4161. {
  4162. // Wait authorize result
  4163. if(!ocpp_get_auth_conf() && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH))
  4164. {
  4165. // Authorization timeout process.
  4166. DEBUG_WARN("Authorize timeout !!!\n");
  4167. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4168. {
  4169. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4170. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4171. }
  4172. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4173. sleep(3);
  4174. ShmCharger->isAuthrizing = FALSE;
  4175. }
  4176. else
  4177. {
  4178. if(ocpp_get_auth_conf() ||
  4179. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  4180. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > 2))
  4181. )
  4182. {
  4183. if(ocpp_get_auth_result(NO) ||
  4184. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  4185. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  4186. {
  4187. if(!isCheckdResult)
  4188. {
  4189. DEBUG_INFO("Authorize pass.\n");
  4190. setSpeaker(ON, SPEAKER_SHORT);
  4191. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4192. ShmCharger->isGetAuthResult = TRUE;
  4193. isCheckdResult = TRUE;
  4194. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  4195. {
  4196. ocpp_set_auth_conf(ON);
  4197. }
  4198. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4199. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  4200. }
  4201. if(getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < ocpp_get_connection_timeout())
  4202. {
  4203. checkHandshakeTime();
  4204. if(GetCardSerialNumber() != FAIL)
  4205. {
  4206. if(isMatchPresentUser())
  4207. {
  4208. DEBUG_INFO("Cancel present user.\n");
  4209. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4210. {
  4211. setLedMotion(gun_index,LED_ACTION_IDLE);
  4212. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4213. }
  4214. ShmCharger->isAuthrizing = FALSE;
  4215. }
  4216. }
  4217. else
  4218. {
  4219. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4220. {
  4221. if(getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < 3)
  4222. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4223. else
  4224. setLedMotion(gun_index,LED_ACTION_AUTHED);
  4225. 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))) ||
  4226. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) && (ShmCharger->gun_selectd == gun_index)) ||
  4227. (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING)) && (ShmCharger->gun_selectd == gun_index))
  4228. )
  4229. {
  4230. DEBUG_INFO("Connector-%02d action detect.\n", gun_index);
  4231. ShmCharger->isAuthrizing = FALSE;
  4232. ShmCharger->gun_info[gun_index].rfidReq = ON;
  4233. break;
  4234. }
  4235. }
  4236. }
  4237. }
  4238. else
  4239. {
  4240. DEBUG_INFO("Connector action detect timeout.\n");
  4241. ShmCharger->isAuthrizing = FALSE;
  4242. ShmCharger->isGetAuthResult = TRUE;
  4243. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4244. {
  4245. setLedMotion(gun_index,LED_ACTION_IDLE);
  4246. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4247. }
  4248. }
  4249. }
  4250. else
  4251. {
  4252. DEBUG_INFO("Authorize fail.\n");
  4253. ShmCharger->isAuthrizing = FALSE;
  4254. ShmCharger->isGetAuthResult = TRUE;
  4255. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4256. {
  4257. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4258. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4259. }
  4260. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4261. sleep(3);
  4262. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4263. {
  4264. setLedMotion(gun_index,LED_ACTION_IDLE);
  4265. }
  4266. }
  4267. }
  4268. }
  4269. }
  4270. }
  4271. //======================================================
  4272. // Main process
  4273. //======================================================
  4274. int main(void)
  4275. {
  4276. //==================================================
  4277. // Create all share memory
  4278. //==================================================
  4279. if(CreatShareMemory()==0)
  4280. {
  4281. DEBUG_ERROR("CreatShareMemory NG\n");
  4282. if(ShmStatusCodeData!=NULL)
  4283. {
  4284. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  4285. }
  4286. sleep(5);
  4287. system("reboot -f");
  4288. sleep(5);
  4289. system("reboot -f");
  4290. }
  4291. else
  4292. {
  4293. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  4294. DEBUG_INFO("CreatShareMemory OK\n");
  4295. }
  4296. if(SpawnTask() != PASS)
  4297. {
  4298. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  4299. DEBUG_ERROR("Task spawn fail.\n");
  4300. }
  4301. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4302. {
  4303. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  4304. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = 0xff;
  4305. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  4306. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  4307. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  4308. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  4309. ShmCharger->gun_selectd = 0;
  4310. }
  4311. //==================================================
  4312. // Main loop
  4313. //==================================================
  4314. for(;;)
  4315. {
  4316. //==============================================
  4317. // Period check for 10 seconds
  4318. //==============================================
  4319. if(getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10)
  4320. {
  4321. //==============================================
  4322. // Check task processing
  4323. //==============================================
  4324. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4325. checkTask();
  4326. //==============================================
  4327. // Check connection timeout specification
  4328. //==============================================
  4329. checkConnectionTimeout();
  4330. refreshStartTimer(&startTime[0][TMR_IDX_CHECK_TASK]);
  4331. }
  4332. //==============================================
  4333. // Check remote reset request
  4334. //==============================================
  4335. checkReset();
  4336. //==============================================
  4337. // Connector loop
  4338. //==============================================
  4339. /*
  4340. * TODO:
  4341. * 1. Rotary switch define change with system specification
  4342. */
  4343. // Only process main logic on cabinet
  4344. if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch != ROTARY_SWITCH_E_DISPENSER)
  4345. {
  4346. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4347. {
  4348. //==============================================
  4349. // Synchronize share memory from OCPP struct
  4350. //==============================================
  4351. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  4352. //==============================================
  4353. // Check RFID authorization
  4354. //==============================================
  4355. // The user is not allowed to use RFID when the system change to under those modes.
  4356. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM) &&
  4357. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_FAULT) &&
  4358. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_DEBUG) &&
  4359. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE) &&
  4360. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_BOOTING) &&
  4361. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_COMPLETE) &&
  4362. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_MAINTAIN))
  4363. {
  4364. checkRfidAuthrize();
  4365. }
  4366. //==============================================
  4367. // Something need run in Idle mode
  4368. //==============================================
  4369. 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)) &&
  4370. ((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) &&
  4371. ((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))
  4372. {
  4373. // Check restore factory setting request
  4374. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4375. {
  4376. // Set led to initial
  4377. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4378. {
  4379. setLedMotion(gun_index,LED_ACTION_INIT);
  4380. }
  4381. sleep(5);
  4382. system("cd /root;./Module_FactoryConfig -m");
  4383. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4384. system("sync");
  4385. sleep(5);
  4386. system("reboot -f");
  4387. sleep(5);
  4388. system("reboot -f");
  4389. }
  4390. // Check upgrade firmware request
  4391. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  4392. ocpp_get_update_firmware_req())
  4393. {
  4394. // If available memory too small, free memory cache first
  4395. if(getAvailableMemory() < (200*1024*1024))
  4396. {
  4397. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  4398. system("echo 3 > /proc/sys/vm/drop_caches");
  4399. }
  4400. ShmCharger->isUpdateSuccess = NO;
  4401. for(int gun_index = 0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  4402. {
  4403. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4404. setChargerMode(gun_index, SYS_MODE_UPDATE);
  4405. }
  4406. }
  4407. }
  4408. //==========================================
  4409. // Power saving logic
  4410. //==========================================
  4411. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus > SYS_MODE_BOOTING)
  4412. {
  4413. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  4414. {
  4415. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  4416. }
  4417. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM) ||
  4418. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_FAULT) ||
  4419. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE) ||
  4420. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_MAINTAIN) ||
  4421. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) ||
  4422. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING) ||
  4423. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_COMPLETE))
  4424. {
  4425. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4426. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4427. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4428. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4429. }
  4430. else
  4431. {
  4432. if((ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES) ||
  4433. (gpio_get_value(GPIO_IN_WAKEUP) == OFF) ||
  4434. (ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON) ||
  4435. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus != ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus) ||
  4436. (((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)) &&
  4437. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) &&
  4438. (getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]) < 10)))
  4439. {
  4440. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4441. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4442. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4443. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4444. if(((gpio_get_value(GPIO_IN_WAKEUP) == OFF) || (ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON)) &&
  4445. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) &&
  4446. (getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_CHECK_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
  4447. {
  4448. ShmCharger->gun_info[gun_index].isCheckPowerConsumption = YES;
  4449. refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
  4450. }
  4451. }
  4452. else
  4453. {
  4454. refreshStartTimer(&startTime[gun_index][TMR_IDX_CHECK_POWER_CONSUMPTION]);
  4455. if((getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_LCM_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
  4456. {
  4457. ShmCharger->gun_info[gun_index].isCheckPowerConsumption = NO;
  4458. }
  4459. }
  4460. }
  4461. if(getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_LCD]) > TIMEOUT_SPEC_POWERSAVING_LCD)
  4462. {
  4463. if(ShmCharger->isLcdOn == ON)
  4464. {
  4465. DEBUG_INFO("LCD into power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_LCD]));
  4466. ShmCharger->isLcdOn = OFF;
  4467. }
  4468. }
  4469. else
  4470. {
  4471. if(ShmCharger->isLcdOn == OFF)
  4472. {
  4473. DEBUG_INFO("LCD exit power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_LCD]));
  4474. ShmCharger->isLcdOn = ON;
  4475. }
  4476. }
  4477. if(getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]) > TIMEOUT_SPEC_POWERSAVING_RFID)
  4478. {
  4479. if(gpio_get_value(GPIO_OUT_RST_RFID) == ON)
  4480. {
  4481. DEBUG_INFO("RFID into power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  4482. gpio_set_value(GPIO_OUT_RST_RFID, OFF);
  4483. }
  4484. }
  4485. else
  4486. {
  4487. if(gpio_get_value(GPIO_OUT_RST_RFID) == OFF)
  4488. {
  4489. DEBUG_INFO("RFID exit power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  4490. gpio_set_value(GPIO_OUT_RST_RFID, ON);
  4491. }
  4492. }
  4493. if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]) > TIMEOUT_SPEC_POWERSAVING_METER)
  4494. {
  4495. if(ShmCharger->gun_info[gun_index].isMeterOn)
  4496. {
  4497. DEBUG_INFO("Meter into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  4498. ShmCharger->gun_info[gun_index].isMeterOn = OFF;
  4499. }
  4500. }
  4501. else
  4502. {
  4503. if(!ShmCharger->gun_info[gun_index].isMeterOn)
  4504. {
  4505. DEBUG_INFO("Meter exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  4506. ShmCharger->gun_info[gun_index].isMeterOn = ON;
  4507. }
  4508. }
  4509. if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]) > TIMEOUT_SPEC_POWERSAVING_LED_STATUS)
  4510. {
  4511. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  4512. {
  4513. DEBUG_INFO("LED status into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
  4514. ShmCharger->gun_info[gun_index].isSleepOn = YES;
  4515. }
  4516. }
  4517. else
  4518. {
  4519. if(ShmCharger->gun_info[gun_index].isSleepOn == YES)
  4520. {
  4521. DEBUG_INFO("LED status exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
  4522. ShmCharger->gun_info[gun_index].isSleepOn = NO;
  4523. }
  4524. }
  4525. }
  4526. //==========================================
  4527. // Synchronize current rating value from MCU
  4528. //==========================================
  4529. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  4530. //==========================================
  4531. // Assign connector location index for OCPP
  4532. //==========================================
  4533. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  4534. //==========================================
  4535. // Synchronize present charging power
  4536. //==========================================
  4537. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  4538. {
  4539. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  4540. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000.0;
  4541. else
  4542. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4543. }
  4544. else
  4545. {
  4546. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  4547. {
  4548. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000.0) +
  4549. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2)/1000.0) +
  4550. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3)/1000.0));
  4551. }
  4552. else
  4553. {
  4554. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4555. }
  4556. }
  4557. //==========================================
  4558. // Check initialization "PASS" or "FAIL"
  4559. //==========================================
  4560. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4561. {
  4562. // Alarm event check
  4563. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0) || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
  4564. {
  4565. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  4566. {
  4567. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  4568. {
  4569. setChargerMode(gun_index, SYS_MODE_ALARM);
  4570. }
  4571. }
  4572. }
  4573. }
  4574. //==========================================
  4575. // Reservation request check
  4576. //==========================================
  4577. checkReservation(gun_index);
  4578. //==========================================
  4579. // Change availability check
  4580. //==========================================
  4581. checkAvailability(gun_index);
  4582. if(ShmCharger->gun_info[gun_index].isOperactive)
  4583. {
  4584. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  4585. {
  4586. setChargerMode(gun_index, SYS_MODE_IDLE);
  4587. }
  4588. }
  4589. else
  4590. {
  4591. if(isMode(gun_index, SYS_MODE_IDLE))
  4592. {
  4593. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  4594. }
  4595. }
  4596. //==========================================
  4597. // Unlock Connector signal check
  4598. //==========================================
  4599. checkUnlocker(gun_index);
  4600. //==========================================
  4601. // Connector process
  4602. //==========================================
  4603. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  4604. {
  4605. case SYS_MODE_BOOTING:
  4606. if(isModeChange(gun_index))
  4607. {
  4608. setLedMotion(gun_index,LED_ACTION_INIT);
  4609. //CSU Initialization & task spawn
  4610. if(Initialization(gun_index) != PASS)
  4611. {
  4612. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  4613. }
  4614. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4615. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4616. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4617. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  4618. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4619. refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
  4620. }
  4621. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  4622. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  4623. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  4624. {
  4625. // OCPP BootNotification info set
  4626. DEBUG_INFO("==========================================\n");
  4627. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  4628. DEBUG_INFO("==========================================\n");
  4629. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  4630. ocpp_boot_info_sync();
  4631. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  4632. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  4633. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  4634. DEBUG_INFO("==========================================\n");
  4635. DEBUG_INFO("========== Set Wifi information ==========\n");
  4636. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  4637. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  4638. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  4639. DEBUG_INFO("==========================================\n");
  4640. // Default Ethernet / Wifi / 4G to 1:disconnected
  4641. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  4642. {
  4643. case 'E':
  4644. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4645. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  4646. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  4647. break;
  4648. case 'W':
  4649. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4650. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  4651. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  4652. break;
  4653. case 'T':
  4654. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4655. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  4656. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  4657. break;
  4658. case 'D':
  4659. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4660. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  4661. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  4662. break;
  4663. }
  4664. // Default LCM brightness to 100
  4665. ShmCharger->isLcdOn = ON;
  4666. // Defaule led status to not sleep mode
  4667. ShmCharger->gun_info[gun_index].isSleepOn = NO;
  4668. // If Web Server OPCC URL is empty kill Module_OcppBackend
  4669. if((strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0) || ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  4670. {
  4671. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  4672. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  4673. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  4674. DEBUG_INFO("Local power sharing enable kill Module_OcppBackend...\n");
  4675. system ("pkill OcppBackend");
  4676. }
  4677. // If rotate switch equal zero, the system needs to change Debug mode
  4678. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  4679. setChargerMode(gun_index, SYS_MODE_DEBUG);
  4680. else
  4681. {
  4682. if((isGpioInitialized(GPIO_OUT_RST_RFID) == PASS) &&
  4683. (isGpioInitialized(GPIO_IN_WAKEUP) == PASS) &&
  4684. (isGpioInitialized(GPIO_OUT_RST_ETH) == PASS) &&
  4685. (isGpioInitialized(GPIO_OUT_RST_4G) == PASS) &&
  4686. (isGpioInitialized(GPIO_OUT_RST_QCA) == PASS))
  4687. {
  4688. //==================================================
  4689. // Get firmware version
  4690. //==================================================
  4691. sleep(3);
  4692. get_firmware_version(gun_index);
  4693. setChargerMode(gun_index, SYS_MODE_IDLE);
  4694. }
  4695. }
  4696. }
  4697. break;
  4698. case SYS_MODE_IDLE:
  4699. if(isModeChange(gun_index))
  4700. {
  4701. setLedMotion(gun_index,LED_ACTION_IDLE);
  4702. setRelay(gun_index, OFF);
  4703. setRequest(gun_index,OFF);
  4704. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4705. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  4706. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4707. ocpp_set_remotestart(gun_index, OFF);
  4708. ocpp_set_remotestop(gun_index, OFF);
  4709. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  4710. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  4711. presentChargedEnergyClear(gun_index);
  4712. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  4713. ocpp_set_unlocker_req(gun_index, OFF);
  4714. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  4715. // Response StopTransactionConf
  4716. ocpp_set_stoptransaction_conf(gun_index, OFF);
  4717. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  4718. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = OFF;
  4719. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4720. DB_Check_Record_Buf(localDb, gun_index);
  4721. }
  4722. // LED status in Idle mode
  4723. if(!ShmCharger->isAuthrizing)
  4724. {
  4725. if(ocpp_get_isRemoteStartWait())
  4726. {
  4727. ShmCharger->gun_info[gun_index].isSleepOn = OFF;
  4728. setLedMotion(gun_index, LED_ACTION_AUTHED);
  4729. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4730. checkHandshakeTime();
  4731. }
  4732. else
  4733. {
  4734. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus == ON)
  4735. {
  4736. if(ShmCharger->gun_info[gun_index].rfidReq == OFF)
  4737. {
  4738. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  4739. setLedMotion(gun_index, LED_ACTION_IDLE_BACKEND_CONNECTED);
  4740. else
  4741. setLedMotion(gun_index, LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP);
  4742. }
  4743. }
  4744. else
  4745. {
  4746. if(ShmCharger->gun_info[gun_index].rfidReq == OFF)
  4747. {
  4748. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  4749. setLedMotion(gun_index, LED_ACTION_IDLE_BACKEND_DISCONNECTED);
  4750. else
  4751. setLedMotion(gun_index, LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP);
  4752. }
  4753. }
  4754. }
  4755. }
  4756. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  4757. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  4758. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)) ||
  4759. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4760. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  4761. (ocpp_get_remotestart(gun_index) == ON) ||
  4762. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  4763. {
  4764. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4765. {
  4766. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4767. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  4768. DEBUG_INFO("Start Method : RFID...\n");
  4769. }
  4770. else if(ocpp_get_remotestart(gun_index))
  4771. {
  4772. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4773. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  4774. ocpp_copy_userid_from_remotestart(gun_index);
  4775. setSpeaker(ON, SPEAKER_SHORT);
  4776. DEBUG_INFO("Start Method : BACKEND...\n");
  4777. }
  4778. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  4779. {
  4780. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  4781. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Local;
  4782. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  4783. setSpeaker(ON, SPEAKER_SHORT);
  4784. DEBUG_INFO("Start Method : BLE...\n");
  4785. }
  4786. else
  4787. {
  4788. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  4789. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_NoAuthorization;
  4790. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  4791. setSpeaker(ON, SPEAKER_SHORT);
  4792. DEBUG_INFO("Start Method : FREE...\n");
  4793. }
  4794. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4795. ocpp_set_remotestart(gun_index, OFF);
  4796. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  4797. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  4798. // Get target current
  4799. ocpp_set_profile_req(gun_index, ON);
  4800. sleep(1);
  4801. checkChargingProfileLimit(gun_index);
  4802. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4803. }
  4804. else
  4805. {}
  4806. break;
  4807. case SYS_MODE_AUTHORIZING:
  4808. if(isModeChange(gun_index))
  4809. {
  4810. refreshStartTimer(&startTime[gun_index][TMR_IDX_AUTH]);
  4811. if(ocpp_isAuthorizeRemoteStart() && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND))
  4812. {
  4813. DEBUG_INFO("Remote start request authorize.\n");
  4814. ocpp_set_auth_req(ON, "ISO14443");
  4815. }
  4816. }
  4817. if(getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  4818. {
  4819. // Authorization timeout process.
  4820. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  4821. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  4822. sleep(3);
  4823. setChargerMode(gun_index, SYS_MODE_IDLE);
  4824. DEBUG_WARN("Authorize timeout !!!\n");
  4825. }
  4826. else
  4827. {
  4828. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  4829. {
  4830. case START_METHOD_BACKEND:
  4831. if(ocpp_isAuthorizeRemoteStart())
  4832. {
  4833. if(ocpp_get_auth_conf())
  4834. {
  4835. if(ocpp_get_auth_result(gun_index))
  4836. {
  4837. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4838. DEBUG_INFO("Authorize pass.\n");
  4839. setSpeaker(ON, SPEAKER_SHORT);
  4840. setLedMotion(gun_index, LED_ACTION_RFID_PASS);
  4841. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4842. }
  4843. else
  4844. {
  4845. DEBUG_INFO("Authorize fail.\n");
  4846. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  4847. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  4848. sleep(3);
  4849. setChargerMode(gun_index, SYS_MODE_IDLE);
  4850. }
  4851. ocpp_set_auth_conf(OFF);
  4852. }
  4853. }
  4854. else
  4855. {
  4856. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4857. }
  4858. break;
  4859. case START_METHOD_RFID:
  4860. case START_METHOD_BLE:
  4861. case START_METHOD_FREE:
  4862. default:
  4863. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4864. break;
  4865. }
  4866. }
  4867. break;
  4868. case SYS_MODE_PREPARING:
  4869. if(isModeChange(gun_index))
  4870. {
  4871. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4872. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  4873. setLedMotion(gun_index, LED_ACTION_AUTHED);
  4874. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4875. if(ShmCharger->isCcsEnable)refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  4876. }
  4877. // If control pilot detect Bx, skip watch dog time out.
  4878. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) ||
  4879. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  4880. {
  4881. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  4882. if(!getRequest(gun_index))
  4883. {
  4884. setRequest(gun_index, ON);
  4885. setLedMotion(gun_index, LED_ACTION_CONNECTED);
  4886. DEBUG_INFO("Set Request On.\n");
  4887. }
  4888. switch(ShmCharger->gun_info[gun_index].ccsHandshakeState)
  4889. {
  4890. case HANDSHAKE_DUTY_5:
  4891. break;
  4892. case HANDSHAKE_DUTY_5_CHECK:
  4893. refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  4894. DEBUG_INFO("HLC slac handshake start.\n");
  4895. break;
  4896. case HANDSHAKE_CCS:
  4897. //CCS handshake timeout
  4898. if(getDiffSecNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  4899. {
  4900. DEBUG_INFO("HLC %d secs slac handshake timeout.\n", TIMEOUT_SPEC_BS_HLC_HANDSHAKE);
  4901. }
  4902. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  4903. {
  4904. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  4905. }
  4906. //CCS status check
  4907. if((16 < ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 254))
  4908. {
  4909. DEBUG_INFO("Enter HLC Mode charging.\n");
  4910. }
  4911. break;
  4912. case HANDSHAKE_CP_STATE_E:
  4913. //if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  4914. {
  4915. // restore normal CP PWM duty
  4916. // Determine max charging current to MCU
  4917. DEBUG_INFO("Determine max charging current to MCU.\n");
  4918. ocpp_set_profile_req(gun_index, ON);
  4919. sleep(1);
  4920. checkChargingProfileLimit(gun_index);
  4921. }
  4922. break;
  4923. case HANDSHAKE_SET_MAX_CURRENT:
  4924. case HANDSHAKE_BS_MODE:
  4925. DEBUG_INFO("Enter BS Mode charging.\n");
  4926. checkChargingProfileLimit(gun_index);
  4927. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  4928. setRelay(gun_index,ON);
  4929. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) || (ShmCharger->gun_info[gun_index].targetCurrent == 0))
  4930. {
  4931. ocpp_set_unlocker_req(gun_index, OFF);
  4932. presentChargedEnergyClear(gun_index);
  4933. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  4934. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  4935. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  4936. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  4937. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  4938. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  4939. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  4940. else
  4941. {
  4942. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  4943. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  4944. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  4945. }
  4946. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4947. ocpp_copy_userid_to_starttransaction(gun_index);
  4948. ocpp_set_starttransaction_req(gun_index, ON);
  4949. refreshStartTimer(&startChargingTime[gun_index]);
  4950. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4951. }
  4952. break;
  4953. case HANDSHAKE_HLC_MODE:
  4954. if(ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)
  4955. {
  4956. setRelay(gun_index,ON);
  4957. ShmCharger->gun_info[gun_index].isCCSStartTransation = ON;
  4958. }
  4959. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) &&
  4960. (ShmCharger->gun_info[gun_index].isCCSStartTransation == ON))
  4961. {
  4962. ocpp_set_unlocker_req(gun_index, OFF);
  4963. presentChargedEnergyClear(gun_index);
  4964. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  4965. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  4966. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  4967. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  4968. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  4969. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  4970. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  4971. else
  4972. {
  4973. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  4974. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  4975. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  4976. }
  4977. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4978. ocpp_copy_userid_to_starttransaction(gun_index);
  4979. ocpp_set_starttransaction_req(gun_index, ON);
  4980. ShmCharger->gun_info[gun_index].isCCSStartTransation = OFF;
  4981. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4982. refreshStartTimer(&startChargingTime[gun_index]);
  4983. refreshStartTimer(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  4984. }
  4985. //120 sec timeout
  4986. if(getDiffSecNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  4987. {
  4988. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  4989. if(getDiffSecNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > (TIMEOUT_SPEC_CCS_HANDSHAKE+5))
  4990. {
  4991. DEBUG_INFO("CCS 120 secs handshake timeout, change to BS Mode...\n");
  4992. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  4993. {
  4994. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  4995. }
  4996. }
  4997. }
  4998. break;
  4999. default:
  5000. break;
  5001. }
  5002. }
  5003. else if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode)
  5004. {
  5005. DEBUG_INFO("Enter Socket-E Mode charging.\n");
  5006. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_SOCKETE;
  5007. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  5008. setRelay(gun_index,ON);
  5009. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5010. ocpp_copy_userid_to_starttransaction(gun_index);
  5011. ocpp_set_starttransaction_req(gun_index, ON);
  5012. refreshStartTimer(&startChargingTime[gun_index]);
  5013. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5014. }
  5015. // Unplug charging gun to Idle mode
  5016. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  5017. {
  5018. DEBUG_INFO("Charging gun is plugged before.\n");
  5019. setChargerMode(gun_index, SYS_MODE_IDLE);
  5020. }
  5021. // Use RFID card to stop handshaking
  5022. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchPresentUser(gun_index))
  5023. {
  5024. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  5025. setChargerMode(gun_index, SYS_MODE_IDLE);
  5026. }
  5027. else
  5028. {
  5029. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5030. }
  5031. checkHandshakeTime();
  5032. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  5033. {
  5034. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  5035. DEBUG_INFO("Handshaking timeout...%d\n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  5036. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5))
  5037. {
  5038. setChargerMode(gun_index, SYS_MODE_IDLE);
  5039. }
  5040. }
  5041. break;
  5042. case SYS_MODE_CHARGING:
  5043. if(isModeChange(gun_index))
  5044. {
  5045. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5046. //ftime(&startChargingTime[gun_index]);
  5047. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  5048. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5049. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  5050. ocpp_set_auth_req(OFF);
  5051. ocpp_reset_smartcharging_profileId(gun_index);
  5052. ocpp_set_profile_req(gun_index, ON);
  5053. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5054. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5055. }
  5056. //if time up, clear CCS MSG count
  5057. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  5058. (getDiffSecNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
  5059. {
  5060. refreshStartTimer(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  5061. if(ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat > 0)
  5062. {
  5063. ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat = 0;
  5064. }
  5065. else
  5066. {
  5067. DEBUG_INFO("CCS could not get MSG from car.\n");
  5068. }
  5069. }
  5070. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  5071. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  5072. ocpp_get_remotestop(gun_index) ||
  5073. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  5074. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  5075. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  5076. ocpp_get_reset_req() ||
  5077. ocpp_get_unlocker_req(gun_index) ||
  5078. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  5079. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)) ||
  5080. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE) ||
  5081. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE))
  5082. {
  5083. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5084. }
  5085. else
  5086. {
  5087. // Charging session info calculation
  5088. setLedMotion(gun_index,LED_ACTION_CHARGING);
  5089. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  5090. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5091. else
  5092. {
  5093. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  5094. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  5095. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  5096. }
  5097. refreshStartTimer(&endChargingTime[gun_index]);
  5098. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  5099. presentChargedEnergyUpdate(gun_index);
  5100. // Response StartTransactionConf
  5101. ocpp_set_starttransaction_conf(gun_index, OFF);
  5102. // Charging profile preparation
  5103. if(getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  5104. {
  5105. if(!ocpp_get_profile_conf(gun_index))
  5106. {
  5107. ocpp_set_profile_req(gun_index, ON);
  5108. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5109. }
  5110. else
  5111. {
  5112. ocpp_set_profile_conf(gun_index, OFF);
  5113. }
  5114. }
  5115. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  5116. checkChargingProfileLimit(gun_index);
  5117. // Charging session target current check if OCPP disconnect and power sharing server connected
  5118. if(!ocpp_get_connection_status() && ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharging)
  5119. {
  5120. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  5121. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent;
  5122. else
  5123. {
  5124. if(ShmCharger->gun_info[gun_index].targetCurrent != 0)
  5125. DEBUG_WARN("Disconnect from power sharing server, target current set to 0.\n");
  5126. ShmCharger->gun_info[gun_index].targetCurrent = 0;
  5127. }
  5128. }
  5129. // Charging session local limit condition check
  5130. if(ocpp_get_connection_status())
  5131. {
  5132. // On-line max condition check
  5133. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  5134. {
  5135. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5136. {
  5137. setRelay(gun_index, OFF);
  5138. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5139. }
  5140. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5141. {
  5142. setRelay(gun_index, OFF);
  5143. }
  5144. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5145. 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));
  5146. }
  5147. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  5148. {
  5149. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5150. {
  5151. setRelay(gun_index, OFF);
  5152. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5153. }
  5154. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5155. {
  5156. setRelay(gun_index, OFF);
  5157. }
  5158. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5159. 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));
  5160. }
  5161. else
  5162. {
  5163. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5164. {
  5165. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5166. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  5167. setRelay(gun_index, ON);
  5168. else
  5169. setRelay(gun_index, OFF);
  5170. }
  5171. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5172. {
  5173. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  5174. setRelay(gun_index, ON);
  5175. }
  5176. }
  5177. }
  5178. else
  5179. {
  5180. // Off-line max condition check
  5181. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  5182. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  5183. {
  5184. 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))))
  5185. {
  5186. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5187. {
  5188. setRelay(gun_index, OFF);
  5189. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5190. }
  5191. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5192. {
  5193. setRelay(gun_index, OFF);
  5194. }
  5195. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5196. 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));
  5197. }
  5198. 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))))
  5199. {
  5200. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5201. {
  5202. setRelay(gun_index, OFF);
  5203. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5204. }
  5205. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5206. {
  5207. setRelay(gun_index, OFF);
  5208. }
  5209. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5210. 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));
  5211. }
  5212. else
  5213. {
  5214. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5215. {
  5216. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5217. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  5218. setRelay(gun_index, ON);
  5219. else
  5220. setRelay(gun_index, OFF);
  5221. }
  5222. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5223. {
  5224. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  5225. setRelay(gun_index, ON);
  5226. }
  5227. }
  5228. }
  5229. else
  5230. {
  5231. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state > 0)
  5232. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  5233. setRelay(gun_index, OFF);
  5234. }
  5235. }
  5236. // Debug information
  5237. if(getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_REFRESH_CHARGING_INFO)
  5238. {
  5239. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  5240. DB_Update_Record_Buf(localDb, gun_index);
  5241. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  5242. }
  5243. }
  5244. break;
  5245. case SYS_MODE_TERMINATING:
  5246. if(isModeChange(gun_index))
  5247. {
  5248. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  5249. {
  5250. setLedMotion(gun_index, LED_ACTION_STOP);
  5251. }
  5252. else
  5253. {}
  5254. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  5255. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5256. }
  5257. refreshStartTimer(&endChargingTime[gun_index]);
  5258. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  5259. {
  5260. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  5261. }
  5262. // End authorize pass
  5263. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) &&isMatchStartUser(gun_index)) ||
  5264. ((ShmCharger->gun_info[gun_index].rfidReq == ON) && ocpp_get_auth_result(YES, gun_index)) ||
  5265. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) && (ShmCharger->gun_info[gun_index].rfidReq == ON)) ||
  5266. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (ShmCharger->gun_info[gun_index].rfidReq == ON)) ||
  5267. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  5268. ocpp_get_remotestop(gun_index) ||
  5269. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  5270. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  5271. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  5272. ocpp_get_reset_req() ||
  5273. ocpp_get_unlocker_req(gun_index) ||
  5274. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  5275. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  5276. {
  5277. //setRelay(gun_index, OFF);
  5278. setLedMotion(gun_index, LED_ACTION_STOP);
  5279. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5280. {
  5281. sleep(1);
  5282. checkStopReason(gun_index);
  5283. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5284. }
  5285. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5286. {
  5287. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5288. //Cancel CCS task negotiating
  5289. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5290. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5291. setRelay(gun_index,OFF);
  5292. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5293. }
  5294. else
  5295. {
  5296. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5297. }
  5298. }
  5299. else
  5300. {
  5301. // If the charger stops charging, the led should be stoped blink
  5302. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  5303. {
  5304. setLedMotion(gun_index, LED_ACTION_STOP);
  5305. }
  5306. // Charging profile preparation
  5307. if(getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  5308. {
  5309. if(!ocpp_get_profile_conf(gun_index))
  5310. {
  5311. ocpp_set_profile_req(gun_index, ON);
  5312. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5313. }
  5314. else
  5315. {
  5316. ocpp_set_profile_conf(gun_index, OFF);
  5317. }
  5318. }
  5319. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  5320. checkChargingProfileLimit(gun_index);
  5321. if(!ocpp_get_auth_result(YES, gun_index))
  5322. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5323. 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)) &&
  5324. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  5325. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  5326. !ocpp_get_remotestop(gun_index) &&
  5327. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  5328. !ocpp_get_reset_req() &&
  5329. ocpp_get_starttransaction_result(gun_index) &&
  5330. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  5331. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  5332. !(!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)))) &&
  5333. !(!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)))) &&
  5334. (((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))
  5335. )
  5336. {
  5337. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5338. }
  5339. }
  5340. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  5341. (ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) &&
  5342. (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  5343. {
  5344. DEBUG_INFO("Set PWM duty 100%% go to SYS_MODE_TERMINATING.\n");
  5345. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  5346. sleep(1);
  5347. checkStopReason(gun_index);
  5348. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5349. }
  5350. break;
  5351. case SYS_MODE_COMPLETE:
  5352. if(isModeChange(gun_index))
  5353. {
  5354. setLedMotion(gun_index, LED_ACTION_STOP);
  5355. sleep(6);
  5356. setRelay(gun_index, OFF);
  5357. setRequest(gun_index, OFF);
  5358. sleep(13);
  5359. }
  5360. if(((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)) ||
  5361. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && (!ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)) ||
  5362. ocpp_get_reset_req())
  5363. {
  5364. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5365. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  5366. ocpp_set_remotestop(gun_index, OFF);
  5367. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  5368. DB_Insert_Record(localDb, gun_index);
  5369. setChargerMode(gun_index, SYS_MODE_IDLE);
  5370. }
  5371. break;
  5372. case SYS_MODE_ALARM:
  5373. setLedMotion(gun_index,LED_ACTION_ALARM);
  5374. setRelay(gun_index, OFF);
  5375. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0) && !ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
  5376. {
  5377. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  5378. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  5379. {
  5380. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  5381. }
  5382. else
  5383. {
  5384. setChargerMode(gun_index, SYS_MODE_IDLE);
  5385. }
  5386. }
  5387. break;
  5388. case SYS_MODE_FAULT:
  5389. if(isModeChange(gun_index))
  5390. {}
  5391. break;
  5392. case SYS_MODE_MAINTAIN:
  5393. if(isModeChange(gun_index))
  5394. {
  5395. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5396. }
  5397. break;
  5398. case SYS_MODE_UPDATE:
  5399. if(isModeChange(gun_index))
  5400. {
  5401. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5402. }
  5403. //======================================
  5404. // Check local upgrade firmware request
  5405. //======================================
  5406. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  5407. {
  5408. DEBUG_INFO("Firmware local upgrading...\n");
  5409. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  5410. if(upgrade_check() == PASS)
  5411. {
  5412. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  5413. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  5414. ShmCharger->isUpdateSuccess = YES;
  5415. DEBUG_INFO("Local update success.\n");
  5416. }
  5417. else
  5418. {
  5419. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  5420. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  5421. ShmCharger->isUpdateSuccess = NO;
  5422. DEBUG_INFO("Local update fail.\n");
  5423. }
  5424. }
  5425. else if(ocpp_get_update_firmware_req())
  5426. {
  5427. //======================================
  5428. // Check remote upgrade firmware request
  5429. //======================================
  5430. checkRemoteUpgradeStatus();
  5431. }
  5432. else
  5433. {
  5434. //======================================
  5435. // Upgrade complete reboot system
  5436. //======================================
  5437. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((modelnameInfo.GetGunCount>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES) && !ShmCharger->isUpgradeLcmReq)
  5438. {
  5439. if(ShmCharger->isUpdateSuccess == YES)
  5440. {
  5441. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5442. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5443. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5444. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5445. DEBUG_WARN("Firmware upgrade success.\n");
  5446. sleep(5);
  5447. DEBUG_INFO("Firmware upgraded, reboot...\n");
  5448. system("reboot -f");
  5449. sleep(5);
  5450. system("reboot -f");
  5451. }
  5452. else
  5453. {
  5454. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5455. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5456. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5457. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5458. DEBUG_WARN("Firmware upgrade fail.\n");
  5459. sleep(5);
  5460. system("rm -rvf /mnt/* ");
  5461. close(wtdFd);
  5462. system("/usr/bin/run_evse_restart.sh");
  5463. }
  5464. }
  5465. }
  5466. break;
  5467. case SYS_MODE_RESERVATION:
  5468. if(isModeChange(gun_index))
  5469. {}
  5470. setLedMotion(gun_index,LED_ACTION_RESERVATION_MODE);
  5471. if(isReservationExpired(gun_index))
  5472. {
  5473. DEBUG_INFO("Reservation: Time's up...\n");
  5474. setChargerMode(gun_index, SYS_MODE_IDLE);
  5475. }
  5476. else if(ocpp_get_cancelreservation_req(gun_index))
  5477. {
  5478. DEBUG_INFO("Reservation: Cancel reservation...\n");
  5479. setChargerMode(gun_index, SYS_MODE_IDLE);
  5480. ocpp_set_cancelreservation_req(gun_index, OFF);
  5481. }
  5482. else
  5483. {
  5484. // Check is there RFID or back end request start
  5485. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  5486. ocpp_get_remotestart(gun_index))
  5487. {
  5488. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  5489. {
  5490. if(ocpp_compare_reserve_id_with_user(gun_index))
  5491. {
  5492. DEBUG_INFO("Start Method in reservation : RFID...\n");
  5493. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  5494. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  5495. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  5496. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5497. }
  5498. else
  5499. {
  5500. setLedMotion(gun_index,LED_ACTION_IDLE);
  5501. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5502. DEBUG_INFO("It's not reserve user id.\n");
  5503. }
  5504. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5505. }
  5506. else if(ocpp_get_remotestart(gun_index))
  5507. {
  5508. if(ocpp_compare_reserve_id_with_remote_user(gun_index))
  5509. {
  5510. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  5511. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  5512. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  5513. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5514. }
  5515. ocpp_set_remotestop(gun_index, OFF);
  5516. }
  5517. }
  5518. }
  5519. break;
  5520. case SYS_MODE_BOOKING:
  5521. if(isModeChange(gun_index))
  5522. {}
  5523. break;
  5524. case SYS_MODE_DEBUG:
  5525. if(isModeChange(gun_index))
  5526. {
  5527. setLedMotion(gun_index,LED_ACTION_DEBUG);
  5528. }
  5529. break;
  5530. }
  5531. }
  5532. }
  5533. // System watch dog reset
  5534. write(wtdFd, "a", 1);
  5535. usleep(50000);
  5536. }
  5537. return FAIL;
  5538. }