main.c 228 KB

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