main.c 296 KB

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