main.c 380 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <stdarg.h>
  15. #include <stdio.h> /*標準輸入輸出定義*/
  16. #include <stdlib.h> /*標準函數庫定義*/
  17. #include <unistd.h> /*Unix 標準函數定義*/
  18. #include <fcntl.h> /*檔控制定義*/
  19. #include <termios.h> /*PPSIX 終端控制定義*/
  20. #include <errno.h> /*錯誤號定義*/
  21. #include <string.h>
  22. #include <stdint.h>
  23. #include <time.h>
  24. #include <ctype.h>
  25. #include <ifaddrs.h>
  26. #include <math.h>
  27. #include <stdbool.h>
  28. #include <dirent.h>
  29. #include "../../define.h"
  30. #include "CheckSystemTask.h"
  31. #include "Config.h"
  32. #include "timeout.h"
  33. #define DEBUG_ALSTON 0
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. #define MODELNAME_FAIL 0
  38. #define BUFFER_SIZE 128
  39. #define YES 1
  40. #define NO 0
  41. #define NORMAL 0
  42. #define ABNORMAL 1
  43. #define EQUAL 0
  44. #define BTN_RELEASE 0
  45. #define BTN_PRESS 1
  46. #define MAX_BUF 64
  47. //#define MtdBlockSize 0x600000
  48. #define MtdBlockSize 0x300000
  49. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  50. #define UPGRADE_FAN 0x02
  51. #define UPGRADE_RB 0x03
  52. #define UPGRADE_PRI 0x04
  53. #define UPGRADE_AC 0x05
  54. #define UPGRADE_LED 0x06
  55. #define SYSTEM_MIN_VOL 80
  56. #define MIN_OUTPUT_CUR 0
  57. #define AC_OUTPUT_VOL 220
  58. #define NO_RESERVE -1
  59. #define NO_DEFINE 255
  60. #define DEFAULT_AC_INDEX 2
  61. #define PSU_MIN_CUR 100
  62. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  63. #define NETWORK_DB_FILE "/Storage/EventLog/Eventlog.db"
  64. #define GUN_OTP_VALUE 150
  65. #define GUN_OTP_RECOVERY 140
  66. #define UNDEFINED_TEMP 255
  67. #define uSEC_VAL 1000000
  68. #define SELFTEST_TIMEOUT 60
  69. #define AUTHORIZE_TIMEOUT 15
  70. #define AUTHORIZE_COMP_TIMEOUT 3
  71. #define AUTHORIZE_FAIL_TIMEOUT 3
  72. #define AUTHORIZE_STOP_TIMEOUT 30
  73. #define RETURN_TO_CHARGING_PAGE 30
  74. #define GUN_PREPARE_TIMEOUT 30
  75. #define GUN_EV_WAIT_TIMEOUT 120
  76. #define GUN_EVSE_WAIT_TIMEOUT 60
  77. #define GUN_COMP_WAIT_TIMEOUT 3
  78. #define GUN_PRECHARGING_TIMEOUT 60
  79. #define TPC_PLUG_DETECT_TIMEOUT 60 //LW_tpc plug detect
  80. #define TIMEOUT_SPEC_HANDSHAKING 180000
  81. #define CERT_PNC_DIR_FILE "/Storage/OCPP/certPnCAuth.pem"
  82. #define CERT_PNC_CCS_DIR_FILE "/mnt/P-256_Tier1_Valid_EVSE_Cert-privkey.pem"
  83. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  84. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  85. int whileLoopTime = 10000; // 10 ms
  86. int wtdFd = -1;
  87. byte _authorizeIndex = NO_DEFINE;
  88. bool IsAuthorizingMode();
  89. void ClearAuthorizedFlag();
  90. bool isDetectPlugin();
  91. void ClearDetectPluginFlag();
  92. int mystrcmp(unsigned char *p1, unsigned char *p2);
  93. void CheckTask();
  94. long long DiffTimebWithNow(struct timeb ST);
  95. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  96. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  97. void ChargingTerminalProcess(byte gunIndex);
  98. void ChkPrimaryStatus();
  99. void StartSystemTimeoutDet(unsigned char flag);
  100. void StopSystemTimeoutDet();
  101. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  102. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  103. int StoreLogMsg_1(const char *fmt, ...);
  104. int GetTimeoutValue(struct timespec *startTime);
  105. void gpio_set_value(unsigned int gpio, unsigned int value);
  106. void PRINTF_FUNC(char *string, ...);
  107. void ChangeGunSelectByIndex(byte sel);
  108. void ChargingAlarmProcess(byte gunIndex);
  109. void CheckSystemErrorFunction(byte index);
  110. int opcc_chk_reserve_expired(byte gunIndex);
  111. void ocpp_process_start();
  112. void ocpp_auto_response_BootNotification();
  113. void ocpp_auto_response_StartTransationConf(byte gunIndex);
  114. void ocpp_auto_response_StopTransationConf(byte gunIndex);
  115. bool ocpp_chk_authrization_cmd();
  116. void ocpp_chk_reset_cmd(byte isforce);
  117. void ocpp_chk_reserved_cmd(byte gunIndex);
  118. void ocpp_chk_availability_cmd(byte gunIndex);
  119. void ocpp_chk_unlock_cmd(byte gunIndex);
  120. bool ocpp_chk_remoteStop_cmd(byte gunIndex);
  121. void ocpp_chk_remoteStart_cmd();
  122. void ocpp_chk_update_cmd();
  123. bool ocpp_chk_invalid_id_cmd(byte gunIndex);
  124. void ocpp_set_startTransation_cmd(byte gunIndex);
  125. void ocpp_set_stopTransation_cmd(byte gunIndex);
  126. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString);
  127. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason);
  128. void ocpp_set_authorizeReq_cmd(byte value);
  129. void ocpp_set_authorizeConf_cmd(byte value);
  130. void ocpp_set_errCode_cmd(byte gunIndex);
  131. void ocpp_clear_errorCode_cmd(byte gunIndex);
  132. void ocpp_clear_idTag_cmd(byte gun_index);
  133. void ocpp_clear_stopReason(byte gun_index);
  134. bool ocpp_get_authorize_conf();
  135. unsigned short _ocpp_get_connect_timeout();
  136. void ocpp_chargingProfile_process(byte gun_index);
  137. void ocpp_set_startTransationPH_cmd(byte gunIndex);
  138. void ocpp_auto_response_StartTransationConfPH(byte gunIndex);
  139. void ocpp_set_stopTransationPH_cmd(byte gunIndex);
  140. void ocpp_auto_response_StopTransationConfPH(byte gunIndex);
  141. void ocpp_copy_stopReason2maintainServer(byte gunIndex);
  142. void ocpp_sub_set_remote_start_transaction_req(byte gunIndex, bool result);
  143. void RecordAlarmCode(byte gunIndex, char *code);
  144. void ReleaseSysAlarmCode(byte gunIndex);
  145. void ReviewCriticalAlarm();
  146. int DB_Open(sqlite3 *db);
  147. int DB_Insert_Record(sqlite3 *db, int gun_index);
  148. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  149. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  150. void InitialDHCP();
  151. int GetStartScheduleTime(unsigned char *time);
  152. void KillAllTask();
  153. void CheckSystemTaskAlive();
  154. void CheckConnectorOTP(byte _index);
  155. void _DetectTpcPlugInTimeout();
  156. int ElapsedTime(uint8_t *start);
  157. bool CheckTheParentId(byte gun_index);
  158. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  159. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  160. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  161. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  162. struct StatusCodeData *ShmStatusCodeData;
  163. struct PsuData *ShmPsuData;
  164. struct CHAdeMOData *ShmCHAdeMOData;
  165. struct GBTData *ShmGBTData;
  166. struct CcsData *ShmCcsData;
  167. struct PrimaryMcuData *ShmPrimaryMcuData;
  168. struct FanModuleData *ShmFanModuleData;
  169. struct RelayModuleData *ShmRelayModuleData;
  170. struct LedModuleData *ShmLedModuleData;
  171. struct OCPP16Data *ShmOCPP16Data;
  172. struct OCPP20Data *ShmOCPP20Data;
  173. struct OCPP16Data *ShmOCPP16DataPH;
  174. struct MeterInformation *ShmCsuMeterData;
  175. struct DcCommonInformation *ShmDcCommonData;
  176. struct SmartBoxData *ShmSmartBoxData;
  177. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  178. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  179. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  180. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  181. int _presentChargingTimeBuf[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  182. struct timespec _startTransation_time[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  183. float _ChargingProfilePwBuffer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  184. float _ChargingMaxProfilePwBuffer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  185. float _AcChargingProfileCurBuffer;
  186. bool _isSendStartTransaction[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  187. GunErr errCollect;
  188. struct timespec _autoRuntestTime;
  189. byte _isTestRun = NO;
  190. // for initial index to check EV board type is correct
  191. byte _gunIndex = 0;
  192. byte _acgunIndex = 0;
  193. byte _chademoIndex = 0;
  194. byte _ccsIndex = 0;
  195. byte _gb_Index = 0;
  196. byte _ac_Index = 0;
  197. byte bd0_1_status = 0;
  198. byte bd0_2_status = 0;
  199. byte bd1_1_status = 0;
  200. byte bd1_2_status = 0;
  201. bool isCardScan = false;
  202. bool isModelNameMatch = true;
  203. byte _chargingCount;
  204. int rfidFd = -1;
  205. char* rfidPortName = "/dev/ttyS2";
  206. char* fwVersion = "V3.02.00.0000.00";
  207. sqlite3 *localDb;
  208. bool isDb_ready;
  209. sqlite3 *networkDb;
  210. char MyDefaultPriceString[501];
  211. byte reset4gStep = RESET_4G_STEP_NONE;
  212. // 避免槍溫偵測誤判
  213. byte gunTempAllowCount[2] = {0, 0};
  214. // RFID 驗證機制
  215. byte _verifyMechanism = VERIFY_MECHANISM_RFID;
  216. bool isPaymentBusy = false;
  217. // WIFI Enable / Disable
  218. byte _isUsingWifi;
  219. // 4G Enable / Disable
  220. byte _isUsing4G;
  221. //================================================
  222. // initial can-bus
  223. //================================================
  224. int InitCanBus()
  225. {
  226. int s0,nbytes;
  227. struct timeval tv;
  228. struct ifreq ifr0;
  229. struct sockaddr_can addr0;
  230. system("/sbin/ip link set can0 down");
  231. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  232. system("/sbin/ip link set can0 up");
  233. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  234. tv.tv_sec = 0;
  235. tv.tv_usec = 10000;
  236. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  237. {
  238. #ifdef SystemLogMessage
  239. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  240. #endif
  241. }
  242. nbytes=40960;
  243. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  244. {
  245. #ifdef SystemLogMessage
  246. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  247. #endif
  248. }
  249. nbytes=40960;
  250. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  251. {
  252. #ifdef SystemLogMessage
  253. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  254. #endif
  255. }
  256. strcpy(ifr0.ifr_name, "can0" );
  257. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  258. addr0.can_family = AF_CAN;
  259. addr0.can_ifindex = ifr0.ifr_ifindex;
  260. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  261. return s0;
  262. }
  263. //================================================
  264. // initial uart port
  265. //================================================
  266. char *_priPortName = "/dev/ttyS1";
  267. char *_485PortName = "/dev/ttyS5";
  268. int InitComPort(byte target)
  269. {
  270. int fd;
  271. struct termios tios;
  272. if(target == UPGRADE_PRI)
  273. fd = open(_priPortName, O_RDWR);
  274. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  275. fd = open(_485PortName, O_RDWR);
  276. if(fd<=0)
  277. {
  278. #ifdef SystemLogMessage
  279. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  280. #endif
  281. return -1;
  282. }
  283. ioctl (fd, TCGETS, &tios);
  284. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  285. tios.c_lflag = 0;
  286. tios.c_iflag = 0;
  287. tios.c_oflag = 0;
  288. tios.c_cc[VMIN]=0;
  289. tios.c_cc[VTIME]=(unsigned char)5;
  290. tios.c_lflag=0;
  291. tcflush(fd, TCIFLUSH);
  292. ioctl (fd, TCSETS, &tios);
  293. return fd;
  294. }
  295. //=================================
  296. // Common routine
  297. //=================================
  298. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  299. {
  300. strncpy(dest, src + start, cnt);
  301. dest[cnt] = 0;
  302. }
  303. int InitWatchDog()
  304. {
  305. int fd;
  306. int timeout = 180;
  307. system("/usr/bin/fuser -k /dev/watchdog");
  308. sleep(1);
  309. system("echo V > /dev/watchdog");
  310. sleep(1);
  311. fd = open("/dev/watchdog", O_RDWR);
  312. if(fd <= 0)
  313. {
  314. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  315. }
  316. ioctl(fd, _IOWR('W', 6, int), &timeout);
  317. return fd;
  318. }
  319. void CreateWatchdog()
  320. {
  321. if (!DEBUG_ALSTON && wtdFd == -1)
  322. {
  323. PRINTF_FUNC("===== Create crazy dog ===== \n");
  324. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  325. {
  326. wtdFd = InitWatchDog();
  327. if (wtdFd < 0)
  328. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  329. }
  330. }
  331. }
  332. void CloseWatchDog()
  333. {
  334. if (wtdFd > 0)
  335. {
  336. close(wtdFd);
  337. sleep(1);
  338. system("/usr/bin/fuser -k /dev/watchdog");
  339. sleep(1);
  340. system("echo V > /dev/watchdog");
  341. sleep(1);
  342. wtdFd = -1;
  343. }
  344. }
  345. int StoreLogMsg_1(const char *fmt, ...)
  346. {
  347. char Buf[4096+256];
  348. char buffer[4096];
  349. va_list args;
  350. struct timeb SeqEndTime;
  351. struct tm *tm;
  352. va_start(args, fmt);
  353. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  354. va_end(args);
  355. memset(Buf,0,sizeof(Buf));
  356. ftime(&SeqEndTime);
  357. SeqEndTime.time = time(NULL);
  358. tm=localtime(&SeqEndTime.time);
  359. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES ||
  360. DEBUG_ALSTON)
  361. {
  362. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  363. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  364. printf("%s \n", Buf);
  365. }
  366. else
  367. {
  368. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog_%s_Log",
  369. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  370. buffer,
  371. tm->tm_year+1900,tm->tm_mon+1,
  372. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  373. system(Buf);
  374. }
  375. return rc;
  376. }
  377. //unsigned long GetTimeoutValue(struct timeval _sour_time)
  378. //{
  379. // struct timeval _end_time;
  380. // gettimeofday(&_end_time, NULL);
  381. //
  382. // return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  383. //}
  384. int GetTimeoutValue(struct timespec *startTime)
  385. {
  386. struct timespec endTime;
  387. clock_gettime(CLOCK_MONOTONIC_COARSE, &endTime);
  388. return endTime.tv_sec - startTime->tv_sec;
  389. }
  390. void GetTimespecFunc(struct timespec *time)
  391. {
  392. clock_gettime(CLOCK_MONOTONIC_COARSE, time);
  393. }
  394. int mystrcmp(unsigned char *p1, unsigned char *p2)
  395. {
  396. while(*p1==*p2)
  397. {
  398. if(*p1=='\0' || *p2=='\0')
  399. break;
  400. p1++;
  401. p2++;
  402. }
  403. if(*p1=='\0' && *p2=='\0')
  404. return(PASS);
  405. else
  406. return(FAIL);
  407. }
  408. int DiffTimeb(struct timeb ST, struct timeb ET)
  409. {
  410. //return milli-second
  411. unsigned int StartTime,StopTime;
  412. StartTime=(unsigned int)ST.time;
  413. StopTime=(unsigned int)ET.time;
  414. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  415. return (StopTime-StartTime);
  416. }
  417. int DiffTimebWithNowSec(struct timeb ST)
  418. {
  419. //return milli-second
  420. struct timeb ET;
  421. unsigned int StartTime,StopTime;
  422. ftime(&ET);
  423. StartTime=(unsigned int)ST.time;
  424. StopTime=(unsigned int)ET.time;
  425. return (StopTime-StartTime);
  426. }
  427. int CheckTimeOut(uint8_t *start)
  428. {
  429. int result = YES;
  430. struct ParsingResult
  431. {
  432. int result;
  433. int scanedElement;
  434. int year;
  435. int month;
  436. int mday;
  437. int hour;
  438. int min;
  439. int sec;
  440. int tz_hour;
  441. int tz_min;
  442. float minSec;
  443. }parsingResult;
  444. struct tm tmStart;
  445. struct timeb tbStart;
  446. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  447. if(strstr((char*)start, ".") != NULL)
  448. {
  449. // Original data with mini second
  450. if(strstr((char*)start, "Z") != NULL)
  451. {
  452. // Original data with Z
  453. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  454. &parsingResult.year,
  455. &parsingResult.month,
  456. &parsingResult.mday,
  457. &parsingResult.hour,
  458. &parsingResult.min,
  459. &parsingResult.sec,
  460. &parsingResult.minSec);
  461. }
  462. else
  463. {
  464. // Original data without Z
  465. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  466. &parsingResult.year,
  467. &parsingResult.month,
  468. &parsingResult.mday,
  469. &parsingResult.hour,
  470. &parsingResult.min,
  471. &parsingResult.sec,
  472. &parsingResult.minSec,
  473. &parsingResult.tz_hour,
  474. &parsingResult.tz_min);
  475. }
  476. }
  477. else
  478. {
  479. // Original data without mini second
  480. if(strstr((char*)start, "Z") != NULL)
  481. {
  482. // Original data with Z
  483. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  484. &parsingResult.year,
  485. &parsingResult.month,
  486. &parsingResult.mday,
  487. &parsingResult.hour,
  488. &parsingResult.min,
  489. &parsingResult.sec);
  490. }
  491. else
  492. {
  493. // Original data without Z
  494. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  495. &parsingResult.year,
  496. &parsingResult.month,
  497. &parsingResult.mday,
  498. &parsingResult.hour,
  499. &parsingResult.min,
  500. &parsingResult.sec,
  501. &parsingResult.tz_hour,
  502. &parsingResult.tz_min);
  503. }
  504. }
  505. if(parsingResult.scanedElement >= 6)
  506. {
  507. tmStart.tm_year = parsingResult.year - 1900;
  508. tmStart.tm_mon = parsingResult.month - 1;
  509. tmStart.tm_mday = parsingResult.mday;
  510. tmStart.tm_hour = parsingResult.hour;
  511. tmStart.tm_min = parsingResult.min;
  512. tmStart.tm_sec = parsingResult.sec;
  513. tmStart.tm_gmtoff = 0;
  514. tbStart.time = mktime(&tmStart);
  515. tbStart.timezone = 0;
  516. tbStart.millitm = 0;
  517. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  518. if(DiffTimebWithNowSec(tbStart) <= 0)
  519. result = NO;
  520. else
  521. result = YES;
  522. }
  523. else
  524. {
  525. PRINTF_FUNC("Start date parsing error.\n");
  526. }
  527. return result;
  528. }
  529. void SetChargerMode(byte gun_index, byte mode)
  530. {
  531. chargingInfo[gun_index]->SystemStatus = mode;
  532. }
  533. void PRINTF_FUNC(char *string, ...)
  534. {
  535. va_list args;
  536. char buffer[4096];
  537. va_start(args, string);
  538. vsnprintf(buffer, sizeof(buffer), string, args);
  539. va_end(args);
  540. DEBUG_INFO_MSG("%s ", buffer);
  541. }
  542. long long DiffTimebWithNow(struct timeb ST)
  543. {
  544. //return milli-second
  545. struct timeb ET;
  546. long long StartTime, StopTime;
  547. ftime(&ET);
  548. StartTime = (long long)ST.time;
  549. StopTime = (long long)ET.time;
  550. return ((StopTime - StartTime)*1000) + (ET.millitm - ST.millitm);
  551. }
  552. bool CheckWebConfigChargingTimeout(byte gunIndex)
  553. {
  554. bool result = false;
  555. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  556. {
  557. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  558. result = true;
  559. }
  560. // 如果後臺斷線的要多看 offline 設定
  561. if (!result && ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  562. {
  563. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  564. {
  565. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  566. result = true;
  567. }
  568. }
  569. return result;
  570. }
  571. bool CheckWebConfigChargingEnergy(byte gunIndex)
  572. {
  573. bool result = false;
  574. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  575. {
  576. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  577. result = true;
  578. }
  579. // 如果後臺斷線的要多看 offline 設定
  580. if (!result && ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  581. {
  582. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  583. {
  584. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  585. result = true;
  586. }
  587. }
  588. return result;
  589. }
  590. bool CheckWebConfigChargingSOC(byte gunIndex)
  591. {
  592. bool result = false;
  593. if (ShmSysConfigAndInfo->SysConfig.MaxChargingSoc > 0)
  594. {
  595. if (chargingInfo[gunIndex]->EvBatterySoc >= ShmSysConfigAndInfo->SysConfig.MaxChargingSoc)
  596. result = true;
  597. }
  598. return result;
  599. }
  600. bool CheckOcppChargingTimeout(byte gunIndex)
  601. {
  602. bool result = false;
  603. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  604. {
  605. if (ShmOCPP16Data->SessionTarget[gunIndex].targetDuration > 0)
  606. {
  607. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmOCPP16Data->SessionTarget[gunIndex].targetDuration * 60))
  608. {
  609. PRINTF_FUNC("CheckOcppChargingTimeout. G_(%d) - (%d) \n", gunIndex,
  610. chargingInfo[gunIndex]->PresentChargedDuration);
  611. result = true;
  612. }
  613. }
  614. }
  615. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  616. {
  617. if (ShmOCPP20Data->SessionTarget[gunIndex].targetDuration > 0)
  618. {
  619. if (chargingInfo [gunIndex]->PresentChargedDuration >= (ShmOCPP20Data->SessionTarget[gunIndex].targetDuration * 60))
  620. {
  621. PRINTF_FUNC ( "CheckOcppChargingTimeout. G_(%d) - (%d) \n", gunIndex,
  622. chargingInfo [gunIndex]->PresentChargedDuration );
  623. result = true;
  624. }
  625. }
  626. }
  627. return result;
  628. }
  629. bool CheckOcppChargingEnergy(byte gunIndex)
  630. {
  631. bool result = false;
  632. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  633. {
  634. if (ShmOCPP16Data->SessionTarget[gunIndex].targetEnergy > 0)
  635. {
  636. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmOCPP16Data->SessionTarget[gunIndex].targetEnergy)
  637. {
  638. PRINTF_FUNC ( "CheckOcppChargingEnergy. G_(%d) - (%f) \n", gunIndex,
  639. chargingInfo[gunIndex]->PresentChargedEnergy );
  640. result = true;
  641. }
  642. }
  643. }
  644. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  645. {
  646. if (ShmOCPP20Data->SessionTarget[gunIndex].targetEnergy > 0)
  647. {
  648. if (chargingInfo [gunIndex]->PresentChargedEnergy >= ShmOCPP20Data->SessionTarget[gunIndex].targetEnergy)
  649. {
  650. PRINTF_FUNC ( "CheckOcppChargingEnergy. G_(%d) - (%f) \n", gunIndex,
  651. chargingInfo [gunIndex]->PresentChargedEnergy );
  652. result = true;
  653. }
  654. }
  655. }
  656. return result;
  657. }
  658. bool CheckOcppChargingSOC(byte gunIndex)
  659. {
  660. bool result = false;
  661. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  662. {
  663. if (ShmOCPP16Data->SessionTarget[gunIndex].targetSoc > 0)
  664. {
  665. if (chargingInfo [gunIndex]->EvBatterySoc >= (ShmOCPP16Data->SessionTarget[gunIndex].targetSoc))
  666. {
  667. PRINTF_FUNC ( "CheckOcppChargingSOC. G_(%d) - (%d) \n", gunIndex,
  668. chargingInfo [gunIndex]->PresentChargedDuration );
  669. result = true;
  670. }
  671. }
  672. }
  673. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  674. {
  675. if (ShmOCPP20Data->SessionTarget[gunIndex].targetSoc > 0)
  676. {
  677. if (chargingInfo [gunIndex]->EvBatterySoc >= (ShmOCPP20Data->SessionTarget[gunIndex].targetSoc))
  678. {
  679. PRINTF_FUNC ( "CheckOcppChargingSOC. G_(%d) - (%d) \n", gunIndex,
  680. chargingInfo [gunIndex]->PresentChargedDuration );
  681. result = true;
  682. }
  683. }
  684. }
  685. return result;
  686. }
  687. bool IsGunUsing(byte dcGunIndex)
  688. {
  689. bool result = false;
  690. if((chargingInfo[dcGunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  691. chargingInfo[dcGunIndex]->SystemStatus <= SYS_MODE_ALARM) ||
  692. (chargingInfo[dcGunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  693. chargingInfo[dcGunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  694. {
  695. result = true;
  696. }
  697. return result;
  698. }
  699. //==========================================
  700. // Log
  701. //==========================================
  702. void InfoFwSlotStatusLog()
  703. {
  704. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  705. {
  706. if ((bd0_1_status == 0 && bd0_2_status == 1) || (bd1_1_status == 0 && bd1_2_status == 1))
  707. PRINTF_FUNC("Connector 1 : Chademo [Ver. = %s]", ShmCHAdeMOData->evse[chargingInfo[0]->type_index].version);
  708. else if ((bd0_1_status == 1 && bd0_2_status == 0) || (bd1_1_status == 1 && bd1_2_status == 0))
  709. PRINTF_FUNC("Connector 1 : CCS [Ver. = %s]", ShmCcsData->V2GMessage_DIN70121[chargingInfo[0]->type_index].version);
  710. else if ((bd0_1_status == 1 && bd0_2_status == 1) || (bd1_1_status == 1 && bd1_2_status == 1))
  711. PRINTF_FUNC("Connector 1 : GB [Ver. = %s]", ShmGBTData->evse[chargingInfo[0]->type_index].version);
  712. }
  713. else
  714. {
  715. if (bd0_1_status == 0 && bd0_2_status == 1)
  716. PRINTF_FUNC("Connector 1 : Chademo [Ver. = %s]", ShmCHAdeMOData->evse[chargingInfo[0]->type_index].version);
  717. else if (bd0_1_status == 1 && bd0_2_status == 0)
  718. PRINTF_FUNC("Connector 1 : CCS [Ver. = %s]", ShmCcsData->V2GMessage_DIN70121[chargingInfo[0]->type_index].version);
  719. else if (bd0_1_status == 1 && bd0_2_status == 1)
  720. PRINTF_FUNC("Connector 1 : GB [Ver. = %s]", ShmGBTData->evse[chargingInfo[0]->type_index].version);
  721. if (bd1_1_status == 0 && bd1_2_status == 1)
  722. PRINTF_FUNC("Connector 2 : Chademo [Ver. = %s]", ShmCHAdeMOData->evse[chargingInfo[1]->type_index].version);
  723. else if (bd1_1_status == 1 && bd1_2_status == 0)
  724. PRINTF_FUNC("Connector 2 : CCS [Ver. = %s]", ShmCcsData->V2GMessage_DIN70121[chargingInfo[1]->type_index].version);
  725. else if (bd1_1_status == 1 && bd1_2_status == 1)
  726. PRINTF_FUNC("Connector 2 : GB [Ver. = %s]", ShmGBTData->evse[chargingInfo[1]->type_index].version);
  727. }
  728. }
  729. void InfoSystemType()
  730. {
  731. if (ShmDcCommonData->systemType == _SYSTEM_TYPE_SIMPLE)
  732. PRINTF_FUNC("DW or DM Serial.");
  733. else if (ShmDcCommonData->systemType == _SYSTEM_TYPE_STANDARD)
  734. PRINTF_FUNC("DS Serial - Standard");
  735. else if (ShmDcCommonData->systemType == _SYSTEM_TYPE_SMART)
  736. PRINTF_FUNC("DS Serial - Dynamic");
  737. }
  738. void InfoLcmVersion()
  739. {
  740. PRINTF_FUNC("LCM Version = V.%03d ", ShmDcCommonData->LcmFwVersion);
  741. }
  742. void CheckHwSlotStatusLog(byte index)
  743. {
  744. if (chargingInfo[index]->Type == _Type_Chademo)
  745. {
  746. PRINTF_FUNC("Hw check : Connector %d, Type : Chademo, Evboard_id = %d \n",
  747. index, chargingInfo[index]->Evboard_id);
  748. }
  749. else if (chargingInfo[index]->Type == _Type_CCS)
  750. {
  751. PRINTF_FUNC("Hw check : Connector %d, Type : CCS, Evboard_id = %d \n",
  752. index, chargingInfo[index]->Evboard_id);
  753. }
  754. else if (chargingInfo[index]->Type == _Type_GB)
  755. {
  756. PRINTF_FUNC("Hw check : Connector %d, Type : GB, Evboard_id = %d \n",
  757. index, chargingInfo[index]->Evboard_id);
  758. }
  759. }
  760. //==========================================
  761. // Check interface status
  762. //==========================================
  763. int isInterfaceUp(const char *interface)
  764. {
  765. int result = FAIL;
  766. FILE *fp;
  767. char cmd[256];
  768. char buf[512];
  769. strcpy(cmd, "ifconfig");
  770. fp = popen(cmd, "r");
  771. if(fp != NULL)
  772. {
  773. while(fgets(buf, sizeof(buf), fp) != NULL)
  774. {
  775. if(strstr(buf, interface) > 0)
  776. {
  777. result = PASS;
  778. }
  779. }
  780. }
  781. pclose(fp);
  782. return result;
  783. }
  784. //=================================
  785. // Create all share memory
  786. //=================================
  787. int CreateShareMemory()
  788. {
  789. int MeterSMId;
  790. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  791. {
  792. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  793. return 0;
  794. }
  795. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  796. {
  797. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  798. return 0;
  799. }
  800. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  801. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  802. {
  803. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  804. return 0;
  805. }
  806. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  807. {
  808. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  809. return 0;
  810. }
  811. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  812. //creat ShmPsuData
  813. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  814. {
  815. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  816. return 0;
  817. }
  818. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  819. {
  820. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  821. return 0;
  822. }
  823. memset(ShmPsuData, 0, sizeof(struct PsuData));
  824. if(CHAdeMO_QUANTITY > 0)
  825. {
  826. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  827. {
  828. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  829. return 0;
  830. }
  831. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  832. {
  833. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  834. return 0;
  835. }
  836. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  837. }
  838. if(GB_QUANTITY > 0)
  839. {
  840. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  841. {
  842. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  843. return 0;
  844. }
  845. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  846. {
  847. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  848. return 0;
  849. }
  850. memset(ShmGBTData, 0, sizeof(struct GBTData));
  851. }
  852. //creat ShmCcsData
  853. if(CCS_QUANTITY > 0)
  854. {
  855. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  856. {
  857. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  858. return 0;
  859. }
  860. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  861. {
  862. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  863. return 0;
  864. }
  865. memset(ShmCcsData, 0, sizeof(struct CcsData));
  866. }
  867. //creat ShmPrimaryMcuData
  868. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  869. {
  870. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  871. return 0;
  872. }
  873. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  874. {
  875. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  876. return 0;
  877. }
  878. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  879. //creat ShmFanModuleData
  880. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  881. {
  882. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  883. return 0;
  884. }
  885. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  886. {
  887. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  888. return 0;
  889. }
  890. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  891. //creat ShmRelayModuleData
  892. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  893. {
  894. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  895. return 0;
  896. }
  897. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  898. {
  899. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  900. return 0;
  901. }
  902. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  903. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  904. {
  905. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmLedModuleData NG \n");
  906. return 0;
  907. }
  908. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  909. {
  910. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmLedModuleData NG \n");
  911. return 0;
  912. }
  913. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  914. //creat ShmOCPP16Data
  915. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  916. {
  917. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  918. return 0;
  919. }
  920. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  921. {
  922. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  923. return 0;
  924. }
  925. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  926. if ((MeterSMId = shmget ( ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777 )) < 0)
  927. {
  928. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmOCPP16DataPH NG \n" );
  929. return 0;
  930. }
  931. else if ((ShmOCPP16DataPH = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  932. {
  933. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmOCPP16DataPH NG \n" );
  934. return 0;
  935. }
  936. memset ( ShmOCPP16DataPH , 0, sizeof(struct OCPP16Data) );
  937. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  938. {
  939. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget OCPP20Data NG\n");
  940. return 0;
  941. }
  942. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  943. {
  944. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat OCPP20Data NG\n");
  945. return 0;
  946. }
  947. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  948. if ((MeterSMId = shmget ( ShmSmartBoxKey, sizeof(struct SmartBoxData), IPC_CREAT | 0777 )) < 0)
  949. {
  950. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmSmartBoxKey NG \n" );
  951. return 0;
  952. }
  953. else if ((ShmSmartBoxData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  954. {
  955. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmSmartBoxData NG \n" );
  956. return 0;
  957. }
  958. memset ( ShmSmartBoxData, 0, sizeof(struct SmartBoxData) );
  959. if ((MeterSMId = shmget(ShmCsuMeterKey, sizeof(struct MeterInformation), IPC_CREAT | 0777)) < 0)
  960. {
  961. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCsuMeterKey NG \n");
  962. return 0;
  963. }
  964. else if ((ShmCsuMeterData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  965. {
  966. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCsuMeterData NG \n");
  967. return 0;
  968. }
  969. memset(ShmCsuMeterData, 0, sizeof(struct MeterInformation));
  970. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  971. {
  972. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCommonKey NG \n");
  973. return 0;
  974. }
  975. else if ((ShmDcCommonData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  976. {
  977. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCommonKey NG \n");
  978. return 0;
  979. }
  980. byte buf = 0;
  981. if (ShmDcCommonData->rebootCount == 1)
  982. buf = ShmDcCommonData->rebootCount;
  983. memset(ShmDcCommonData, 0, sizeof(struct DcCommonInformation));
  984. ShmDcCommonData->rebootCount = buf;
  985. return 1;
  986. }
  987. void SetupRebootCount(byte value)
  988. {
  989. int MeterSMId;
  990. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  991. {
  992. DEBUG_ERROR_MSG("[main]RecordRebootCount:shmget ShmCommonKey NG \n");
  993. }
  994. else
  995. {
  996. ShmDcCommonData->rebootCount = value;
  997. shmctl(MeterSMId, IPC_SET, 0);
  998. }
  999. }
  1000. //=================================
  1001. // LCM Page
  1002. //=================================
  1003. void ChangeLcmByIndex(byte page_index)
  1004. {
  1005. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL ||
  1006. page_index == _LCM_COMPLETE || page_index == _LCM_FIX || page_index == _LCM_EMC)
  1007. {
  1008. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0 &&
  1009. (page_index == _LCM_FIX || page_index == _LCM_EMC))
  1010. {
  1011. bool isUpdate = false;
  1012. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  1013. {
  1014. if (chargingInfo[_index]->SystemStatus == SYS_MODE_UPDATE)
  1015. {
  1016. isUpdate = true;
  1017. break;
  1018. }
  1019. }
  1020. if (!isUpdate &&
  1021. ShmDcCommonData->LcmFwVersion > 0 &&
  1022. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  1023. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_EMC;
  1024. else
  1025. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_FIX;
  1026. }
  1027. else
  1028. {
  1029. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  1030. }
  1031. }
  1032. }
  1033. //======================================================
  1034. // Peripheral initial
  1035. //======================================================
  1036. void InitGPIO()
  1037. {
  1038. /*****************0~3, 4 bank, bank x 32+ num*********************/
  1039. /***************************************************************/
  1040. /*************** GPIO 0 ***************************************/
  1041. /***************************************************************/
  1042. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  1043. system("echo 22 > /sys/class/gpio/export");
  1044. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  1045. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  1046. system("echo 23 > /sys/class/gpio/export");
  1047. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  1048. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  1049. system("echo 26 > /sys/class/gpio/export");
  1050. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  1051. system("echo 1 > /sys/class/gpio/gpio26/value");
  1052. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  1053. system("echo 27 > /sys/class/gpio/export");
  1054. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  1055. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  1056. system("echo 29 > /sys/class/gpio/export");
  1057. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  1058. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  1059. system("echo 19 > /sys/class/gpio/export");
  1060. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  1061. system("echo 1 > /sys/class/gpio/gpio19/value");
  1062. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  1063. system("echo 20 > /sys/class/gpio/export");
  1064. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  1065. /***************************************************************/
  1066. /*************** GPIO 1 ***************************************/
  1067. /***************************************************************/
  1068. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  1069. system("echo 44 > /sys/class/gpio/export");
  1070. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  1071. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  1072. system("echo 45 > /sys/class/gpio/export");
  1073. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  1074. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  1075. system("echo 46 > /sys/class/gpio/export");
  1076. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  1077. system("echo 0 > /sys/class/gpio/gpio46/value");
  1078. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  1079. system("echo 47 > /sys/class/gpio/export");
  1080. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  1081. /***************************************************************/
  1082. /*************** GPIO 2 ***************************************/
  1083. /***************************************************************/
  1084. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  1085. system("echo 89 > /sys/class/gpio/export");
  1086. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  1087. system("echo 1 > /sys/class/gpio/gpio89/value");
  1088. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  1089. system("echo 87 > /sys/class/gpio/export");
  1090. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  1091. system("echo 0 > /sys/class/gpio/gpio87/value");
  1092. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  1093. system("echo 88 > /sys/class/gpio/export");
  1094. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  1095. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  1096. system("echo 86 > /sys/class/gpio/export");
  1097. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  1098. /***************************************************************/
  1099. /*************** GPIO 3 ***************************************/
  1100. /***************************************************************/
  1101. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  1102. system("echo 111 > /sys/class/gpio/export");
  1103. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  1104. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  1105. system("echo 114 > /sys/class/gpio/export");
  1106. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  1107. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  1108. system("echo 113 > /sys/class/gpio/export");
  1109. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  1110. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  1111. system("echo 110 > /sys/class/gpio/export");
  1112. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  1113. system("echo 0 > /sys/class/gpio/gpio110/value");
  1114. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  1115. system("echo 115 > /sys/class/gpio/export");
  1116. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  1117. system("echo 0 > /sys/class/gpio/gpio115/value");
  1118. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  1119. system("echo 112 > /sys/class/gpio/export");
  1120. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  1121. system("echo 1 > /sys/class/gpio/gpio112/value");
  1122. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  1123. system("echo 116 > /sys/class/gpio/export");
  1124. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  1125. system("echo 0 > /sys/class/gpio/gpio116/value");
  1126. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  1127. system("echo 103 > /sys/class/gpio/export");
  1128. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  1129. system("echo 0 > /sys/class/gpio/gpio103/value");
  1130. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  1131. system("echo 104 > /sys/class/gpio/export");
  1132. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  1133. system("echo 0 > /sys/class/gpio/gpio104/value");
  1134. #ifdef SystemLogMessage
  1135. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK \n");
  1136. #endif
  1137. }
  1138. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1139. {
  1140. int fd,wrd;
  1141. unsigned char *buf;
  1142. unsigned int ChkSum,ChkSumOrg;
  1143. if((buf=malloc(MtdBlockSize))==NULL)
  1144. {
  1145. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  1146. if(ShmStatusCodeData!=NULL)
  1147. {
  1148. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1149. }
  1150. sleep(5);
  1151. system("reboot -f");
  1152. sleep(5);
  1153. system("reboot -f");
  1154. }
  1155. memset(buf, 0, MtdBlockSize);
  1156. //================================================
  1157. // Load configuration from mtdblock10
  1158. //================================================
  1159. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1160. //fd = open("/dev/mtdblock10", O_RDWR);
  1161. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1162. if (fd < 0)
  1163. {
  1164. free(buf);
  1165. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  1166. if(ShmStatusCodeData!=NULL)
  1167. {
  1168. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1169. }
  1170. sleep(5);
  1171. system("reboot -f");
  1172. sleep(5);
  1173. system("reboot -f");
  1174. }
  1175. wrd=read(fd, buf, MtdBlockSize);
  1176. close(fd);
  1177. if(wrd<MtdBlockSize)
  1178. {
  1179. free(buf);
  1180. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  1181. if(ShmStatusCodeData!=NULL)
  1182. {
  1183. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1184. }
  1185. sleep(5);
  1186. system("reboot -f");
  1187. sleep(5);
  1188. system("reboot -f");
  1189. }
  1190. ChkSum=0;
  1191. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1192. // {
  1193. // ChkSum+=buf[wrd];
  1194. // }
  1195. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1196. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1197. {
  1198. ChkSum+=buf[wrd];
  1199. }
  1200. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1201. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1202. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1203. //================================================
  1204. // Load configuration from mtdblock11
  1205. //================================================
  1206. if(ChkSum!=ChkSumOrg)
  1207. {
  1208. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  1209. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1210. //fd = open("/dev/mtdblock11", O_RDWR);
  1211. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1212. if (fd < 0)
  1213. {
  1214. free(buf);
  1215. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  1216. if(ShmStatusCodeData!=NULL)
  1217. {
  1218. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1219. }
  1220. sleep(5);
  1221. system("reboot -f");
  1222. sleep(5);
  1223. system("reboot -f");
  1224. }
  1225. memset(buf, 0, MtdBlockSize);
  1226. wrd=read(fd, buf,MtdBlockSize);
  1227. close(fd);
  1228. if(wrd<MtdBlockSize)
  1229. {
  1230. free(buf);
  1231. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  1232. if(ShmStatusCodeData!=NULL)
  1233. {
  1234. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1235. }
  1236. sleep(5);
  1237. system("reboot -f");
  1238. sleep(5);
  1239. system("reboot -f");
  1240. }
  1241. ChkSum=0;
  1242. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1243. // {
  1244. // ChkSum+=buf[wrd];
  1245. // }
  1246. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1247. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1248. {
  1249. ChkSum+=buf[wrd];
  1250. }
  1251. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1252. //================================================
  1253. // Load configuration from mtdblock12 (Factory default)
  1254. //================================================
  1255. if(ChkSum!=ChkSumOrg)
  1256. {
  1257. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  1258. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1259. //fd = open("/dev/mtdblock12", O_RDWR);
  1260. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1261. if (fd < 0)
  1262. {
  1263. free(buf);
  1264. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  1265. if(ShmStatusCodeData!=NULL)
  1266. {
  1267. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1268. }
  1269. sleep(5);
  1270. system("reboot -f");
  1271. sleep(5);
  1272. system("reboot -f");
  1273. }
  1274. memset(buf, 0, MtdBlockSize);
  1275. wrd=read(fd, buf,MtdBlockSize);
  1276. close(fd);
  1277. if(wrd<MtdBlockSize)
  1278. {
  1279. free(buf);
  1280. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  1281. if(ShmStatusCodeData!=NULL)
  1282. {
  1283. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1284. }
  1285. sleep(5);
  1286. system("reboot -f");
  1287. sleep(5);
  1288. system("reboot -f");
  1289. }
  1290. ChkSum=0;
  1291. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1292. // {
  1293. // ChkSum+=buf[wrd];
  1294. // }
  1295. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1296. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1297. {
  1298. ChkSum+=buf[wrd];
  1299. }
  1300. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1301. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1302. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1303. if(ChkSum!=ChkSumOrg)
  1304. {
  1305. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  1306. free(buf);
  1307. //system("cd /root;./FactoryConfig -m");
  1308. sleep(5);
  1309. char facBuf[256];
  1310. sprintf(facBuf, "cd /root;./FactoryConfig -m %s %s",
  1311. ShmSysConfigAndInfo->SysConfig.ModelName,
  1312. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  1313. system(facBuf);
  1314. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1315. system("sync");
  1316. sleep(5);
  1317. system("reboot -f");
  1318. sleep(5);
  1319. system("reboot -f");
  1320. return FAIL;
  1321. }
  1322. }
  1323. }
  1324. //load OK
  1325. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1326. free(buf);
  1327. system("rm -f /mnt/EvseConfig.bin");
  1328. // SysConfig in flash is empty (0xffffffff)
  1329. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1330. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1331. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1332. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1333. {
  1334. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1335. {
  1336. PRINTF_FUNC("Model name over length.\n");
  1337. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1338. }
  1339. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1340. {
  1341. PRINTF_FUNC("Model serial number over length.\n");
  1342. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1343. }
  1344. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1345. {
  1346. PRINTF_FUNC("System id over length.\n");
  1347. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1348. }
  1349. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1350. {
  1351. PRINTF_FUNC("Ethernet dhcp config is null.\n");
  1352. }
  1353. system("cd /root;./FactoryConfig -m");
  1354. sleep(3);
  1355. system("/usr/bin/run_evse_restart.sh");
  1356. }
  1357. PRINTF_FUNC("*********************************************************** \n");
  1358. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  1359. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1360. {
  1361. memcpy((char*)ShmOCPP16Data->ChargeBoxId,
  1362. (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1363. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1364. }
  1365. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1366. {
  1367. memcpy((char*)ShmOCPP20Data->ChargeBoxId,
  1368. (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1369. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1370. }
  1371. return PASS;
  1372. }
  1373. int isRouteFail()
  1374. {
  1375. int result = YES;
  1376. FILE *fp;
  1377. char buf[512];
  1378. fp = popen("route -n", "r");
  1379. if(fp != NULL)
  1380. {
  1381. while(fgets(buf, sizeof(buf), fp) != NULL)
  1382. {
  1383. if(strstr(buf, "eth0") != NULL)
  1384. result = NO;
  1385. }
  1386. }
  1387. pclose(fp);
  1388. return result;
  1389. }
  1390. int isReachableInternet()
  1391. {
  1392. int result = FAIL;
  1393. FILE *fp;
  1394. char cmd[256];
  1395. char buf[512];
  1396. char tmp[512];
  1397. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1398. // {
  1399. // result = PASS;
  1400. // }
  1401. // else
  1402. {
  1403. strcpy(cmd, "ifconfig eth0");
  1404. fp = popen(cmd, "r");
  1405. if (fp != NULL)
  1406. {
  1407. while(fgets(buf, sizeof(buf), fp) != NULL)
  1408. {
  1409. if (strstr(buf, "inet addr:") > 0)
  1410. {
  1411. sscanf(buf, "%*s%s", tmp);
  1412. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1413. tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  1414. sscanf(buf, "%*s%*s%*s%s", tmp);
  1415. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1416. tmp, strspn(tmp, "Mask:"), strlen(tmp)-strspn(tmp, "Mask:"));
  1417. }
  1418. }
  1419. }
  1420. pclose(fp);
  1421. memset(buf, 0x00, sizeof(buf));
  1422. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1423. {
  1424. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1425. fp = popen(cmd, "r");
  1426. if(fp != NULL)
  1427. {
  1428. while(fgets(buf, sizeof(buf), fp) != NULL)
  1429. {
  1430. if(strstr(buf, "transmitted") > 0)
  1431. {
  1432. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1433. if(strstr(buf,"100%") != NULL)
  1434. {
  1435. }
  1436. else
  1437. {
  1438. result = PASS;
  1439. }
  1440. //DEBUG_INFO("%s",buf);
  1441. //DEBUG_INFO("%s\n",tmp);
  1442. }
  1443. }
  1444. }
  1445. pclose(fp);
  1446. }
  1447. }
  1448. return result;
  1449. }
  1450. void InitEthernet()
  1451. {
  1452. system("ifconfig eth0 down");// eth0 down
  1453. system("ifconfig eth1 down");// eth1 down
  1454. sleep(2);
  1455. char tmpbuf[256];
  1456. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1457. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1458. sleep(2);
  1459. //Init Eth0 for internet
  1460. memset(tmpbuf,0,256);
  1461. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1462. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1463. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1464. system(tmpbuf);
  1465. memset(tmpbuf,0,256);
  1466. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1467. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1468. system(tmpbuf);
  1469. //system("ifconfig lo up");
  1470. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1471. //Init Eth1 for administrator tool
  1472. memset(tmpbuf,0,256);
  1473. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  1474. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  1475. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  1476. system(tmpbuf);
  1477. // local loopback network
  1478. memset(tmpbuf,0,256);
  1479. sprintf(tmpbuf,"ifconfig lo up");
  1480. system(tmpbuf);
  1481. //Run DHCP client if enabled
  1482. system("killall udhcpc");
  1483. system("rm -rf /etc/resolv.conf");
  1484. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1485. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1486. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1487. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1488. {
  1489. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1490. system(tmpbuf);
  1491. }
  1492. //Upgrade system id to /etc/hostname
  1493. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1494. system(tmpbuf);
  1495. pid_t pid = fork();
  1496. uint8_t cnt_pingDNS_Fail;
  1497. if(pid == 0)
  1498. {
  1499. for(;;)
  1500. {
  1501. if (isRouteFail())
  1502. {
  1503. //PRINTF_FUNC("eth0 not in route, restart eht0. \n");
  1504. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1505. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1506. {
  1507. InitialDHCP();
  1508. }
  1509. else
  1510. {
  1511. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1512. memset(tmpbuf,0,256);
  1513. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1514. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1515. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1516. system(tmpbuf);
  1517. memset(tmpbuf,0,256);
  1518. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1519. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1520. system(tmpbuf);
  1521. }
  1522. }
  1523. if(isReachableInternet() == PASS)
  1524. {
  1525. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1526. cnt_pingDNS_Fail = 0;
  1527. }
  1528. else
  1529. {
  1530. if(++cnt_pingDNS_Fail > 3)
  1531. {
  1532. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1533. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1534. {
  1535. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1536. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1537. system(tmpbuf);
  1538. }
  1539. else
  1540. {
  1541. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1542. memset(tmpbuf,0,256);
  1543. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1544. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1545. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1546. system(tmpbuf);
  1547. memset(tmpbuf,0,256);
  1548. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1549. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1550. system(tmpbuf);
  1551. }
  1552. cnt_pingDNS_Fail = 0;
  1553. }
  1554. }
  1555. bool ethResult = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet;
  1556. ShmSysConfigAndInfo->SysInfo.ethInternetConn = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet;
  1557. if(ethResult == YES)
  1558. {
  1559. system("/sbin/ifmetric eth0 0");
  1560. if(_isUsingWifi)
  1561. system("/sbin/ifmetric mlan0 1");
  1562. if(_isUsing4G)
  1563. system("/sbin/ifmetric ppp0 2");
  1564. }
  1565. if (!ethResult &&
  1566. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1567. _isUsingWifi)
  1568. {
  1569. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1570. if (ethResult)
  1571. {
  1572. system("/sbin/ifmetric eth0 1");
  1573. if(_isUsingWifi)
  1574. system("/sbin/ifmetric mlan0 0");
  1575. if(_isUsing4G)
  1576. system("/sbin/ifmetric ppp0 2");
  1577. }
  1578. }
  1579. if (!ethResult &&
  1580. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1581. _isUsing4G)
  1582. {
  1583. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == YES ? NO : YES;
  1584. if (ethResult)
  1585. {
  1586. system("/sbin/ifmetric eth0 1");
  1587. if(_isUsingWifi)
  1588. system("/sbin/ifmetric mlan0 2");
  1589. if(_isUsing4G)
  1590. system("/sbin/ifmetric ppp0 0");
  1591. }
  1592. }
  1593. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1594. sleep(5);
  1595. }
  1596. }
  1597. #ifdef SystemLogMessage
  1598. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK. \n");
  1599. #endif
  1600. }
  1601. int InitialRfidPort()
  1602. {
  1603. int uartO2 = open(rfidPortName, O_RDWR);
  1604. struct termios tios;
  1605. if (uartO2 != FAIL)
  1606. {
  1607. ioctl (uartO2, TCGETS, &tios);
  1608. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1609. tios.c_lflag = 0;
  1610. tios.c_iflag = 0;
  1611. tios.c_oflag = 0;
  1612. tios.c_cc[VMIN] = 0;
  1613. tios.c_cc[VTIME] = (unsigned char) 1;
  1614. tios.c_lflag = 0;
  1615. tcflush(uartO2, TCIFLUSH);
  1616. ioctl(uartO2, TCSETS, &tios);
  1617. }
  1618. if (uartO2 < 0)
  1619. {
  1620. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1621. }
  1622. return uartO2;
  1623. }
  1624. void GetMacAddress()
  1625. {
  1626. for (byte index = 0; index < 2; index++)
  1627. {
  1628. int fd;
  1629. struct ifreq ifr;
  1630. char tarEth[5];
  1631. char Mac[18];
  1632. sprintf(tarEth,"eth%d",index);
  1633. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1634. ifr.ifr_addr.sa_family = AF_INET;
  1635. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1636. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1637. close(fd);
  1638. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1639. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1640. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1641. if (index == 0)
  1642. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1643. else
  1644. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1645. }
  1646. }
  1647. void GetFirmwareVersion()
  1648. {
  1649. // Get CSU root file system version
  1650. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1651. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1652. for(uint8_t idx=0;idx<3;idx++)
  1653. {
  1654. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J' ||
  1655. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'K' ||
  1656. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'L' ||
  1657. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'S' ||
  1658. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'W')
  1659. {
  1660. chademo++;
  1661. count++;
  1662. }
  1663. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1664. {
  1665. gb++;
  1666. count++;
  1667. }
  1668. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'U' ||
  1669. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'E' ||
  1670. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'T' ||
  1671. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'D' ||
  1672. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'M' ||
  1673. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'N' ||
  1674. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'R' ||
  1675. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'V' ||
  1676. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'F' ||
  1677. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'Y' ||
  1678. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'Z' ||
  1679. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'I' ||
  1680. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'Q')
  1681. {
  1682. ccs++;
  1683. count++;
  1684. }
  1685. }
  1686. if (count == 1)
  1687. {
  1688. if (chademo > 0)
  1689. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1690. else if (ccs > 0)
  1691. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1692. else if (gb > 0)
  1693. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1694. }
  1695. else
  1696. {
  1697. if (chademo == 1 && ccs == 1)
  1698. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1699. else if (chademo == 1 && gb == 1)
  1700. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1701. else if (ccs == 1 && gb == 1)
  1702. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1703. else if (chademo == 2)
  1704. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '7';
  1705. else if (ccs == 2)
  1706. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '8';
  1707. else if (gb == 2)
  1708. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '9';
  1709. }
  1710. // Get network option from model name
  1711. if (_isUsingWifi && _isUsing4G)
  1712. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1713. else if (_isUsingWifi)
  1714. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1715. else if (_isUsing4G)
  1716. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1717. else
  1718. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1719. // switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1720. // {
  1721. // case 'B':
  1722. // case 'U':
  1723. // //Blue tooth
  1724. // ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1725. // break;
  1726. // case 'W':
  1727. // // WIFI
  1728. // ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1729. // break;
  1730. // case 'T':
  1731. // // 3G/4G
  1732. // ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1733. // break;
  1734. // case 'D':
  1735. // ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1736. // break;
  1737. // default:
  1738. // // LAN
  1739. // ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1740. // break;
  1741. // }
  1742. // Get rating power from model name
  1743. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1744. // Get IEC or UL
  1745. char _buf[3] = {0};
  1746. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[3], 1);
  1747. if (strcmp(_buf, "E") == EQUAL || strcmp(_buf, "G") == EQUAL || strcmp(_buf, "C") == EQUAL ||
  1748. strcmp(_buf, "T") == EQUAL || strcmp(_buf, "K") == EQUAL || strcmp(_buf, "Z") == EQUAL ||
  1749. strcmp(_buf, "M") == EQUAL || strcmp(_buf, "P") == EQUAL || strcmp(_buf, "I") == EQUAL ||
  1750. strcmp(_buf, "F") == EQUAL)
  1751. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1752. else if (strcmp(_buf, "U") == EQUAL || strcmp(_buf, "B") == EQUAL || strcmp(_buf, "L") == EQUAL)
  1753. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1754. else if (strcmp(_buf, "J") == EQUAL)//LWADD
  1755. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_JARI;
  1756. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
  1757. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
  1758. if (strcmp(_buf, "PN") == EQUAL ||
  1759. strcmp(_buf, "N7") == EQUAL ||
  1760. strcmp(_buf, "NX") == EQUAL ||
  1761. strcmp(_buf, "OL") == EQUAL ||
  1762. strcmp(_buf, "DE") == EQUAL)
  1763. ShmDcCommonData->ShowLogoFlag = NO;
  1764. else
  1765. ShmDcCommonData->ShowLogoFlag = YES;
  1766. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'G')
  1767. ShmDcCommonData->chargerType = CHARGER_TYPE_SIMPLE;
  1768. else
  1769. ShmDcCommonData->chargerType = CHARGER_TYPE_STANDARD;
  1770. }
  1771. void InitialGunIndexToUnUse()
  1772. {
  1773. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  1774. {
  1775. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index = NO_DEFINE;
  1776. }
  1777. for (byte index = 0; index < CCS_QUANTITY; index++)
  1778. {
  1779. ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index = NO_DEFINE;
  1780. }
  1781. for (byte index = 0; index < GB_QUANTITY; index++)
  1782. {
  1783. ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index = NO_DEFINE;
  1784. }
  1785. for (byte index = 0; index < AC_QUANTITY; index++)
  1786. {
  1787. ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index = NO_DEFINE;
  1788. }
  1789. }
  1790. void ParsingModelNameForDeviceEnable()
  1791. {
  1792. // Payment
  1793. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == '1')
  1794. {
  1795. _verifyMechanism = VERIFY_MECHANISM_ENEGATE;
  1796. }
  1797. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == '2')
  1798. {
  1799. _verifyMechanism = VERIFY_MECHANISM_BAZEL8;
  1800. }
  1801. // Wifi
  1802. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == '1' ||
  1803. ShmSysConfigAndInfo->SysConfig.ModelName[10] == '2' ||
  1804. ShmSysConfigAndInfo->SysConfig.ModelName[10] == '3' ||
  1805. ShmSysConfigAndInfo->SysConfig.ModelName[10] == '4' ||
  1806. ShmSysConfigAndInfo->SysConfig.ModelName[10] == '7' ||
  1807. ShmSysConfigAndInfo->SysConfig.ModelName[10] == '8' ||
  1808. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'A' ||
  1809. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D' ||
  1810. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'K' ||
  1811. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'L' ||
  1812. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1813. _isUsingWifi = YES;
  1814. else
  1815. _isUsingWifi = NO;
  1816. // 4G
  1817. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == '1' ||
  1818. ShmSysConfigAndInfo->SysConfig.ModelName[10] == '2' ||
  1819. ShmSysConfigAndInfo->SysConfig.ModelName[10] == '3' ||
  1820. ShmSysConfigAndInfo->SysConfig.ModelName[10] == '4' ||
  1821. ShmSysConfigAndInfo->SysConfig.ModelName[10] == '7' ||
  1822. ShmSysConfigAndInfo->SysConfig.ModelName[10] == '8' ||
  1823. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'A' ||
  1824. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D' ||
  1825. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'L' ||
  1826. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1827. _isUsing4G = YES;
  1828. else
  1829. _isUsing4G = NO;
  1830. }
  1831. void InitialShareMemoryInfo()
  1832. {
  1833. FILE *fp;
  1834. char cmd[512];
  1835. char buf[512];
  1836. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1837. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1838. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1839. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1840. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1841. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1842. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1843. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1844. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1845. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1846. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1847. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1848. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1849. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1850. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1851. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1852. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1853. sprintf(cmd, "/bin/uname -r");
  1854. fp = popen(cmd, "r");
  1855. if(fp == NULL)
  1856. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1857. else
  1858. {
  1859. while(fgets(buf, sizeof(buf), fp) != NULL)
  1860. {
  1861. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1862. }
  1863. }
  1864. GetFirmwareVersion();
  1865. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1866. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1867. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1868. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1869. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1870. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1871. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1872. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1873. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1874. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1875. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1876. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1877. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1878. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1879. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1880. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1881. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1882. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1883. ShmSysConfigAndInfo->SysInfo.InternetConn = NO;
  1884. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = NO;
  1885. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1886. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1887. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1888. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1889. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1890. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = NO;
  1891. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1892. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1893. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1894. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1895. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail = NO;
  1896. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1897. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1898. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1899. ShmRelayModuleData->SelfTest_Comp = NO;
  1900. ShmFanModuleData->SelfTest_Comp = NO;
  1901. ShmLedModuleData->SelfTest_Comp = NO;
  1902. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_NORMAL;
  1903. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1904. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  1905. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1906. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOTTING;
  1907. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  1908. ShmFanModuleData->TestFanSpeed = 0;
  1909. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1910. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1911. InitialGunIndexToUnUse();
  1912. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1913. ShmDcCommonData->systemType = _SYSTEM_TYPE_SIMPLE;
  1914. ShmDcCommonData->CcsVersion = _CCS_VERSION_CHECK_TAG_V015S0;
  1915. ShmDcCommonData->psuKeepCommunication = NO;
  1916. ShmDcCommonData->acContactSwitch = NO;
  1917. ShmDcCommonData->ConnectErrList[0].GunErrMessage = 0;
  1918. ShmDcCommonData->ConnectErrList[1].GunErrMessage = 0;
  1919. ShmDcCommonData->LcmFwVersion = 0;
  1920. ShmDcCommonData->minOutputCur = 0;
  1921. ShmDcCommonData->evBoardResetFlag = NO;
  1922. ShmDcCommonData->enObtainAuthorizbyCCID = NO;
  1923. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  1924. ShmDcCommonData->_psuProtocol = _PSU_PROTOCOL_INFY;
  1925. ShmDcCommonData->_offlinePower = 0;
  1926. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE; //LW_tpc plug detect
  1927. ShmDcCommonData->TpcDetectFunction = NO; //LW_tpc plug detect
  1928. ShmDcCommonData->_isPsuErrorOccur = NO;
  1929. ShmDcCommonData->stopSessionIndex = NO_DEFINE;
  1930. ParsingModelNameForDeviceEnable();
  1931. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1932. }
  1933. int Initialization()
  1934. {
  1935. // 初始化卡號驗證的 Flag
  1936. ClearAuthorizedFlag();
  1937. // 初始化插槍驗證的 Flag
  1938. ClearDetectPluginFlag();
  1939. // UART 2
  1940. if (_verifyMechanism == VERIFY_MECHANISM_RFID)
  1941. rfidFd = InitialRfidPort();
  1942. int pinOut[2] = { 116, 115 }; //D13(Cha)、C13(CCS)
  1943. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1944. {
  1945. chargingInfo[count]->RemoteStartFlag = NO;
  1946. chargingInfo[count]->ReservedStartFlag = NO;
  1947. if (chargingInfo[count]->Type == _Type_Chademo)
  1948. {
  1949. gpio_set_value(pinOut[count], 0x00);
  1950. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1951. }
  1952. else if (chargingInfo[count]->Type == _Type_GB)
  1953. {
  1954. gpio_set_value(pinOut[count], 0x00);
  1955. ShmGBTData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1956. }
  1957. else if (chargingInfo[count]->Type == _Type_CCS)
  1958. {
  1959. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1960. gpio_set_value(pinOut[1], 0x00);
  1961. else
  1962. gpio_set_value(pinOut[count], 0x00);
  1963. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1964. }
  1965. strcpy((char *)ShmDcCommonData->_reserved_UserId[count], "");
  1966. }
  1967. byte acDirIndex = 0;
  1968. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1969. {
  1970. if (count == 1)
  1971. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  1972. ocpp_set_noErrorCode_cmd(count + acDirIndex, "NoError");
  1973. }
  1974. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  1975. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  1976. {
  1977. ac_chargingInfo[count]->RemoteStartFlag = NO;
  1978. if (ac_chargingInfo[count]->Type == _Type_AC)
  1979. {
  1980. ac_chargingInfo[count]->SelfTest_Comp = NO;
  1981. ocpp_set_noErrorCode_cmd(count + hasDc, "NoError");
  1982. }
  1983. }
  1984. PRINTF_FUNC("Information check done \n");
  1985. return PASS;
  1986. }
  1987. bool InitialSystemDefaultConfig()
  1988. {
  1989. bool result = true;
  1990. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1991. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  1992. InitGPIO();
  1993. InitEthernet();
  1994. GetMacAddress();
  1995. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1996. // sleep(3);
  1997. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1998. // sleep(1);
  1999. // system("/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up");
  2000. return result;
  2001. }
  2002. bool DisplaySelfTestFailReason()
  2003. {
  2004. bool result = false;
  2005. // RB、FB、407、EV 小板中有些板子無回應
  2006. if (ShmRelayModuleData->SelfTest_Comp == NO)
  2007. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; result = true; }
  2008. if (ShmFanModuleData->SelfTest_Comp == NO)
  2009. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; result = true; }
  2010. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  2011. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; result = true; }
  2012. // if (ShmLedModuleData->SelfTest_Comp == NO)
  2013. // { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES; }
  2014. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES)
  2015. { result = true; }
  2016. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2017. {
  2018. if (chargingInfo[index]->Type == _Type_Chademo)
  2019. {
  2020. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  2021. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; result = true; }
  2022. }
  2023. else if (chargingInfo[index]->Type == _Type_GB)
  2024. {
  2025. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  2026. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; result = true; }
  2027. }
  2028. else if (chargingInfo[index]->Type == _Type_CCS)
  2029. {
  2030. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2031. {
  2032. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  2033. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; result = true; }
  2034. }
  2035. }
  2036. }
  2037. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  2038. {
  2039. // 先借 GBT 顯示
  2040. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  2041. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; result = true; }
  2042. }
  2043. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  2044. {
  2045. // AC Contact 未搭上
  2046. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  2047. result = true;
  2048. }
  2049. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  2050. {
  2051. result = true;
  2052. }
  2053. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  2054. {
  2055. // PSU 通訊問題
  2056. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  2057. result = true;
  2058. }
  2059. return result;
  2060. }
  2061. void SelfTestRun()
  2062. {
  2063. bool evInitFlag = false;
  2064. StartSystemTimeoutDet(Timeout_SelftestChk);
  2065. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  2066. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  2067. GetTimeoutValue(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) <= 20)
  2068. {
  2069. ReviewCriticalAlarm();
  2070. ChkPrimaryStatus();
  2071. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL ||
  2072. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  2073. {
  2074. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2075. return;
  2076. }
  2077. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  2078. {
  2079. if (ShmPsuData->Work_Step == _NO_WORKING ||
  2080. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  2081. {
  2082. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2083. return;
  2084. }
  2085. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  2086. {
  2087. case _STEST_VERSION:
  2088. {
  2089. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
  2090. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  2091. {
  2092. //PRINTF_FUNC("RB pass \n");
  2093. ShmRelayModuleData->SelfTest_Comp = YES;
  2094. }
  2095. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  2096. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  2097. {
  2098. //PRINTF_FUNC("Fan pass \n");
  2099. ShmFanModuleData->SelfTest_Comp = YES;
  2100. }
  2101. if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
  2102. ShmPrimaryMcuData->version[0] != '\0')
  2103. {
  2104. //PRINTF_FUNC("407 pass \n");
  2105. ShmPrimaryMcuData->SelfTest_Comp = YES;
  2106. }
  2107. // EV 小板
  2108. if (!evInitFlag)
  2109. {
  2110. evInitFlag = YES;
  2111. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2112. {
  2113. if (chargingInfo[index]->Type == _Type_Chademo)
  2114. {
  2115. if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
  2116. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  2117. {
  2118. //PRINTF_FUNC("chademo pass \n");
  2119. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  2120. }
  2121. else
  2122. {
  2123. //PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  2124. evInitFlag = NO;
  2125. }
  2126. }
  2127. else if (chargingInfo[index]->Type == _Type_GB)
  2128. {
  2129. if (strlen((char *)ShmGBTData->evse[chargingInfo[index]->type_index].version) != 0 ||
  2130. ShmGBTData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  2131. {
  2132. //PRINTF_FUNC("GBT pass \n");
  2133. ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  2134. }
  2135. else
  2136. {
  2137. //PRINTF_FUNC("GBT fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  2138. evInitFlag = NO;
  2139. }
  2140. }
  2141. else if (chargingInfo[index]->Type == _Type_CCS)
  2142. {
  2143. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2144. {
  2145. if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
  2146. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  2147. {
  2148. //PRINTF_FUNC("ccs fw = %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  2149. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  2150. }
  2151. else
  2152. {
  2153. //PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  2154. evInitFlag = NO;
  2155. }
  2156. }
  2157. }
  2158. }
  2159. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  2160. {
  2161. if (ac_chargingInfo[index]->Type == _Type_AC)
  2162. {
  2163. if (strlen((char *)ac_chargingInfo[index]->version) != 0 ||
  2164. ac_chargingInfo[index]->version[0] != '\0')
  2165. {
  2166. ac_chargingInfo[index]->SelfTest_Comp = YES;
  2167. }
  2168. else
  2169. {
  2170. evInitFlag = NO;
  2171. }
  2172. }
  2173. }
  2174. }
  2175. if (ShmFanModuleData->SelfTest_Comp &&
  2176. ShmRelayModuleData->SelfTest_Comp &&
  2177. ShmPrimaryMcuData->SelfTest_Comp &&
  2178. evInitFlag)
  2179. {
  2180. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  2181. }
  2182. }
  2183. break;
  2184. case _STEST_AC_CONTACTOR:
  2185. {
  2186. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  2187. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  2188. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  2189. {
  2190. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  2191. PRINTF_FUNC("Communication board pass. \n");
  2192. }
  2193. }
  2194. break;
  2195. case _STEST_PSU_DETECT:
  2196. {
  2197. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  2198. {
  2199. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  2200. }
  2201. }
  2202. break;
  2203. case _STEST_PSU_CAP:
  2204. {
  2205. // 此測試是要確認當前總輸出能力
  2206. // 如果沒有 PSU 模組請 bypass
  2207. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  2208. {
  2209. sleep(1);
  2210. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  2211. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  2212. }
  2213. }
  2214. break;
  2215. }
  2216. }
  2217. else
  2218. break;
  2219. usleep(100000);
  2220. }
  2221. }
  2222. int SpawnTask()
  2223. {
  2224. sleep(2);
  2225. system("/root/Module_EventLogging &");
  2226. system("/root/Module_PrimaryComm &");
  2227. system("/root/Module_EvComm &");
  2228. system("/root/Module_LcmControl &");
  2229. system("/root/Module_InternalComm &");
  2230. system("/root/Module_PsuComm &");
  2231. system("/root/Module_SmartBox &");
  2232. system("/root/Module_ProduceUtils &");
  2233. if (_verifyMechanism == VERIFY_MECHANISM_BAZEL8)
  2234. system("/root/Module_Payment_Bazel8 &");
  2235. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2236. system("/root/OcppBackend &");
  2237. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2238. system("/root/OcppBackend20 &");
  2239. if(_isUsing4G)
  2240. {
  2241. system("/root/Module_4g &");
  2242. }
  2243. if(_isUsingWifi)
  2244. {
  2245. system("/root/Module_Wifi &");
  2246. }
  2247. return PASS;
  2248. }
  2249. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2250. {
  2251. int result = PASS;
  2252. int fd,wrd;
  2253. unsigned int i,Chk;
  2254. unsigned char *ptr, *BufTmp;
  2255. Chk=0;
  2256. ptr=(unsigned char *)UsrData;
  2257. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2258. {
  2259. memset(BufTmp, 0, MtdBlockSize);
  2260. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2261. for(i=0; i<MtdBlockSize-4; i++)
  2262. Chk+=*(ptr+i);
  2263. memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
  2264. fd = open("/dev/mtdblock10", O_RDWR);
  2265. if (fd > 0)
  2266. {
  2267. wrd=write(fd, BufTmp, MtdBlockSize);
  2268. close(fd);
  2269. if(wrd >= MtdBlockSize)
  2270. {
  2271. fd = open("/dev/mtdblock11", O_RDWR);
  2272. if (fd > 0)
  2273. {
  2274. wrd=write(fd, BufTmp, MtdBlockSize);
  2275. close(fd);
  2276. if(wrd < MtdBlockSize)
  2277. {
  2278. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  2279. result = FAIL;
  2280. }
  2281. }
  2282. else
  2283. {
  2284. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  2285. result = FAIL;
  2286. }
  2287. }
  2288. else
  2289. {
  2290. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  2291. result = FAIL;
  2292. }
  2293. }
  2294. else
  2295. {
  2296. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  2297. result = FAIL;
  2298. }
  2299. }
  2300. else
  2301. {
  2302. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  2303. result = FAIL;
  2304. }
  2305. if(BufTmp != NULL)
  2306. free(BufTmp);
  2307. return result;
  2308. }
  2309. //===============================================
  2310. // Common Detect Chk - Stop Charging ?
  2311. //===============================================
  2312. bool isEvBoardStopChargeFlag(byte gunIndex)
  2313. {
  2314. return chargingInfo[gunIndex]->StopChargeFlag;
  2315. }
  2316. bool isEvBoardNormalStopChargeFlag(byte gunIndex)
  2317. {
  2318. return chargingInfo[gunIndex]->NormalStopChargeFlag;
  2319. }
  2320. //===============================================
  2321. // 插槍鎖槍狀況
  2322. //===============================================
  2323. void ClearDetectPluginFlag()
  2324. {
  2325. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2326. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2327. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2328. {
  2329. ocpp_sub_set_remote_start_transaction_req(gun_index, NO);
  2330. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  2331. chargingInfo[gun_index]->RemoteStartFlag = NO;
  2332. if (chargingInfo[gun_index]->ReservedStartFlag == YES)
  2333. chargingInfo[gun_index]->ReservedStartFlag = NO;
  2334. }
  2335. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  2336. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2337. }
  2338. void DetectPluginStart()
  2339. {
  2340. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  2341. }
  2342. bool isDetectPlugin()
  2343. {
  2344. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  2345. return YES;
  2346. return NO;
  2347. }
  2348. bool isEvGunLocked(byte gunIndex)
  2349. {
  2350. return chargingInfo[gunIndex]->GunLocked;
  2351. }
  2352. //===============================================
  2353. // Common Detect Chk - Chademo
  2354. //===============================================
  2355. bool isEvContactorWelding_chademo(byte gunIndex)
  2356. {
  2357. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  2358. }
  2359. bool isEvStopReq_chademo(byte gunIndex)
  2360. {
  2361. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  2362. }
  2363. byte isPrechargeStatus_chademo(byte gunIndex)
  2364. {
  2365. byte result = 0x00;
  2366. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2367. return result;
  2368. }
  2369. //===============================================
  2370. // Common Detect Chk - GB
  2371. //===============================================
  2372. byte isPrechargeStatus_gb(byte gunIndex)
  2373. {
  2374. byte result = 0x00;
  2375. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2376. return result;
  2377. }
  2378. //===============================================
  2379. // Common Detect Chk - CCS
  2380. //===============================================
  2381. byte isPrechargeStatus_ccs(byte gunIndex)
  2382. {
  2383. byte result = 0x00;
  2384. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2385. {
  2386. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2387. }
  2388. return result;
  2389. }
  2390. //===============================================
  2391. // Callback
  2392. //===============================================
  2393. void DisplayChargingInfo()
  2394. {
  2395. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  2396. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2397. {
  2398. if (chargingInfo[i]->SystemStatus != SYS_MODE_IDLE &&
  2399. chargingInfo[i]->SystemStatus != SYS_MODE_RESERVATION)
  2400. {
  2401. ChangeGunSelectByIndex(i);
  2402. usleep(50000);
  2403. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2404. return;
  2405. }
  2406. }
  2407. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2408. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  2409. ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_COMPLETE)
  2410. {
  2411. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2412. }
  2413. usleep(50000);
  2414. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2415. }
  2416. void _AutoReturnTimeout()
  2417. {
  2418. PRINTF_FUNC("*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  2419. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2420. {
  2421. ClearDetectPluginFlag();
  2422. }
  2423. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2424. {
  2425. if (ShmDcCommonData->stopSessionIndex != NO_DEFINE &&
  2426. ShmDcCommonData->stopSessionIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2427. {
  2428. PRINTF_FUNC("NormalStop (%d) \n", ShmDcCommonData->stopSessionIndex);
  2429. ChargingTerminalProcess(ShmDcCommonData->stopSessionIndex);
  2430. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2431. ShmDcCommonData->stopSessionIndex = NO_DEFINE;
  2432. }
  2433. else
  2434. {
  2435. bool needPluginDet = true;
  2436. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  2437. {
  2438. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2439. {
  2440. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  2441. {
  2442. needPluginDet = false;
  2443. break;
  2444. }
  2445. }
  2446. }
  2447. if (needPluginDet)
  2448. DetectPluginStart();
  2449. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHCOMP;
  2450. }
  2451. }
  2452. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  2453. {
  2454. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHFAIL;
  2455. }
  2456. usleep(50000);
  2457. stopChargingChkByCard = false;
  2458. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2459. }
  2460. void _SelfTestTimeout()
  2461. {
  2462. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  2463. {
  2464. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2465. {
  2466. ChargingAlarmProcess(gun_index);
  2467. }
  2468. }
  2469. ShmPsuData->Work_Step = _NO_WORKING;
  2470. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2471. PRINTF_FUNC("Self test timeout. \n");
  2472. }
  2473. void _AuthorizedTimeout()
  2474. {
  2475. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  2476. isCardScan = false;
  2477. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2478. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHFAIL;
  2479. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2480. ClearAuthorizedFlag();
  2481. }
  2482. void _DetectPlugInTimeout()
  2483. {
  2484. PRINTF_FUNC("*********** _DetectPlugInTimeout (%d)*********** \n", _connectionTimeout);
  2485. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2486. ClearDetectPluginFlag();
  2487. usleep(50000);
  2488. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2489. }
  2490. //LW_tpc plug detect
  2491. void _DetectTpcPlugInTimeout()
  2492. {
  2493. PRINTF_FUNC("// _Detect CHAdeMO_TPC PlugInTimeout(60 sec) // \n");
  2494. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE;
  2495. }
  2496. void _DetectEvChargingEnableTimeout(byte gunIndex)
  2497. {
  2498. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2499. {
  2500. if(!isEvGunLocked(gunIndex))
  2501. {
  2502. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  2503. }
  2504. }
  2505. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2506. {
  2507. if(!isEvGunLocked(gunIndex))
  2508. {
  2509. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  2510. }
  2511. }
  2512. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  2513. {
  2514. if(!isEvGunLocked(gunIndex))
  2515. {
  2516. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  2517. }
  2518. }
  2519. ChargingTerminalProcess(gunIndex);
  2520. _AutoReturnTimeout();
  2521. }
  2522. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  2523. {
  2524. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  2525. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  2526. {
  2527. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  2528. _AutoReturnTimeout();
  2529. }
  2530. }
  2531. void _PrepareTimeout(byte gunIndex)
  2532. {
  2533. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  2534. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  2535. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  2536. _AutoReturnTimeout();
  2537. }
  2538. void _CcsPrechargeTimeout(byte gunIndex)
  2539. {
  2540. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  2541. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  2542. }
  2543. //===============================================
  2544. // 卡號驗證
  2545. //===============================================
  2546. void AuthorizingStart()
  2547. {
  2548. ocpp_set_authorizeReq_cmd(YES);
  2549. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  2550. }
  2551. void ClearAuthorizedFlag()
  2552. {
  2553. ocpp_set_authorizeConf_cmd(NO);
  2554. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  2555. }
  2556. bool isAuthorizedComplete()
  2557. {
  2558. return ocpp_get_authorize_conf();
  2559. }
  2560. bool IsAuthorizingMode()
  2561. {
  2562. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  2563. return false;
  2564. return true;
  2565. }
  2566. //===============================================
  2567. // 紀錄 Alarm Code
  2568. //===============================================
  2569. void RecordAlarmCode(byte gunIndex, char *code)
  2570. {
  2571. if (strcmp(code, "011018") == EQUAL) { ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = YES; return; }
  2572. else if (strcmp(code, "011019") == EQUAL) { ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = YES; return; }
  2573. else if (strcmp(code, "011020") == EQUAL) { ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = YES; return; }
  2574. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) == EQUAL)
  2575. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  2576. if (strcmp(code, "012234") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = YES;
  2577. else if (strcmp(code, "012235") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  2578. else if (strcmp(code, "012236") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = YES;
  2579. else if (strcmp(code, "012288") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = YES;
  2580. else if (strcmp(code, "012289") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = YES;
  2581. else if (strcmp(code, "012290") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = YES;
  2582. else if (strcmp(code, "012229") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = YES;
  2583. else if (strcmp(code, "012230") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = YES;
  2584. else if (strcmp(code, "012231") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = YES;
  2585. else if (strcmp(code, "012296") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = YES;
  2586. else if (strcmp(code, "012297") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = YES;
  2587. else if (strcmp(code, "012298") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = YES;
  2588. else if (strcmp(code, "011011") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = YES;
  2589. else if (strcmp(code, "011013") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = YES;
  2590. else if (strcmp(code, "011015") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = YES;
  2591. else if (strcmp(code, "011012") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = YES;
  2592. else if (strcmp(code, "011014") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = YES;
  2593. else if (strcmp(code, "011016") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = YES;
  2594. else if (strcmp(code, "012320") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = YES;
  2595. else if (strcmp(code, "012321") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = YES;
  2596. else if (strcmp(code, "012322") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = YES;
  2597. else if (strcmp(code, "011039") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault = YES;
  2598. else if (strcmp(code, "011040") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault = YES;
  2599. else if (strcmp(code, "023984") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.StopByEvseCondition = YES;
  2600. }
  2601. void ResetChargerAlarmCode(byte gunIndex, char *code)
  2602. {
  2603. if (strcmp(code, "012229") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP)
  2604. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  2605. else if (strcmp(code, "012230") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP)
  2606. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  2607. else if (strcmp(code, "012231") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP)
  2608. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  2609. else if (strcmp(code, "011011") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault)
  2610. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  2611. else if (strcmp(code, "011013") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault)
  2612. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  2613. else if (strcmp(code, "011015") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault)
  2614. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  2615. else if (strcmp(code, "011012") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault)
  2616. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  2617. else if (strcmp(code, "011014") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault)
  2618. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  2619. else if (strcmp(code, "011016") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault)
  2620. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  2621. else if (strcmp(code, "011018") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail)
  2622. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  2623. else if (strcmp(code, "011019") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail)
  2624. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  2625. else if (strcmp(code, "011020") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail)
  2626. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  2627. else if (strcmp(code, "012320") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP)
  2628. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = NO;
  2629. else if (strcmp(code, "012321") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP)
  2630. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = NO;
  2631. else if (strcmp(code, "012322") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP)
  2632. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = NO;
  2633. else if (strcmp(code, "011039") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault)
  2634. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault = NO;
  2635. else if (strcmp(code, "011040") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault)
  2636. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault = NO;
  2637. else if (strcmp(code, "023984") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.StopByEvseCondition)
  2638. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.StopByEvseCondition = NO;
  2639. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6) == EQUAL)
  2640. {
  2641. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2642. }
  2643. }
  2644. void ReleaseSysAlarmCode(byte gunIndex)
  2645. {
  2646. // 回 idle 後主要清除 GFD Trip、UVP、OVP、GFD Warning,PSU fail
  2647. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2648. {
  2649. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012251", 6) == EQUAL ||
  2650. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012252", 6) == EQUAL ||
  2651. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012324", 6) == EQUAL ||
  2652. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012325", 6) == EQUAL ||
  2653. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012267", 6) == EQUAL ||
  2654. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012346", 6) == EQUAL)
  2655. {
  2656. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2657. }
  2658. }
  2659. if (gunIndex == 0)
  2660. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_1 = NO;
  2661. else if (gunIndex == 1)
  2662. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_2 = NO;
  2663. ResetChargerAlarmCode(gunIndex, "023984");
  2664. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2665. {
  2666. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  2667. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  2668. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP = NO;
  2669. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  2670. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = NO;
  2671. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2672. {
  2673. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL ||
  2674. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL ||
  2675. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012217", 6) == EQUAL ||
  2676. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012296", 6) == EQUAL)
  2677. {
  2678. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2679. }
  2680. }
  2681. }
  2682. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  2683. {
  2684. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  2685. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  2686. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP = NO;
  2687. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  2688. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = NO;
  2689. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2690. {
  2691. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL ||
  2692. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL ||
  2693. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012219", 6) == EQUAL ||
  2694. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012297", 6) == EQUAL)
  2695. {
  2696. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2697. }
  2698. }
  2699. }
  2700. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2701. {
  2702. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  2703. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  2704. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP = NO;
  2705. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  2706. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = NO;
  2707. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2708. {
  2709. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL ||
  2710. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL ||
  2711. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012221", 6) == EQUAL ||
  2712. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012298", 6) == EQUAL)
  2713. {
  2714. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2715. }
  2716. }
  2717. }
  2718. ocpp_clear_errorCode_cmd(gunIndex);
  2719. }
  2720. void NormalStop(byte gunIndex)
  2721. {
  2722. PRINTF_FUNC("NormalStop (%d) \n", gunIndex);
  2723. }
  2724. //===============================================
  2725. // EmergencyStop and Charging Stop
  2726. //===============================================
  2727. void ChargingTerminalProcess(byte gunIndex)
  2728. {
  2729. SetChargerMode(gunIndex, SYS_MODE_TERMINATING);
  2730. }
  2731. void ChargingAlarmProcess(byte gunIndex)
  2732. {
  2733. CheckSystemErrorFunction(gunIndex);
  2734. ocpp_set_errCode_cmd(gunIndex);
  2735. SetChargerMode(gunIndex, SYS_MODE_ALARM);
  2736. }
  2737. void AcChargingTerminalProcess()
  2738. {
  2739. ac_chargingInfo[0]->SystemStatus = SYS_MODE_TERMINATING;
  2740. }
  2741. //void StopChargingProcessByString(byte level)
  2742. //{
  2743. // if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2744. // {
  2745. // ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2746. // }
  2747. //}
  2748. //void ReleaseChargingProcessByString(byte level)
  2749. //{
  2750. // if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2751. // ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2752. //}
  2753. // 一般錯誤停止充電處理函式
  2754. void BoardErrOccurByString(byte index, char *code)
  2755. {
  2756. //byte level = 1;
  2757. if ((chargingInfo[index]->SystemStatus > SYS_MODE_IDLE && chargingInfo[index]->SystemStatus < SYS_MODE_TERMINATING) ||
  2758. (chargingInfo[index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2759. {
  2760. PRINTF_FUNC("BoardErrOccurByString (%d) - (%s) \n", index, code);
  2761. if (strncmp(code, "023730", 6) == EQUAL &&
  2762. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2763. {
  2764. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2765. }
  2766. ChargingAlarmProcess(index);
  2767. }
  2768. //60StopChargingProcessByString(level);
  2769. }
  2770. // 急停狀況的停止充電處理函式
  2771. void EmcOccurThenStopCharging()
  2772. {
  2773. //byte level = 2;
  2774. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen...
  2775. // 其錯誤等級為 2
  2776. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2777. {
  2778. if ((chargingInfo[gun]->SystemStatus > SYS_MODE_IDLE && chargingInfo[gun]->SystemStatus < SYS_MODE_TERMINATING) ||
  2779. (chargingInfo[gun]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[gun]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2780. {
  2781. ChargingAlarmProcess(gun);
  2782. }
  2783. }
  2784. //StopChargingProcessByString(level);
  2785. }
  2786. void ReleaseBoardErrOccurByString(byte index, char *code)
  2787. {
  2788. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  2789. {
  2790. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2791. }
  2792. }
  2793. void ReleaseEmsOccureByString(byte index, char *code)
  2794. {
  2795. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2796. {
  2797. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2798. }
  2799. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2800. {
  2801. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2802. }
  2803. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2804. {
  2805. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2806. }
  2807. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2808. {
  2809. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2810. }
  2811. }
  2812. //===============================================
  2813. // 確認硬體 (按鈕) 狀態
  2814. //===============================================
  2815. bool leftBtnPush = false;
  2816. bool rightBtnPush = false;
  2817. byte _delayDetectSingleBtnCnt = 0;
  2818. bool twoBtnPress = 0;
  2819. bool btnDelayFlag = 0;
  2820. void ButtonTriggerEvent()
  2821. {
  2822. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS &&
  2823. ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS)
  2824. {
  2825. ShmSysConfigAndInfo->SysConfig.ShowInformation = YES;
  2826. twoBtnPress = TRUE;
  2827. }
  2828. else
  2829. {
  2830. ShmSysConfigAndInfo->SysConfig.ShowInformation = NO;
  2831. //Delay for detecting the single btn state after pressing two btn.
  2832. if (twoBtnPress == TRUE && _delayDetectSingleBtnCnt < 100)
  2833. _delayDetectSingleBtnCnt++;
  2834. else
  2835. {
  2836. if (twoBtnPress == TRUE)
  2837. twoBtnPress = FALSE;
  2838. _delayDetectSingleBtnCnt = 0;
  2839. }
  2840. }
  2841. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  2842. {
  2843. if(leftBtnPush == BTN_RELEASE)
  2844. {
  2845. leftBtnPush = BTN_PRESS;
  2846. PRINTF_FUNC("left btn down............................... \n");
  2847. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2848. {
  2849. switch(ac_chargingInfo[0]->SystemStatus)
  2850. {
  2851. case SYS_MODE_IDLE:
  2852. case SYS_MODE_MAINTAIN:
  2853. case SYS_MODE_RESERVATION:
  2854. {
  2855. if(isDetectPlugin())
  2856. {
  2857. _DetectPlugInTimeout();
  2858. StopSystemTimeoutDet();
  2859. }
  2860. }
  2861. break;
  2862. case SYS_MODE_MODE_REASSIGN_CHECK:
  2863. case SYS_MODE_REASSIGN:
  2864. case SYS_MODE_PREPARING:
  2865. case SYS_MODE_PREPARE_FOR_EV:
  2866. case SYS_MODE_PREPARE_FOR_EVSE:
  2867. case SYS_MODE_CCS_PRECHARGE_STEP0:
  2868. case SYS_MODE_CCS_PRECHARGE_STEP1:
  2869. {
  2870. // 取消充電
  2871. AcChargingTerminalProcess();
  2872. }
  2873. break;
  2874. case SYS_MODE_CHARGING:
  2875. {
  2876. if (ShmSysConfigAndInfo->SysConfig.StopChargingByButton == YES)
  2877. {
  2878. // 停止充電
  2879. AcChargingTerminalProcess();
  2880. }
  2881. }
  2882. break;
  2883. case SYS_MODE_COMPLETE:
  2884. {}
  2885. break;
  2886. }
  2887. }
  2888. else
  2889. {
  2890. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2891. {
  2892. case SYS_MODE_IDLE:
  2893. case SYS_MODE_MAINTAIN:
  2894. case SYS_MODE_RESERVATION:
  2895. {
  2896. if(isDetectPlugin())
  2897. {
  2898. _DetectPlugInTimeout();
  2899. StopSystemTimeoutDet();
  2900. btnDelayFlag = TRUE;
  2901. }
  2902. else
  2903. {
  2904. //LW_tpc plug detect
  2905. if (ShmDcCommonData->TpcDetectFunction == YES
  2906. && ShmSysConfigAndInfo->SysConfig.ShowInformation == NO && twoBtnPress == FALSE
  2907. && btnDelayFlag == FALSE)
  2908. {
  2909. if (ShmDcCommonData->_tpcPlugDetectState != _TPC_PLUG_DETECT_STATUS_WAIT)
  2910. {
  2911. PRINTF_FUNC("// Start Detect Plug in // \n");
  2912. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_WAIT;
  2913. GetTimespecFunc(&_tcpDetectStatus_time);
  2914. }
  2915. else
  2916. {
  2917. PRINTF_FUNC("// Stop Detect PlugIn for TPC. // \n");
  2918. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE;
  2919. StopGunInfoTimeoutDet(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2920. }
  2921. }
  2922. }
  2923. }
  2924. break;
  2925. case SYS_MODE_MODE_REASSIGN_CHECK:
  2926. case SYS_MODE_REASSIGN:
  2927. case SYS_MODE_PREPARING:
  2928. case SYS_MODE_PREPARE_FOR_EV:
  2929. case SYS_MODE_PREPARE_FOR_EVSE:
  2930. case SYS_MODE_CCS_PRECHARGE_STEP0:
  2931. case SYS_MODE_CCS_PRECHARGE_STEP1:
  2932. {
  2933. // 按鈕 - 取消充電
  2934. // if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2935. // AcChargingTerminalProcess();
  2936. // else
  2937. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2938. }
  2939. break;
  2940. case SYS_MODE_CHARGING:
  2941. {
  2942. if (ShmSysConfigAndInfo->SysConfig.StopChargingByButton == YES)
  2943. {
  2944. // 按鈕 - 停止充電
  2945. NormalStop(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2946. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2947. }
  2948. }
  2949. break;
  2950. case SYS_MODE_COMPLETE:
  2951. {
  2952. // 回 IDLE
  2953. //PRINTF_FUNC("right btn down.................SYS_MODE_COMPLETE \n");
  2954. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = SYS_MODE_IDLE;
  2955. }
  2956. break;
  2957. }
  2958. }
  2959. }
  2960. }
  2961. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  2962. {
  2963. if(leftBtnPush == BTN_PRESS)
  2964. {
  2965. leftBtnPush = BTN_RELEASE;
  2966. PRINTF_FUNC("left btn up............................... \n");
  2967. //LW_tpc plug detect
  2968. switch (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2969. {
  2970. case SYS_MODE_IDLE:
  2971. btnDelayFlag = FALSE;
  2972. break;
  2973. }
  2974. }
  2975. }
  2976. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  2977. {
  2978. if(rightBtnPush == BTN_RELEASE)
  2979. {
  2980. rightBtnPush = BTN_PRESS;
  2981. PRINTF_FUNC("right btn down .................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2982. if (ShmSysConfigAndInfo->SysInfo.PageIndex >= _LCM_AUTHORIZING &&
  2983. ShmSysConfigAndInfo->SysInfo.PageIndex <= _LCM_AUTHORIZ_FAIL)
  2984. return;
  2985. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2986. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  2987. ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 == ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2988. {
  2989. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2990. }
  2991. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2992. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  2993. {
  2994. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2995. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2996. }
  2997. else if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2998. {
  2999. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  3000. {
  3001. if (chargingInfo[_index]->SystemStatus != SYS_MODE_BOOTING &&
  3002. chargingInfo[_index]->SystemStatus != SYS_MODE_IDLE &&
  3003. chargingInfo[_index]->SystemStatus != SYS_MODE_RESERVATION)
  3004. {
  3005. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  3006. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3007. return;
  3008. }
  3009. }
  3010. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  3011. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3012. }
  3013. else
  3014. {
  3015. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  3016. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3017. }
  3018. }
  3019. }
  3020. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  3021. {
  3022. if(rightBtnPush == BTN_PRESS)
  3023. {
  3024. rightBtnPush = BTN_RELEASE;
  3025. PRINTF_FUNC("right btn up............................... \n");
  3026. }
  3027. }
  3028. }
  3029. void ChkPrimaryStatus()
  3030. {
  3031. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  3032. {
  3033. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  3034. EmcOccurThenStopCharging();
  3035. }
  3036. else
  3037. ReleaseEmsOccureByString(0, "012251");
  3038. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  3039. {
  3040. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  3041. EmcOccurThenStopCharging();
  3042. }
  3043. else
  3044. ReleaseEmsOccureByString(0, "012238");
  3045. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  3046. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  3047. else
  3048. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  3049. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL &&
  3050. ShmDcCommonData->_tiltSensorStep == _TILT_SENSOR_STEP_NONE)
  3051. {
  3052. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  3053. EmcOccurThenStopCharging();
  3054. }
  3055. else
  3056. ReleaseEmsOccureByString(0, "012252");
  3057. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  3058. {
  3059. EmcOccurThenStopCharging();
  3060. }
  3061. else
  3062. ReleaseEmsOccureByString(0, "012267");
  3063. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU)
  3064. {
  3065. EmcOccurThenStopCharging ();
  3066. }
  3067. else
  3068. ReleaseEmsOccureByString (0,"012346");
  3069. }
  3070. //===============================================
  3071. // 確認各小板偵測的錯誤狀況
  3072. //===============================================
  3073. void CheckSystemErrorFunction(byte index)
  3074. {
  3075. // System
  3076. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  3077. RecordAlarmCode(index, "012251");
  3078. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip)
  3079. RecordAlarmCode(index, "012238");
  3080. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen &&
  3081. ShmDcCommonData->_tiltSensorStep == _TILT_SENSOR_STEP_NONE)
  3082. RecordAlarmCode(index, "012252");
  3083. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  3084. RecordAlarmCode(index, "012267");
  3085. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU)
  3086. RecordAlarmCode(index, "012346");
  3087. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  3088. {
  3089. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  3090. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  3091. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  3092. {
  3093. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  3094. {
  3095. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  3096. }
  3097. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  3098. RecordAlarmCode(index, "012203");
  3099. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  3100. RecordAlarmCode(index, "012204");
  3101. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  3102. RecordAlarmCode(index, "012205");
  3103. chargingInfo[index]->StopChargeFlag = YES;
  3104. }
  3105. else
  3106. {
  3107. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  3108. {
  3109. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3110. }
  3111. }
  3112. }
  3113. else
  3114. {
  3115. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  3116. {
  3117. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3118. }
  3119. }
  3120. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  3121. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  3122. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  3123. {
  3124. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  3125. {
  3126. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  3127. }
  3128. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  3129. RecordAlarmCode(index, "012200");
  3130. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  3131. RecordAlarmCode(index, "012201");
  3132. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  3133. RecordAlarmCode(index, "012202");
  3134. chargingInfo[index]->StopChargeFlag = YES;
  3135. }
  3136. else
  3137. {
  3138. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  3139. {
  3140. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3141. }
  3142. }
  3143. }
  3144. //===============================================
  3145. // 確認 GPIO 狀態
  3146. //===============================================
  3147. void gpio_set_value(unsigned int gpio, unsigned int value)
  3148. {
  3149. int fd;
  3150. char buf[MAX_BUF];
  3151. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  3152. fd = open(buf, O_WRONLY);
  3153. if (fd < 0)
  3154. {
  3155. perror("gpio/set-value");
  3156. return;
  3157. }
  3158. if (value)
  3159. write(fd, "1", 2);
  3160. else
  3161. write(fd, "0", 2);
  3162. close(fd);
  3163. }
  3164. int gpio_get_value(unsigned int gpio, unsigned int *value)
  3165. {
  3166. int fd;
  3167. char buf[MAX_BUF];
  3168. char ch;
  3169. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  3170. fd = open(buf, O_RDONLY);
  3171. if (fd < 0) {
  3172. perror("gpio/get-value");
  3173. return fd;
  3174. }
  3175. read(fd, &ch, 1);
  3176. if (ch != '0') {
  3177. *value = 1;
  3178. } else {
  3179. *value = 0;
  3180. }
  3181. close(fd);
  3182. return 0;
  3183. }
  3184. void CheckGunTypeFromHw()
  3185. {
  3186. int pinIn[4] = { 22, 23, 44, 45 };
  3187. unsigned int gpioValue = 0;
  3188. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  3189. gpio_get_value(pinIn[i], &gpioValue);
  3190. {
  3191. switch (pinIn[i])
  3192. {
  3193. case 22:
  3194. bd1_1_status = gpioValue;
  3195. break;
  3196. case 23:
  3197. bd1_2_status = gpioValue;
  3198. break;
  3199. case 44:
  3200. bd0_1_status = gpioValue;
  3201. break;
  3202. case 45:
  3203. bd0_2_status = gpioValue;
  3204. break;
  3205. }
  3206. }
  3207. }
  3208. }
  3209. // Bypass useless functions
  3210. void CheckGpioInStatus()
  3211. {
  3212. int pinIn[2] = { 27, 47 };
  3213. unsigned int gpioValue = 0;
  3214. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  3215. {
  3216. gpio_get_value(pinIn[i], &gpioValue);
  3217. if (gpioValue == 0x01)
  3218. {
  3219. switch(pinIn[i])
  3220. {
  3221. // 小板緊急停止
  3222. case 47:
  3223. {
  3224. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3225. {
  3226. if (chargingInfo[i]->slotsIndex == 1)
  3227. {
  3228. // if (chargingInfo[i]->Type == _Type_Chademo)
  3229. // BoardErrOccurByString(i, "023730");
  3230. // else if (chargingInfo[i]->Type == _Type_CCS)
  3231. // BoardErrOccurByString(i, "013627");
  3232. break;
  3233. }
  3234. }
  3235. }
  3236. break;
  3237. case 27:
  3238. {
  3239. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3240. {
  3241. if (chargingInfo[i]->slotsIndex == 3)
  3242. {
  3243. // if (chargingInfo[i]->Type == _Type_Chademo)
  3244. // BoardErrOccurByString(i, "023730");
  3245. // else if (chargingInfo[i]->Type == _Type_CCS)
  3246. // BoardErrOccurByString(i, "013627");
  3247. break;
  3248. }
  3249. }
  3250. }
  3251. break;
  3252. }
  3253. }
  3254. else
  3255. {
  3256. switch (pinIn[i])
  3257. {
  3258. // 小板解除緊急停止
  3259. case 47:
  3260. {
  3261. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3262. {
  3263. if (chargingInfo[i]->slotsIndex == 1)
  3264. {
  3265. if (chargingInfo[i]->Type == _Type_Chademo)
  3266. ReleaseBoardErrOccurByString(i, "023730");
  3267. // else if (chargingInfo[i]->Type == _Type_CCS)
  3268. // ReleaseBoardErrOccurByString(i, "013627");
  3269. break;
  3270. }
  3271. }
  3272. }
  3273. break;
  3274. case 27:
  3275. {
  3276. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3277. {
  3278. if (chargingInfo[i]->slotsIndex == 3)
  3279. {
  3280. if (chargingInfo[i]->Type == _Type_Chademo)
  3281. ReleaseBoardErrOccurByString(i, "023730");
  3282. // else if (chargingInfo[i]->Type == _Type_CCS)
  3283. // ReleaseBoardErrOccurByString(i, "013627");
  3284. break;
  3285. }
  3286. }
  3287. }
  3288. break;
  3289. }
  3290. }
  3291. }
  3292. }
  3293. //===============================================
  3294. // Main process
  3295. //===============================================
  3296. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  3297. {
  3298. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  3299. {
  3300. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  3301. {
  3302. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  3303. return true;
  3304. }
  3305. }
  3306. for (byte index = 0; index < CCS_QUANTITY; index++)
  3307. {
  3308. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  3309. {
  3310. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  3311. return true;
  3312. }
  3313. }
  3314. for (byte index = 0; index < GB_QUANTITY; index++)
  3315. {
  3316. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  3317. {
  3318. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  3319. return true;
  3320. }
  3321. }
  3322. return false;
  3323. }
  3324. // 檢查 Byte 中某個 Bit 的值
  3325. // _byte : 欲改變的 byte
  3326. // _bit : 該 byte 的第幾個 bit
  3327. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  3328. {
  3329. return ( _byte & mask_table[_bit] ) != 0x00;
  3330. }
  3331. // 設定 Byte 中某個 Bit的值
  3332. // _byte : 欲改變的 byte
  3333. // _bit : 該 byte 的第幾個 bit
  3334. // value : 修改的值為 0 or 1
  3335. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  3336. {
  3337. if(value == 1)
  3338. *_byte |= (1 << _bit);
  3339. else if (value == 0)
  3340. *_byte ^= (1 << _bit);
  3341. }
  3342. void GetCcidAuthPossible(bool *idleReq, byte *idleIndex)
  3343. {
  3344. for (byte gun_index = 0;
  3345. gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  3346. gun_index ++)
  3347. {
  3348. //LW_GBT_EVCCID
  3349. if (((chargingInfo [gun_index]->SystemStatus == SYS_MODE_PREPARE_FOR_EV && chargingInfo[gun_index]->Type == _Type_CCS)
  3350. || (chargingInfo [gun_index]->SystemStatus == SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[gun_index]->Type == _Type_GB))
  3351. && ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  3352. {
  3353. *idleIndex = gun_index;
  3354. *idleReq = true;
  3355. break;
  3356. }
  3357. }
  3358. }
  3359. void UserScanFunction()
  3360. {
  3361. bool idleReq = false;
  3362. unsigned char idleIndex = 255;
  3363. //unsigned char stopReq = 255;
  3364. bool hasGunUsing = false;
  3365. bool isReservedCard = false;
  3366. // 當前非驗證的狀態
  3367. if(!IsAuthorizingMode())
  3368. {
  3369. // 先判斷現在是否可以提供刷卡
  3370. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  3371. // 2. 停止充電
  3372. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX ||
  3373. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_EMC)
  3374. {
  3375. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3376. return;
  3377. }
  3378. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  3379. {
  3380. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  3381. ac_chargingInfo[0]->SystemStatus != SYS_MODE_ALARM)
  3382. {
  3383. // 如果選 AC 槍
  3384. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  3385. ac_chargingInfo[0]->SystemStatus < SYS_MODE_ALARM)
  3386. {
  3387. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  3388. {
  3389. char value[32];
  3390. PRINTF_FUNC("ac stop charging \n");
  3391. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  3392. ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  3393. memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId, ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  3394. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  3395. {
  3396. AcChargingTerminalProcess();
  3397. }
  3398. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3399. }
  3400. }
  3401. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE ||
  3402. ac_chargingInfo[0]->SystemStatus == SYS_MODE_RESERVATION)
  3403. {
  3404. idleReq = true;
  3405. }
  3406. }
  3407. else
  3408. {
  3409. if (IsGunUsing(ShmSysConfigAndInfo->SysInfo.CurGunSelected))
  3410. {
  3411. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_CHARGING)
  3412. {
  3413. char value[32];
  3414. PRINTF_FUNC("stop charging \n");
  3415. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  3416. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  3417. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  3418. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  3419. // 同一張卡直接停掉
  3420. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  3421. {
  3422. NormalStop(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3423. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3424. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3425. }
  3426. else
  3427. {
  3428. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  3429. {
  3430. GetCcidAuthPossible(&idleReq, &idleIndex);
  3431. }
  3432. if (!idleReq)
  3433. {
  3434. PRINTF_FUNC("Remote check for stop session = %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3435. stopChargingChkByCard = true;
  3436. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  3437. ShmDcCommonData->stopSessionIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  3438. // 進入確認卡號狀態
  3439. AuthorizingStart();
  3440. return;
  3441. // PRINTF_FUNC ( "_LCM_AUTHORIZ_FAIL \n" );
  3442. // stopChargingChkByCard = true;
  3443. // strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  3444. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3445. }
  3446. }
  3447. }
  3448. else
  3449. {
  3450. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  3451. {
  3452. GetCcidAuthPossible(&idleReq, &idleIndex);
  3453. }
  3454. }
  3455. }
  3456. // else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE ||
  3457. // chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  3458. // {
  3459. // idleIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  3460. // idleReq = true;
  3461. // }
  3462. else
  3463. {
  3464. if (ShmDcCommonData->enObtainAuthorizbyCCID &&
  3465. (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZING ||
  3466. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZ_COMP ||
  3467. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZ_FAIL))
  3468. return;
  3469. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3470. {
  3471. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  3472. {
  3473. idleIndex = gun_index;
  3474. idleReq = true;
  3475. }
  3476. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3477. {
  3478. idleReq = true;
  3479. }
  3480. else
  3481. {
  3482. hasGunUsing = IsGunUsing(gun_index);
  3483. }
  3484. }
  3485. // 壁掛不給充 - 已經有至少一把槍在充電
  3486. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES &&
  3487. hasGunUsing)
  3488. {
  3489. PRINTF_FUNC("DW Serial not support dual charging. \n");
  3490. idleReq = false;
  3491. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3492. }
  3493. }
  3494. }
  3495. if (idleReq)
  3496. {
  3497. // if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  3498. // stopReq != 255 &&
  3499. // ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  3500. // {
  3501. // idleReq = false;
  3502. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3503. // }
  3504. // else
  3505. {
  3506. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gun_index++)
  3507. {
  3508. if (ac_chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3509. {
  3510. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3511. (char *) ShmDcCommonData->_reserved_ac_UserId [gun_index] ) == EQUAL)
  3512. {
  3513. isReservedCard = true;
  3514. // 驗證通過
  3515. ac_chargingInfo[gun_index]->ReservedStartFlag = YES;
  3516. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3517. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3518. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3519. break;
  3520. }
  3521. }
  3522. }
  3523. if (!isReservedCard)
  3524. {
  3525. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3526. {
  3527. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3528. {
  3529. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId,
  3530. (char *)ShmDcCommonData->_reserved_UserId[gun_index]) == EQUAL)
  3531. {
  3532. isReservedCard = true;
  3533. // 驗證通過
  3534. chargingInfo[gun_index]->ReservedStartFlag = YES;
  3535. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3536. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3537. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3538. break;
  3539. }
  3540. }
  3541. }
  3542. }
  3543. if (!isReservedCard &&
  3544. ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  3545. (idleIndex != 255 && (chargingInfo[idleIndex]->SystemStatus == SYS_MODE_IDLE ||
  3546. chargingInfo[idleIndex]->SystemStatus == SYS_MODE_PREPARE_FOR_EV ||
  3547. chargingInfo[idleIndex]->SystemStatus == SYS_MODE_PREPARE_FOR_EVSE)))) //LW_GBT_EVCCID
  3548. {
  3549. //LW_GB_EVCCID .. need to test it would be affect the ccs type or not.
  3550. if (ShmDcCommonData->_CcidAuthProcessStep != _CCID_AUTH)
  3551. {
  3552. PRINTF_FUNC("Start Authorizing...UserId = %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3553. // LCM => Authorizing
  3554. if (ShmDcCommonData->enObtainAuthorizbyCCID &&
  3555. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != DEFAULT_AC_INDEX)
  3556. {
  3557. ShmSysConfigAndInfo->SysInfo.CurGunSelected = idleIndex;
  3558. }
  3559. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  3560. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTH;
  3561. // 進入確認卡號狀態
  3562. AuthorizingStart();
  3563. }
  3564. }
  3565. else
  3566. {
  3567. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3568. }
  3569. }
  3570. }
  3571. else
  3572. {
  3573. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3574. }
  3575. }
  3576. }
  3577. }
  3578. unsigned char isModeChange(unsigned char gun_index)
  3579. {
  3580. unsigned char result = NO;
  3581. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  3582. {
  3583. result = YES;
  3584. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  3585. }
  3586. return result;
  3587. }
  3588. void ScannerCardProcess()
  3589. {
  3590. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL &&
  3591. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3592. {
  3593. isCardScan = true;
  3594. // 處理刷卡及驗證卡號的動作
  3595. UserScanFunction();
  3596. }
  3597. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3598. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3599. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  3600. {
  3601. StartSystemTimeoutDet(Timeout_Authorizing);
  3602. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  3603. ShmDcCommonData->stopSessionIndex == NO_DEFINE)
  3604. {
  3605. // 白名單驗證
  3606. for (int i = 0; i < 10; i++)
  3607. {
  3608. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  3609. {
  3610. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  3611. {
  3612. PRINTF_FUNC("White Card Pass (%s)... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3613. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3614. ClearAuthorizedFlag();
  3615. break;
  3616. }
  3617. }
  3618. }
  3619. }
  3620. // 確認驗證卡號完成沒
  3621. if (isAuthorizedComplete() || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3622. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3623. {
  3624. StopSystemTimeoutDet();
  3625. // 判斷後台回覆狀態
  3626. if(ocpp_chk_authrization_cmd() || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3627. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3628. {
  3629. if (ShmDcCommonData->stopSessionIndex != NO_DEFINE &&
  3630. ShmDcCommonData->stopSessionIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3631. {
  3632. if (CheckTheParentId(ShmDcCommonData->stopSessionIndex))
  3633. {
  3634. // LCM => Authorize complete
  3635. PRINTF_FUNC ( "_LCM_AUTHORIZ_COMP (stop) \n" );
  3636. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3637. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  3638. }
  3639. else
  3640. {
  3641. // LCM => Authorize fail
  3642. PRINTF_FUNC ( "_LCM_AUTHORIZ_FAIL (stop) \n" );
  3643. ShmDcCommonData->stopSessionIndex = NO_DEFINE;
  3644. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3645. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3646. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  3647. }
  3648. }
  3649. else
  3650. {
  3651. //bool using
  3652. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  3653. {
  3654. if ((chargingInfo[_index]->SystemStatus == SYS_MODE_IDLE || chargingInfo[_index]->SystemStatus == SYS_MODE_RESERVATION) &&
  3655. ShmDcCommonData->remoteStartAuthFlag[_index])
  3656. {
  3657. chargingInfo[_index]->RemoteStartFlag = YES;
  3658. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3659. }
  3660. }
  3661. // LCM => Authorize complete
  3662. PRINTF_FUNC("_LCM_AUTHORIZ_COMP \n");
  3663. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3664. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  3665. }
  3666. }
  3667. else
  3668. {
  3669. // LCM => Authorize fail
  3670. PRINTF_FUNC("_LCM_AUTHORIZ_FAIL \n");
  3671. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3672. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3673. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  3674. }
  3675. ClearAuthorizedFlag();
  3676. }
  3677. }
  3678. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  3679. {
  3680. StartSystemTimeoutDet(Timeout_VerifyFail);
  3681. isCardScan = false;
  3682. }
  3683. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  3684. {
  3685. StartSystemTimeoutDet(Timeout_VerifyComp);
  3686. }
  3687. else if(ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  3688. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  3689. {
  3690. StartSystemTimeoutDet(Timeout_WaitPlug);
  3691. }
  3692. else
  3693. {
  3694. isCardScan = false;
  3695. }
  3696. }
  3697. bool AddGunInfoByConnector(byte typeValue, byte slots)
  3698. {
  3699. bool result = true;
  3700. switch (typeValue)
  3701. {
  3702. case '0': // none
  3703. break;
  3704. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  3705. break;
  3706. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  3707. break;
  3708. case '3': // IEC 62196-2 Type 2 Plug
  3709. case '4': // IEC 62196-2 Type 2 Socket
  3710. if (AC_QUANTITY > _ac_Index)
  3711. {
  3712. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  3713. // AC 固定 index
  3714. ac_chargingInfo[_acgunIndex]->Index = DEFAULT_AC_INDEX;
  3715. ac_chargingInfo[_acgunIndex]->ReservationId = NO_RESERVE;
  3716. ac_chargingInfo[_acgunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3717. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  3718. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  3719. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3720. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3721. _ac_Index++;
  3722. _acgunIndex++;
  3723. }
  3724. else
  3725. result = false;
  3726. break;
  3727. case '5': // GB/T AC Plug
  3728. break;
  3729. case '6': // GB/T AC Socket
  3730. break;
  3731. case 'J': // CHAdeMO - 60A
  3732. case 'K': // CHAdeMO - 200A / 500V
  3733. case 'L': // CHAdeMO - 80A
  3734. case 'S': // CHAdeMO - 125A (boost : 200A)
  3735. case 'W': // CHAdeMO - 200a (boost : 350A)
  3736. {
  3737. if (CHAdeMO_QUANTITY > _chademoIndex)
  3738. {
  3739. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  3740. chargingInfo[_gunIndex]->Index = _gunIndex;
  3741. chargingInfo[_gunIndex]->ReservationId = NO_RESERVE;
  3742. chargingInfo[_gunIndex]->slotsIndex = slots;
  3743. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3744. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  3745. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  3746. chargingInfo[_gunIndex]->IsAvailable = YES;
  3747. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3748. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3749. chargingInfo[_gunIndex]->ModelType = typeValue;
  3750. ShmDcCommonData->TpcDetectFunction = YES; //LW_tpc plug detect
  3751. _chademoIndex++;
  3752. _gunIndex++;
  3753. }
  3754. else
  3755. result = false;
  3756. }
  3757. break;
  3758. case 'U': // Natural cooling CCS1
  3759. case 'E': // Natural cooling CCS2
  3760. case 'T': // Rema CCS1 - 300A
  3761. case 'D': // Rema CCS2 - 300A
  3762. case 'M': // CCS2 - 80A
  3763. case 'N': // CCS1 - 80A
  3764. case 'R': // CCS2 - 250A
  3765. case 'V' : // Liquid cooling CCS1 - 500A
  3766. case 'F' : // Liquid cooling CCS2 - 500A
  3767. case 'Y' : // CCS1 - 150A
  3768. case 'Z' : // CCS2 - 150A
  3769. case 'I' : // CCS1 - 300A ~ 500A (Boost mode)
  3770. case 'Q' : // CCS2 - 300A ~ 500A (Boost mode)
  3771. {
  3772. if (CCS_QUANTITY > _ccsIndex)
  3773. {
  3774. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  3775. chargingInfo[_gunIndex]->Index = _gunIndex;
  3776. chargingInfo[_gunIndex]->ReservationId = NO_RESERVE;
  3777. chargingInfo[_gunIndex]->slotsIndex = slots;
  3778. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3779. chargingInfo[_gunIndex]->Type = _Type_CCS;
  3780. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  3781. chargingInfo[_gunIndex]->IsAvailable = YES;
  3782. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3783. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3784. chargingInfo[_gunIndex]->ModelType = typeValue;
  3785. // 現階段預設為走 DIN70121
  3786. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  3787. if (typeValue == 'U' || typeValue == 'T' ||
  3788. typeValue == 'N' || typeValue == 'V' ||
  3789. typeValue == 'Y' || typeValue == 'I')
  3790. ShmDcCommonData->CcsTypeSaved[_gunIndex] = _CCS_TYPE_CCS1;
  3791. else if (typeValue == 'E' || typeValue == 'D' ||
  3792. typeValue == 'M' || typeValue == 'R' ||
  3793. typeValue == 'F' || typeValue == 'Z' ||
  3794. typeValue == 'Q')
  3795. ShmDcCommonData->CcsTypeSaved[_gunIndex] = _CCS_TYPE_CCS2;
  3796. _ccsIndex++;
  3797. _gunIndex++;
  3798. }
  3799. else
  3800. result = false;
  3801. }
  3802. break;
  3803. case 'G': // GBT DC
  3804. case 'B':
  3805. {
  3806. if (GB_QUANTITY > _gb_Index)
  3807. {
  3808. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  3809. chargingInfo[_gunIndex]->Index = _gunIndex;
  3810. chargingInfo[_gunIndex]->ReservationId = NO_RESERVE;
  3811. chargingInfo[_gunIndex]->slotsIndex = slots;
  3812. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3813. chargingInfo[_gunIndex]->Type = _Type_GB;
  3814. chargingInfo[_gunIndex]->type_index = _gb_Index;
  3815. chargingInfo[_gunIndex]->IsAvailable = YES;
  3816. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3817. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3818. chargingInfo[_gunIndex]->ModelType = typeValue;
  3819. _gb_Index++;
  3820. _gunIndex++;
  3821. }
  3822. else
  3823. result = false;
  3824. }
  3825. break;
  3826. }
  3827. return result;
  3828. }
  3829. bool CheckConnectorTypeStatus()
  3830. {
  3831. bool result = true;
  3832. // 硬體 0 1 => CCS,1 0 => Chademo,1 1 => GB
  3833. //CheckFwSlotSta.tusLog();
  3834. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  3835. {
  3836. byte slots = 1;
  3837. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  3838. {
  3839. if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  3840. {
  3841. return false;
  3842. }
  3843. slots++;
  3844. }
  3845. // AC index 接在 DC 後面
  3846. // if (_ac_Index > 0)
  3847. // ac_chargingInfo[0]->Index += _gunIndex;
  3848. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3849. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  3850. PRINTF_FUNC("Charger Count => DC-Count = %d, AC-Count = %d \n",
  3851. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  3852. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  3853. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  3854. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  3855. result = false;
  3856. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3857. {
  3858. bool isFind = false;
  3859. if (chargingInfo[0]->Type == _Type_Chademo)
  3860. {
  3861. if ((bd0_1_status == 0 && bd0_2_status == 1) ||
  3862. (bd1_1_status == 0 && bd1_2_status == 1))
  3863. {
  3864. isFind = true;
  3865. }
  3866. }
  3867. else if (chargingInfo[0]->Type == _Type_CCS)
  3868. {
  3869. if ((bd0_1_status == 1 && bd0_2_status == 0) ||
  3870. (bd1_1_status == 1 && bd1_2_status == 0))
  3871. {
  3872. isFind = true;
  3873. }
  3874. }
  3875. else if (chargingInfo[0]->Type == _Type_GB)
  3876. {
  3877. if ((bd0_1_status == 1 && bd0_2_status == 1) ||
  3878. (bd1_1_status == 1 && bd1_2_status == 1))
  3879. {
  3880. isFind = true;
  3881. }
  3882. }
  3883. if (isFind)
  3884. {
  3885. chargingInfo[0]->Evboard_id = 0x01;
  3886. CheckHwSlotStatusLog(0);
  3887. }
  3888. else
  3889. {
  3890. PRINTF_FUNC("HW Conn, Flag1 = %d, Flag2 = %d \n", bd0_1_status, bd0_2_status);
  3891. PRINTF_FUNC("HW Conn, Flag1 = %d, Flag2 = %d \n", bd1_1_status, bd1_2_status);
  3892. result = false;
  3893. }
  3894. }
  3895. else
  3896. {
  3897. char type = NO_DEFINE;
  3898. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  3899. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  3900. {
  3901. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  3902. {
  3903. PRINTF_FUNC("HW Conn %d (Chademo) \n", gunIndex);
  3904. // 與硬體相同 type : Chademo
  3905. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3906. {
  3907. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3908. }
  3909. }
  3910. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  3911. {
  3912. PRINTF_FUNC("HW Conn %d (CCS) \n", gunIndex);
  3913. // 與硬體相同 type : CCS
  3914. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  3915. {
  3916. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3917. }
  3918. }
  3919. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  3920. {
  3921. PRINTF_FUNC("HW Conn %d (GB) \n", gunIndex);
  3922. // 與硬體相同 type : GB
  3923. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3924. {
  3925. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3926. }
  3927. }
  3928. else if (gunIndex == 0)
  3929. {
  3930. PRINTF_FUNC("HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n",
  3931. gunIndex, bd0_1_status, bd0_2_status);
  3932. }
  3933. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  3934. {
  3935. PRINTF_FUNC("HW Conn %d (Chademo) \n", gunIndex);
  3936. // 與硬體相同 type : Chademo
  3937. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3938. {
  3939. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3940. }
  3941. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3942. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3943. }
  3944. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  3945. {
  3946. PRINTF_FUNC("HW Conn %d (CCS) \n", gunIndex);
  3947. // 與硬體相同 type : CCS
  3948. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  3949. {
  3950. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3951. }
  3952. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3953. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3954. }
  3955. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  3956. {
  3957. PRINTF_FUNC("HW Conn %d (GB) \n", gunIndex);
  3958. // 與硬體相同 type : GB
  3959. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3960. {
  3961. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3962. }
  3963. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3964. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3965. }
  3966. else if (gunIndex == 1)
  3967. {
  3968. PRINTF_FUNC("HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n",
  3969. gunIndex, bd1_1_status, bd1_2_status);
  3970. }
  3971. CheckHwSlotStatusLog(gunIndex);
  3972. if (type == NO_DEFINE)
  3973. type = chargingInfo[gunIndex]->Type;
  3974. else
  3975. {
  3976. // 雙槍都是同一個 type
  3977. if (type == chargingInfo[gunIndex]->Type)
  3978. ShmDcCommonData->SysGunAreSameType = YES;
  3979. }
  3980. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  3981. result = false;
  3982. }
  3983. }
  3984. }
  3985. else
  3986. {
  3987. // Module Name 不正確 - 告警
  3988. result = false;
  3989. }
  3990. return result;
  3991. }
  3992. void KillTask()
  3993. {
  3994. ChangeLcmByIndex(_LCM_FIX);
  3995. system("killall Module_EventLogging");
  3996. system("killall Module_PrimaryComm");
  3997. system("killall Module_EvComm");
  3998. system("killall Module_InternalComm");
  3999. system("killall Module_PsuComm");
  4000. system("killall Module_Module_SmartBox");
  4001. system("killall Module_4g &");
  4002. system("killall Module_Wifi &");
  4003. system("killall Module_Payment_Bazel8 &");
  4004. }
  4005. void KillTaskExceptDetectTask()
  4006. {
  4007. ChangeLcmByIndex(_LCM_FIX);
  4008. sleep(3);
  4009. system("killall Module_EvComm");
  4010. //system("killall Module_InternalComm");
  4011. system("killall Module_PsuComm");
  4012. system("killall Module_Module_SmartBox");
  4013. system("killall Module_4g &");
  4014. system("killall Module_Wifi &");
  4015. system("killall Module_Payment_Bazel8 &");
  4016. }
  4017. void KillAllTask()
  4018. {
  4019. ChangeLcmByIndex(_LCM_FIX);
  4020. system("killall Module_EventLogging");
  4021. system("killall Module_PrimaryComm");
  4022. system("killall Module_EvComm");
  4023. system("killall Module_LcmControl");
  4024. system("killall Module_InternalComm");
  4025. system("killall Module_PsuComm");
  4026. system("killall Module_Module_SmartBox");
  4027. system("killall OcppBackend &");
  4028. system("killall Module_4g &");
  4029. system("killall Module_Wifi &");
  4030. system("killall Module_DcMeter &");
  4031. system("killall Module_Payment_Bazel8 &");
  4032. }
  4033. bool CreateBufferForLcmFile(char *file)
  4034. {
  4035. bool result = FAIL;
  4036. unsigned char *ptr = NULL;
  4037. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  4038. int fd, fd_lcm;
  4039. int wrd_lcm;
  4040. char Buf_lcm[128] = "/mnt/BUFFER.zip";
  4041. fd = open ( file, O_RDONLY );
  4042. if (fd < 0)
  4043. {
  4044. close ( fd );
  4045. return result;
  4046. }
  4047. ptr = malloc(MaxLen);
  4048. ImageLen = read(fd, ptr, MaxLen);
  4049. fd_lcm = open ( Buf_lcm, O_RDWR | O_CREAT | O_EXCL );
  4050. if (fd_lcm < 0)
  4051. {
  4052. PRINTF_FUNC ( "Can not create %s \n", Buf_lcm );
  4053. }
  4054. else
  4055. {
  4056. PRINTF_FUNC ( "Writing data to %s file.... \n", Buf_lcm );
  4057. wrd_lcm = write ( fd_lcm, ptr + 48, ImageLen - 48 );
  4058. close ( fd_lcm );
  4059. PRINTF_FUNC ( ">> %s Written length : 0x%x \n", Buf_lcm, wrd_lcm );
  4060. if (wrd_lcm != (ImageLen - 48))
  4061. {
  4062. result = FAIL;
  4063. }
  4064. else
  4065. {
  4066. result = PASS;
  4067. }
  4068. close(fd);
  4069. free(ptr);
  4070. }
  4071. return result;
  4072. }
  4073. void LogUpgradeType(unsigned int type)
  4074. {
  4075. switch (type)
  4076. {
  4077. case _UPDATE_TYPE_CSU_UBOOT: PRINTF_FUNC("CSU bootloader (uboot) \n"); break;
  4078. case _UPDATE_TYPE_CSU_DTB: PRINTF_FUNC("CSU kernel configuration (dtb) \n"); break;
  4079. case _UPDATE_TYPE_CSU_ZIMAGE: PRINTF_FUNC("CSU kernel image (zImage) \n"); break;
  4080. case _UPDATE_TYPE_CSU_RFILES: PRINTF_FUNC("CSU root file system (ramdisk.gz) \n"); break;
  4081. case _UPDATE_TYPE_CSU_CONFIG: PRINTF_FUNC("CSU user configuration (bin) \n"); break;
  4082. case _UPDATE_TYPE_CCS_UBOOT: PRINTF_FUNC("CCS board bootloader (uboot) \n"); break;
  4083. case _UPDATE_TYPE_CCS_DTB: PRINTF_FUNC("CCS board kernel configuration (dtb) \n"); break;
  4084. case _UPDATE_TYPE_CCS_ZIMAGE: PRINTF_FUNC("CCS board kernel image (zImage) \n"); break;
  4085. case _UPDATE_TYPE_CCS_RFILES: PRINTF_FUNC("CCS board file system (ramdisk.gz) \n"); break;
  4086. case _UPDATE_TYPE_DCM_ST407: PRINTF_FUNC("DCM 407 primary controller \n"); break;
  4087. case _UPDATE_TYPE_RELAY_CTR: PRINTF_FUNC("Relay control board \n"); break;
  4088. case _UPDATE_TYPE_FAN_CTR: PRINTF_FUNC("Fan control board \n"); break;
  4089. case _UPDATE_TYPE_AC_WLMOUNT_CTR: PRINTF_FUNC("AC wall-mount (low-end) controller \n"); break;
  4090. case _UPDATE_TYPE_LED_CTR: PRINTF_FUNC("LED control board \n"); break;
  4091. case _UPDATE_TYPE_CHADEMO: PRINTF_FUNC("CAHdeMO board \n"); break;
  4092. case _UPDATE_TYPE_GBT: PRINTF_FUNC("GBT board \n"); break;
  4093. case _UPDATE_TYPE_DWIN_LCM: PRINTF_FUNC("LCM Module \n"); break;
  4094. }
  4095. }
  4096. int CheckUpdateProcess(void)
  4097. {
  4098. //bool isPass = true;
  4099. uint8_t retSucc = 0;
  4100. uint8_t retFail = 0;
  4101. byte index = 0;
  4102. byte target = 0;
  4103. char new_str[256];
  4104. unsigned char *ptr = NULL;
  4105. int fd = 0;
  4106. int CanFd = 0;
  4107. int uartFd = 0;
  4108. unsigned int Type = 0;
  4109. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  4110. DIR *d;
  4111. struct dirent *dir;
  4112. byte result = PASS;
  4113. char Buf[256];
  4114. d = opendir("/mnt/");
  4115. if (d)
  4116. {
  4117. while ((dir = readdir(d)) != NULL)
  4118. {
  4119. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
  4120. {
  4121. continue;
  4122. }
  4123. memset(new_str, 0x00, sizeof(new_str));
  4124. strcat(new_str, "/mnt/");
  4125. strcat(new_str, dir->d_name);
  4126. PRINTF_FUNC("%s \n", new_str);
  4127. fd = open(new_str, O_RDONLY);
  4128. if (fd < 0)
  4129. {
  4130. close(fd);
  4131. result = FAIL;
  4132. sleep(1);
  4133. continue;
  4134. }
  4135. ptr = malloc(MaxLen); //-48 is take out the header
  4136. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  4137. //get the image length
  4138. ImageLen = read(fd, ptr, MaxLen);
  4139. bool isModelNameFail = false;
  4140. // DS-----J0E-2PH
  4141. if((ShmSysConfigAndInfo->SysConfig.ModelName[0] != ptr[0]) ||
  4142. (ShmSysConfigAndInfo->SysConfig.ModelName[1] != ptr[1]) ||
  4143. (ShmSysConfigAndInfo->SysConfig.ModelName[7] != ptr[7]) ||
  4144. (ShmSysConfigAndInfo->SysConfig.ModelName[8] != ptr[8]) ||
  4145. (ShmSysConfigAndInfo->SysConfig.ModelName[9] != ptr[9]) ||
  4146. (ShmSysConfigAndInfo->SysConfig.ModelName[11] != ptr[11]) ||
  4147. (ShmSysConfigAndInfo->SysConfig.ModelName[12] != ptr[12]) ||
  4148. (ShmSysConfigAndInfo->SysConfig.ModelName[13] != ptr[13]))
  4149. {
  4150. result = MODELNAME_FAIL;
  4151. isModelNameFail = true;
  4152. }
  4153. if (isModelNameFail)
  4154. {
  4155. close(fd);
  4156. continue;
  4157. }
  4158. PRINTF_FUNC("model name check pass. \n");
  4159. if (ImageLen > 20) {
  4160. Type = (((unsigned int)ptr[16]) << 24 |
  4161. ((unsigned int)ptr[17]) << 16 |
  4162. ((unsigned int)ptr[18]) << 8 |
  4163. ((unsigned int)ptr[19]));
  4164. LogUpgradeType(Type);
  4165. PRINTF_FUNC("Upgrade Status : PASS : %d, FAIL : %d \n", retSucc, retFail);
  4166. free(ptr);
  4167. switch (Type)
  4168. {
  4169. case _UPDATE_TYPE_CSU_UBOOT:
  4170. case _UPDATE_TYPE_CSU_DTB:
  4171. case _UPDATE_TYPE_CSU_ZIMAGE:
  4172. case _UPDATE_TYPE_CSU_RFILES:
  4173. case _UPDATE_TYPE_CSU_CONFIG:
  4174. {
  4175. system("echo 3 > /proc/sys/vm/drop_caches");
  4176. sleep(2);
  4177. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  4178. retSucc++;
  4179. else
  4180. {
  4181. PRINTF_FUNC("Upgrade CSU Failed (%d) \n", Type);
  4182. retFail++;
  4183. }
  4184. memset(Buf, 0, sizeof(Buf));
  4185. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4186. system(Buf);
  4187. }
  4188. break;
  4189. case _UPDATE_TYPE_CCS_UBOOT:
  4190. case _UPDATE_TYPE_CCS_DTB:
  4191. case _UPDATE_TYPE_CCS_ZIMAGE:
  4192. case _UPDATE_TYPE_CCS_RFILES:
  4193. {
  4194. CanFd = InitCanBus();
  4195. if (CanFd > 0)
  4196. {
  4197. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  4198. {
  4199. if (chargingInfo[index]->Type == _Type_CCS)
  4200. {
  4201. byte targetID = chargingInfo[index]->Evboard_id;
  4202. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1 &&
  4203. ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0)
  4204. {
  4205. targetID += 1;
  4206. }
  4207. system("echo 3 > /proc/sys/vm/drop_caches");
  4208. sleep(2);
  4209. PRINTF_FUNC("Upgrade CCS Processing..target id = %d \n", targetID);
  4210. if (Upgrade_CCS(CanFd, Type, targetID, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL)
  4211. {
  4212. PRINTF_FUNC("Upgrade CCS Failed \n");
  4213. retFail++;
  4214. }
  4215. else
  4216. retSucc++;
  4217. }
  4218. }
  4219. close(CanFd);
  4220. }
  4221. memset(Buf, 0, sizeof(Buf));
  4222. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4223. system(Buf);
  4224. }
  4225. break;
  4226. case _UPDATE_TYPE_DCM_ST407:
  4227. case _UPDATE_TYPE_RELAY_CTR:
  4228. case _UPDATE_TYPE_FAN_CTR:
  4229. case _UPDATE_TYPE_AC_WLMOUNT_CTR:
  4230. case _UPDATE_TYPE_LED_CTR:
  4231. {
  4232. target = 0x00;
  4233. if (Type == _UPDATE_TYPE_DCM_ST407) {
  4234. target = UPGRADE_PRI;
  4235. } else if (Type == _UPDATE_TYPE_RELAY_CTR) {
  4236. target = UPGRADE_RB;
  4237. } else if (Type == _UPDATE_TYPE_FAN_CTR) {
  4238. target = UPGRADE_FAN;
  4239. } else if (Type == _UPDATE_TYPE_AC_WLMOUNT_CTR) {
  4240. target = UPGRADE_AC;
  4241. } else if (Type == _UPDATE_TYPE_LED_CTR) {
  4242. target = UPGRADE_LED;
  4243. }
  4244. uartFd = InitComPort(target);
  4245. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  4246. {
  4247. retSucc++;
  4248. }
  4249. else
  4250. {
  4251. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  4252. retFail++;
  4253. }
  4254. if (uartFd > 0)
  4255. {
  4256. close(uartFd);
  4257. }
  4258. memset(Buf, 0, sizeof(Buf));
  4259. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4260. system(Buf);
  4261. }
  4262. break;
  4263. case _UPDATE_TYPE_CHADEMO:
  4264. case _UPDATE_TYPE_GBT:
  4265. {
  4266. CanFd = InitCanBus();
  4267. if (CanFd > 0)
  4268. {
  4269. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  4270. {
  4271. if ((Type == _UPDATE_TYPE_CHADEMO && chargingInfo[index]->Type == _Type_Chademo) ||
  4272. (Type == _UPDATE_TYPE_GBT && chargingInfo[index]->Type == _Type_GB))
  4273. {
  4274. PRINTF_FUNC("Upgrade GBT / Chademo Processing..target id = %d \n", chargingInfo[index]->Evboard_id);
  4275. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  4276. retSucc++;
  4277. else
  4278. {
  4279. if (chargingInfo[index]->Type == _Type_Chademo)
  4280. PRINTF_FUNC("Upgrade Chademo Failed \n");
  4281. else
  4282. PRINTF_FUNC("Upgrade GBT Failed \n");
  4283. retFail++;
  4284. }
  4285. }
  4286. }
  4287. close(CanFd);
  4288. }
  4289. memset(Buf, 0, sizeof(Buf));
  4290. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4291. system(Buf);
  4292. }
  4293. break;
  4294. case _UPDATE_TYPE_DWIN_LCM:
  4295. {
  4296. if (CreateBufferForLcmFile(new_str))
  4297. {
  4298. runShellCmd ( "mkdir -p /mnt/lcd" );
  4299. runShellCmd ( "unzip /mnt/BUFFER.zip -d /mnt/lcd" );
  4300. PRINTF_FUNC ( "Start upgrade LCM files. \n" );
  4301. ShmDcCommonData->_upgrade_lcm_flag = YES;
  4302. while (ShmDcCommonData->_upgrade_lcm_result == _LCM_UPGRADE_RESULT_WAIT)
  4303. {
  4304. sleep (5);
  4305. }
  4306. if (ShmDcCommonData->_upgrade_lcm_result == _LCM_UPGRADE_RESULT_PASS)
  4307. retSucc ++;
  4308. else
  4309. retFail ++;
  4310. }
  4311. }
  4312. break;
  4313. }
  4314. }
  4315. close(fd);
  4316. }
  4317. }
  4318. closedir(d);
  4319. PRINTF_FUNC("Upgrade Result : PASS : %d, FAIL : %d \n", retSucc, retFail);
  4320. sleep(1);
  4321. system("rm -rvf /mnt/* ");
  4322. if (retFail != 0)
  4323. {
  4324. result = FAIL;
  4325. }
  4326. return result;
  4327. }
  4328. void CreateRfidFork()
  4329. {
  4330. pid_t rfidRecPid;
  4331. rfidRecPid = fork();
  4332. if (rfidRecPid == 0)
  4333. {
  4334. int module_type = MODULE_EWT;
  4335. while(true)
  4336. {
  4337. // 刷卡判斷
  4338. RFID rfid;
  4339. if (!ShmSysConfigAndInfo->SysConfig.isRFID ||
  4340. _verifyMechanism != VERIFY_MECHANISM_RFID)
  4341. {}
  4342. else if(getRequestCardSN(rfidFd, module_type, &rfid))
  4343. {
  4344. //PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4345. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  4346. {
  4347. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  4348. {
  4349. switch (rfid.snType)
  4350. {
  4351. case RFID_SN_TYPE_6BYTE:
  4352. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4353. "%02X%02X%02X%02X%02X%02X",
  4354. rfid.currentCard[0], rfid.currentCard[1],
  4355. rfid.currentCard[2], rfid.currentCard[3],
  4356. rfid.currentCard[4], rfid.currentCard[5]);
  4357. break;
  4358. case RFID_SN_TYPE_7BYTE:
  4359. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4360. "%02X%02X%02X%02X%02X%02X%02X",
  4361. rfid.currentCard[0], rfid.currentCard[1],
  4362. rfid.currentCard[2], rfid.currentCard[3],
  4363. rfid.currentCard[4], rfid.currentCard[5],
  4364. rfid.currentCard[6]);
  4365. break;
  4366. case RFID_SN_TYPE_8BYTE :
  4367. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4368. "%02X%02X%02X%02X%02X%02X%02X%02X",
  4369. rfid.currentCard[0], rfid.currentCard[1],
  4370. rfid.currentCard[2], rfid.currentCard[3],
  4371. rfid.currentCard[4], rfid.currentCard[5],
  4372. rfid.currentCard[6], rfid.currentCard[7]);
  4373. break;
  4374. case RFID_SN_TYPE_10BYTE:
  4375. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4376. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  4377. rfid.currentCard[0], rfid.currentCard[1],
  4378. rfid.currentCard[2], rfid.currentCard[3],
  4379. rfid.currentCard[4], rfid.currentCard[5],
  4380. rfid.currentCard[6], rfid.currentCard[7],
  4381. rfid.currentCard[8], rfid.currentCard[9]);
  4382. break;
  4383. case RFID_SN_TYPE_4BYTE:
  4384. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4385. "%02X%02X%02X%02X",
  4386. rfid.currentCard[0], rfid.currentCard[1],
  4387. rfid.currentCard[2], rfid.currentCard[3]);
  4388. break;
  4389. }
  4390. }
  4391. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  4392. {
  4393. switch (rfid.snType)
  4394. {
  4395. case RFID_SN_TYPE_6BYTE:
  4396. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4397. "%02X%02X%02X%02X%02X%02X",
  4398. rfid.currentCard[5], rfid.currentCard[4],
  4399. rfid.currentCard[3], rfid.currentCard[2],
  4400. rfid.currentCard[1], rfid.currentCard[0]);
  4401. break;
  4402. case RFID_SN_TYPE_7BYTE:
  4403. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4404. "%02X%02X%02X%02X%02X%02X%02X",
  4405. rfid.currentCard[6], rfid.currentCard[5],
  4406. rfid.currentCard[4], rfid.currentCard[3],
  4407. rfid.currentCard[2], rfid.currentCard[1],
  4408. rfid.currentCard[0]);
  4409. break;
  4410. case RFID_SN_TYPE_8BYTE:
  4411. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4412. "%02X%02X%02X%02X%02X%02X%02X%02X",
  4413. rfid.currentCard [7], rfid.currentCard [6],
  4414. rfid.currentCard [5], rfid.currentCard [4],
  4415. rfid.currentCard [3], rfid.currentCard [2],
  4416. rfid.currentCard [1], rfid.currentCard [0]);
  4417. break;
  4418. case RFID_SN_TYPE_10BYTE:
  4419. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4420. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  4421. rfid.currentCard[9], rfid.currentCard[8],
  4422. rfid.currentCard[7], rfid.currentCard[6],
  4423. rfid.currentCard[5], rfid.currentCard[4],
  4424. rfid.currentCard[3], rfid.currentCard[2],
  4425. rfid.currentCard[1], rfid.currentCard[0]);
  4426. break;
  4427. case RFID_SN_TYPE_4BYTE:
  4428. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4429. "%02X%02X%02X%02X",
  4430. rfid.currentCard[3], rfid.currentCard[2],
  4431. rfid.currentCard[1], rfid.currentCard[0]);
  4432. break;
  4433. }
  4434. }
  4435. PRINTF_FUNC("RFID : Get card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4436. }
  4437. if (rfid.cardType == ISO14443A)
  4438. sethaltCard ( rfidFd, module_type);
  4439. else if (rfid.cardType == IS014443B)
  4440. { }
  4441. else if (rfid.cardType == FELICA)
  4442. { }
  4443. }
  4444. sleep(1);
  4445. }
  4446. }
  4447. }
  4448. void CreateCheckSystemTaskFork()
  4449. {
  4450. pid_t taskPid;
  4451. taskPid = fork();
  4452. if (taskPid == 0)
  4453. {
  4454. bool stopToDet = false;
  4455. while(true)
  4456. {
  4457. if (!stopToDet)
  4458. {
  4459. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4460. {
  4461. if (chargingInfo[_index]->SystemStatus == SYS_MODE_UPDATE ||
  4462. chargingInfo[_index]->Type == 0x09)
  4463. {
  4464. stopToDet = true;
  4465. continue;
  4466. }
  4467. }
  4468. if (!stopToDet)
  4469. CheckSystemTaskAlive();
  4470. }
  4471. sleep(5);
  4472. }
  4473. }
  4474. }
  4475. void StartSystemTimeoutDet(unsigned char flag)
  4476. {
  4477. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  4478. {
  4479. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4480. }
  4481. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  4482. }
  4483. void StopSystemTimeoutDet()
  4484. {
  4485. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4486. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  4487. }
  4488. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  4489. {
  4490. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  4491. {
  4492. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  4493. {
  4494. GetTimespecFunc(&chargingInfo[gunIndex]->ConnectorTimeout);
  4495. }
  4496. chargingInfo[gunIndex]->TimeoutFlag = flag;
  4497. }
  4498. }
  4499. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  4500. {
  4501. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  4502. {
  4503. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  4504. }
  4505. }
  4506. void CheckConnectionTimeout()
  4507. {
  4508. if(system("pidof -s OcppBackend > /dev/null") != 0)
  4509. {
  4510. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4511. }
  4512. else
  4513. {
  4514. _connectionTimeout = _ocpp_get_connect_timeout();
  4515. if(_connectionTimeout <= 0)
  4516. {
  4517. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4518. }
  4519. }
  4520. }
  4521. void GetOfflinePowerValue()
  4522. {
  4523. int value = atoi((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[OffLineMaxChargingPower].ItemData);//Unit: W
  4524. if (ShmDcCommonData->_offlinePower != value)
  4525. {
  4526. ShmDcCommonData->_offlinePower = value;
  4527. PRINTF_FUNC("OCPP offline .. Read OfflinePower = %d \n", ShmDcCommonData->_offlinePower);
  4528. }
  4529. }
  4530. void CreateTimeoutFork()
  4531. {
  4532. pid_t timeoutPid;
  4533. timeoutPid = fork();
  4534. if (timeoutPid > 0)
  4535. {
  4536. GetTimespecFunc(&_cmdSubPriority_time);
  4537. CheckConnectionTimeout();
  4538. int _timebuf = 0;
  4539. while(true)
  4540. {
  4541. _timebuf = GetTimeoutValue(&_cmdSubPriority_time);
  4542. if (_timebuf < 0)
  4543. {
  4544. GetTimespecFunc(&_cmdSubPriority_time);
  4545. }
  4546. else
  4547. {
  4548. if (_timebuf >= 5)
  4549. {
  4550. CheckConnectionTimeout();
  4551. //LW_OfflinePower
  4552. GetOfflinePowerValue();
  4553. GetTimespecFunc(&_cmdSubPriority_time);
  4554. }
  4555. }
  4556. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  4557. // 系統
  4558. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_None)
  4559. {
  4560. _timebuf = GetTimeoutValue(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4561. if (_timebuf < 0)
  4562. {
  4563. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4564. }
  4565. else
  4566. {
  4567. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  4568. {
  4569. case Timeout_SelftestChk:
  4570. if (_timebuf >= SELFTEST_TIMEOUT)
  4571. {
  4572. _SelfTestTimeout();
  4573. StopSystemTimeoutDet();
  4574. }
  4575. break;
  4576. case Timeout_Authorizing:
  4577. if (_timebuf >= AUTHORIZE_TIMEOUT)
  4578. {
  4579. _AuthorizedTimeout();
  4580. StopSystemTimeoutDet();
  4581. }
  4582. break;
  4583. case Timeout_VerifyFail:
  4584. if (_timebuf >= AUTHORIZE_FAIL_TIMEOUT)
  4585. {
  4586. _AutoReturnTimeout();
  4587. StopSystemTimeoutDet();
  4588. }
  4589. break;
  4590. case Timeout_VerifyComp:
  4591. if (_timebuf >= AUTHORIZE_COMP_TIMEOUT)
  4592. {
  4593. _AutoReturnTimeout();
  4594. StopSystemTimeoutDet();
  4595. }
  4596. break;
  4597. case Timeout_WaitPlug:
  4598. if (_timebuf >= _connectionTimeout)
  4599. {
  4600. _DetectPlugInTimeout();
  4601. StopSystemTimeoutDet();
  4602. }
  4603. break;
  4604. case Timeout_ReturnToChargingGunDet:
  4605. {
  4606. if (_timebuf >= RETURN_TO_CHARGING_PAGE)
  4607. {
  4608. DisplayChargingInfo();
  4609. StopSystemTimeoutDet();
  4610. }
  4611. }
  4612. break;
  4613. // case Timeout_AuthorizingForStop:
  4614. // {
  4615. // if (_timebuf / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT)
  4616. // {
  4617. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  4618. // ClearAuthorizedFlag();
  4619. // StopSystemTimeoutDet();
  4620. // }
  4621. // }
  4622. // break;
  4623. }
  4624. }
  4625. }
  4626. // 各槍
  4627. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4628. {
  4629. if (chargingInfo[gun_index]->TimeoutFlag != Timeout_None)
  4630. {
  4631. _timebuf = GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout);
  4632. if (_timebuf < 0)
  4633. {
  4634. GetTimespecFunc(&chargingInfo[gun_index]->ConnectorTimeout);
  4635. }
  4636. else
  4637. {
  4638. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  4639. switch(chargingInfo[gun_index]->TimeoutFlag)
  4640. {
  4641. case Timeout_Preparing:
  4642. {
  4643. if (_timebuf >= GUN_PREPARE_TIMEOUT)
  4644. {
  4645. _PrepareTimeout(gun_index);
  4646. StopGunInfoTimeoutDet(gun_index);
  4647. }
  4648. }
  4649. break;
  4650. case Timeout_EvChargingDet:
  4651. {
  4652. if (_timebuf >= GUN_EV_WAIT_TIMEOUT)
  4653. {
  4654. _DetectEvChargingEnableTimeout(gun_index);
  4655. StopGunInfoTimeoutDet(gun_index);
  4656. }
  4657. }
  4658. break;
  4659. case Timeout_EvseChargingDet:
  4660. {
  4661. if (chargingInfo[gun_index]->Type == _Type_GB)
  4662. {
  4663. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT * 3)
  4664. {
  4665. _DetectEvseChargingEnableTimeout(gun_index);
  4666. StopGunInfoTimeoutDet(gun_index);
  4667. }
  4668. }
  4669. else
  4670. {
  4671. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT)
  4672. {
  4673. _DetectEvseChargingEnableTimeout(gun_index);
  4674. StopGunInfoTimeoutDet(gun_index);
  4675. }
  4676. }
  4677. }
  4678. break;
  4679. case Timeout_EvseCompleteDet:
  4680. {
  4681. if (_timebuf >= GUN_COMP_WAIT_TIMEOUT)
  4682. {
  4683. StopGunInfoTimeoutDet(gun_index);
  4684. }
  4685. }
  4686. break;
  4687. case Timeout_ForCcsPrechargeDet:
  4688. {
  4689. if (_timebuf >= GUN_PRECHARGING_TIMEOUT)
  4690. {
  4691. _CcsPrechargeTimeout(gun_index);
  4692. StopGunInfoTimeoutDet(gun_index);
  4693. }
  4694. }
  4695. break;
  4696. }
  4697. }
  4698. }
  4699. }
  4700. sleep(1);
  4701. }
  4702. }
  4703. }
  4704. void GetSystemTime()
  4705. {
  4706. struct timeb csuTime;
  4707. struct tm *tmCSU;
  4708. ftime(&csuTime);
  4709. tmCSU = localtime(&csuTime.time);
  4710. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  4711. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4712. tmCSU->tm_sec);
  4713. // byte date[14];
  4714. //
  4715. //
  4716. // //sprintf(&date, "%d", );
  4717. //
  4718. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  4719. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  4720. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  4721. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  4722. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  4723. //
  4724. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  4725. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  4726. //
  4727. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  4728. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  4729. //
  4730. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  4731. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  4732. //
  4733. // date[10] = (tmCSU->tm_min) / 10 % 10;
  4734. // date[11] = (tmCSU->tm_min) / 1 % 10;
  4735. //
  4736. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  4737. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  4738. // PRINTF_FUNC("%x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x \n", date[0], date[1], date[2], date[3],
  4739. // date[4], date[5], date[6], date[7],
  4740. // date[8], date[9], date[10], date[11],
  4741. // date[12], date[13]);
  4742. }
  4743. void CheckFactoryConfigFunction()
  4744. {
  4745. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4746. {
  4747. char Buf[256];
  4748. sleep(5);
  4749. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  4750. ShmSysConfigAndInfo->SysConfig.ModelName,
  4751. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  4752. system(Buf);
  4753. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4754. system("sync");
  4755. sleep(5);
  4756. system("reboot -f");
  4757. sleep(5);
  4758. system("reboot -f");
  4759. }
  4760. }
  4761. void CheckFwUpdateFunction()
  4762. {
  4763. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  4764. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  4765. {
  4766. DEBUG_INFO_MSG("ftp : update start. \n");
  4767. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4768. {
  4769. SetChargerMode(gun_index, SYS_MODE_UPDATE);
  4770. }
  4771. CloseWatchDog();
  4772. sleep(2);
  4773. KillTask();
  4774. byte updateResult = CheckUpdateProcess();
  4775. if (updateResult == PASS)
  4776. DEBUG_INFO_MSG("ftp : update complete. \n");
  4777. else if (updateResult == MODELNAME_FAIL)
  4778. {
  4779. DEBUG_INFO_MSG("ftp : model name is none match. \n");
  4780. KillAllTask();
  4781. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4782. sleep(5);
  4783. system("/usr/bin/run_evse_restart.sh");
  4784. return;
  4785. }
  4786. else
  4787. DEBUG_INFO_MSG("ftp : update fail. \n");
  4788. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4789. sleep(5);
  4790. system("reboot -f");
  4791. }
  4792. else
  4793. {
  4794. ocpp_chk_update_cmd();
  4795. }
  4796. }
  4797. int TariffParsing(char *StringItem, char *TariffCode)
  4798. {
  4799. char *ptrSave, *ptrToken;
  4800. char strSource[128];
  4801. int fee = 0;
  4802. memcpy(strSource, StringItem, 128);
  4803. ptrToken = strtok_r(strSource, ";", &ptrSave);
  4804. while(ptrToken != NULL)
  4805. {
  4806. char *strTariff = strstr(ptrToken, TariffCode);
  4807. if(strTariff != NULL)
  4808. {
  4809. char *strFee = strstr(strTariff, "$");
  4810. if(strFee != NULL)
  4811. {
  4812. strFee++;
  4813. float fFee = atof(strFee);
  4814. fee = (int)(fFee * 100);
  4815. break;
  4816. }
  4817. }
  4818. ptrToken = strtok_r(NULL, ";", &ptrSave);
  4819. }
  4820. return fee;
  4821. }
  4822. void DefaultPriceHandlerOcpp16(char *StrDefaultPrice)
  4823. {
  4824. if (strstr(StrDefaultPrice, "Connection Fee") != NULL)
  4825. {
  4826. ShmDcCommonData->_useVersion = _CDFA_VERSION_NOODOE;
  4827. // 拓連後台
  4828. unsigned int connectionFee = 0, currentRate = 0, occupancyFee = 0;
  4829. connectionFee = TariffParsing(StrDefaultPrice, "Connection Fee");
  4830. currentRate = TariffParsing(StrDefaultPrice, "Current Rate");
  4831. occupancyFee = TariffParsing(StrDefaultPrice, "Occupancy Fee");
  4832. PRINTF_FUNC("*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100));
  4833. PRINTF_FUNC("*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100));
  4834. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100));
  4835. ShmDcCommonData->balanceInfo.defaultPrice_Noodoe = currentRate;
  4836. }
  4837. else
  4838. {
  4839. // CTEP
  4840. ShmDcCommonData->_useVersion = _CDFA_VERSION_10;
  4841. PRINTF_FUNC("*********DefaultPriceHandlerOcpp16**************** - %s \n", StrDefaultPrice);
  4842. sprintf(ShmDcCommonData->balanceInfo.defaultPrice, "%s", StrDefaultPrice);
  4843. }
  4844. }
  4845. void UserPriceHandlerOcpp16(char *UserId, char *UserPrice)
  4846. {
  4847. unsigned int connectionFee = 0, currentRate = 0, occupancyFee = 0;
  4848. int accountBalance = 0;
  4849. byte acGunIndex = 0;
  4850. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  4851. {
  4852. if((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4853. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) &&
  4854. strcmp((char *)chargingInfo[gunIndex]->StartUserId, UserId) == EQUAL)
  4855. {
  4856. if (strstr(UserPrice, "Connection Fee") != NULL)
  4857. {
  4858. // 拓連後台
  4859. connectionFee = TariffParsing(UserPrice, "Connection Fee");
  4860. currentRate = TariffParsing(UserPrice, "Current Rate");
  4861. occupancyFee = TariffParsing(UserPrice, "Occupancy Fee");
  4862. accountBalance = TariffParsing(UserPrice, "Account Balance");
  4863. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex].UserPrice != currentRate ||
  4864. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex].AccountBalance != accountBalance)
  4865. {
  4866. PRINTF_FUNC("*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100));
  4867. PRINTF_FUNC("*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100));
  4868. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100));
  4869. PRINTF_FUNC("*** Account Balance: %d.%02d ***", (accountBalance / 100), (accountBalance % 100));
  4870. PRINTF_FUNC("*** Connector Id %d User Price %d.%02d, Account Balance %d.%02d ***",
  4871. (gunIndex), (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100));
  4872. }
  4873. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex].UserPrice = currentRate;
  4874. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex].AccountBalance = accountBalance;
  4875. }
  4876. else
  4877. {
  4878. // CTEP
  4879. //sprintf(ShmDcCommonData->balanceInfo.defaultPrice, "%s", UserPrice);
  4880. }
  4881. }
  4882. }
  4883. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex++)
  4884. {
  4885. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  4886. acGunIndex = 1;
  4887. else
  4888. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4889. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4890. if(strcmp((char *)ac_chargingInfo[gunIndex]->StartUserId, UserId) == EQUAL)
  4891. {
  4892. if (strstr(UserPrice, "Connection Fee") != NULL)
  4893. {
  4894. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].UserPrice != currentRate ||
  4895. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance != accountBalance)
  4896. {
  4897. PRINTF_FUNC("*** AC Connector User Price %d.%02d, Account Balance %d.%02d ***",
  4898. (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100));
  4899. }
  4900. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].UserPrice = currentRate;
  4901. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance = accountBalance;
  4902. }
  4903. }
  4904. }
  4905. }
  4906. void ExtractNoodoeInfor(byte gunIndex, byte connectionFee, byte sessionFee, byte occupancyFee, byte totalCost, int accountBalance)
  4907. {
  4908. PRINTF_FUNC("*** Connection Fee: %d.%02d *** \n", (connectionFee / 100), (connectionFee % 100));
  4909. PRINTF_FUNC("*** Session Fee: %d.%02d *** \n", (sessionFee / 100), (sessionFee % 100));
  4910. PRINTF_FUNC("*** Occupancy Fee: %d.%02d *** \n", (occupancyFee / 100), (occupancyFee % 100));
  4911. PRINTF_FUNC("*** Total Cost: %d.%02d *** \n", (totalCost / 100), (totalCost % 100));
  4912. PRINTF_FUNC("*** Account Balance: %d.%02d *** \n", (accountBalance / 100), (accountBalance % 100));
  4913. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex].TotalCost != totalCost ||
  4914. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex].AccountBalance != accountBalance)
  4915. {
  4916. PRINTF_FUNC("*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d *** \n",
  4917. (gunIndex), (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100));
  4918. }
  4919. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex].TotalCost = totalCost;
  4920. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex].AccountBalance = accountBalance;
  4921. }
  4922. void ExtractCtep10Infor(byte gunIndex, json_object **jsonDescription)
  4923. {
  4924. if (json_object_object_get ( *jsonDescription, "chargingFee" ) != NULL)
  4925. {
  4926. ShmDcCommonData->cdfaInfor[gunIndex]._ctepEnergyCost = json_object_get_double
  4927. (json_object_object_get ( *jsonDescription, "chargingFee" ) );
  4928. }
  4929. if (json_object_object_get ( *jsonDescription, "parkingFee" ) != NULL)
  4930. {
  4931. ShmDcCommonData->cdfaInfor[gunIndex]._ctepParkingFee = json_object_get_double
  4932. (json_object_object_get ( *jsonDescription, "parkingFee" ) );
  4933. }
  4934. if ((ShmDcCommonData->cdfaInfor[gunIndex]._ctepEnergyCost + ShmDcCommonData->cdfaInfor[gunIndex]._ctepParkingFee) > 0)
  4935. ShmDcCommonData->cdfaInfor[gunIndex]._ctepTotalCost = ShmDcCommonData->cdfaInfor[gunIndex]._ctepEnergyCost + ShmDcCommonData->cdfaInfor[gunIndex]._ctepParkingFee;
  4936. else
  4937. ShmDcCommonData->cdfaInfor[gunIndex]._ctepTotalCost = 0;
  4938. PRINTF_FUNC("Energy Cost = %f \n", ShmDcCommonData->cdfaInfor[gunIndex]._ctepEnergyCost);
  4939. PRINTF_FUNC("Parking Fee = %f \n", ShmDcCommonData->cdfaInfor[gunIndex]._ctepParkingFee);
  4940. PRINTF_FUNC("Total Cost = %f \n", ShmDcCommonData->cdfaInfor[gunIndex]._ctepTotalCost);
  4941. }
  4942. void ExtractCtep20Infor(byte gunIndex)
  4943. {
  4944. // 充電能量價格處理 : _ctepEnergyCost + 這段時間充電能量 * _prekWhPrice
  4945. ShmDcCommonData->cdfaInfor[gunIndex]._ctepEnergyCost = ShmDcCommonData->cdfaInfor[gunIndex]._preEnergyCost +
  4946. ((chargingInfo[gunIndex]->PresentChargedEnergy - ShmDcCommonData->cdfaInfor[gunIndex]._preOutputEnergy) * ShmDcCommonData->cdfaInfor[gunIndex]._prekWhPrice);
  4947. // 占用停車價格處理 : _ctepParkingFee + 經過時間 * _prehourPrice
  4948. // 先算出當前時間與 _preTimestamp 的經過時間
  4949. float elapsedTime = (float)ElapsedTime(ShmDcCommonData->cdfaInfor[gunIndex]._preTimestamp) / 3600;
  4950. ShmDcCommonData->cdfaInfor[gunIndex]._ctepParkingFee = ShmDcCommonData->cdfaInfor[gunIndex]._preParkingFee +
  4951. (elapsedTime * ShmDcCommonData->cdfaInfor[gunIndex]._prehourPrice);
  4952. // 總金額處理 : 充電能量價格處理 + 占用停車價格處理 + flatFee
  4953. ShmDcCommonData->cdfaInfor[gunIndex]._ctepTotalCost =
  4954. ShmDcCommonData->cdfaInfor[gunIndex]._ctepEnergyCost +
  4955. ShmDcCommonData->cdfaInfor[gunIndex]._ctepParkingFee +
  4956. ShmOCPP16Data->Cost.RunningCost[gunIndex].chargingPrice.flatFee;
  4957. }
  4958. void RunningFinalCostHandlerOcpp16()
  4959. {
  4960. unsigned int connectionFee = 0, sessionFee = 0, occupancyFee = 0, totalCost = 0;
  4961. int accountBalance = 0;
  4962. byte acGunIndex = 0;
  4963. json_object *jsonDescription;
  4964. bool isFind;
  4965. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  4966. {
  4967. isFind = false;
  4968. if (gunIndex == 1)
  4969. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4970. else
  4971. acGunIndex = 0;
  4972. if (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE &&
  4973. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4974. {
  4975. if(ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].txId != 0 &&
  4976. ShmOCPP16Data->StartTransaction[gunIndex + acGunIndex].ResponseTransactionId == ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].txId)
  4977. {
  4978. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description) > 0)
  4979. {
  4980. if (strstr((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Connection Fee") != NULL)
  4981. {
  4982. ShmDcCommonData->_useVersion = _CDFA_VERSION_NOODOE;
  4983. connectionFee = TariffParsing ((char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description, "Connection Fee" );
  4984. sessionFee = TariffParsing ((char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description, "Session Fee" );
  4985. occupancyFee = TariffParsing ((char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description, "Occupancy Fee" );
  4986. totalCost = TariffParsing ((char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description, "Total Cost" );
  4987. accountBalance = TariffParsing ((char *) ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex].description, "Account Balance" );
  4988. PRINTF_FUNC ( "*** Connector Id: %d Running Cost *** \n", gunIndex );
  4989. memset (&ShmOCPP16Data->Cost.RunningCost [gunIndex + acGunIndex], 0x00, sizeof(struct StrcutRunningCost) );
  4990. isFind = true;
  4991. }
  4992. else
  4993. {
  4994. //========================
  4995. // CDFA 1.0 Structure
  4996. //========================
  4997. ShmDcCommonData->_useVersion = _CDFA_VERSION_10;
  4998. PRINTF_FUNC("*** (CDFA 1.0) Connector Id: %d Running Cost *** \n", gunIndex + acGunIndex);
  4999. jsonDescription = json_tokener_parse(
  5000. (char*)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description);
  5001. isFind = true;
  5002. }
  5003. }
  5004. else
  5005. {
  5006. //========================
  5007. // CDFA 2.0 Structure
  5008. //========================
  5009. ShmDcCommonData->_useVersion = _CDFA_VERSION_20;
  5010. if (strcmp((char*)ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preTimestamp,
  5011. (char*)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].timestamp) != EQUAL &&
  5012. strcmp((char*)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].timestamp, "") != EQUAL)
  5013. {
  5014. // 如果是未來的時間~ 先不用
  5015. if ((float)ElapsedTime(ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].timestamp) > 0)
  5016. {
  5017. // 資料有更新
  5018. strcpy((char*)ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preTimestamp, (char*)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].timestamp);
  5019. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preOutputEnergy = chargingInfo[gunIndex]->PresentChargedEnergy;
  5020. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._prekWhPrice = ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].chargingPrice.kWhPrice;
  5021. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._prehourPrice = ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].chargingPrice.hourPrice;
  5022. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preEnergyCost = ShmDcCommonData->cdfaInfor[gunIndex]._ctepEnergyCost;
  5023. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preParkingFee = ShmDcCommonData->cdfaInfor[gunIndex]._ctepParkingFee;
  5024. PRINTF_FUNC("***************** (CDFA 2.0) RunningCost**************** \n");
  5025. PRINTF_FUNC("cost = %f \n", ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].cost);
  5026. PRINTF_FUNC("flatFee = %f \n", ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].chargingPrice.flatFee);
  5027. PRINTF_FUNC("_ctepEnergyCost = %f \n", ShmDcCommonData->cdfaInfor[gunIndex]._ctepEnergyCost);
  5028. PRINTF_FUNC("******************************************** \n");
  5029. if (ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].cost > ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].chargingPrice.flatFee)
  5030. {
  5031. if ((ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].cost - ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].chargingPrice.flatFee) >=
  5032. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._ctepEnergyCost)
  5033. {
  5034. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preParkingFee = ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].cost -
  5035. ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].chargingPrice.flatFee - ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._ctepEnergyCost;
  5036. }
  5037. }
  5038. }
  5039. isFind = true;
  5040. }
  5041. else if (strcmp((char*)ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preTimestamp, "") != EQUAL)
  5042. { isFind = true; }
  5043. }
  5044. }
  5045. if((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_TERMINATING && chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_ALARM) &&
  5046. ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].txId != 0 &&
  5047. ShmOCPP16Data->StartTransaction[gunIndex + acGunIndex].ResponseTransactionId == ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].txId)
  5048. {
  5049. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description) > 0)
  5050. {
  5051. if (strstr((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Connection Fee") != NULL)
  5052. {
  5053. ShmDcCommonData->_useVersion = _CDFA_VERSION_NOODOE;
  5054. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Connection Fee");
  5055. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Session Fee");
  5056. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Occupancy Fee");
  5057. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Total Cost");
  5058. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Account Balance");
  5059. PRINTF_FUNC("********** Connector Id: %d Final Cost ********** \n", gunIndex);
  5060. memset(&ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex], 0x00, sizeof(struct StrcutFinalCost));
  5061. isFind = true;
  5062. }
  5063. else
  5064. {
  5065. //========================
  5066. // CDFA 1.0 Structure
  5067. //========================
  5068. ShmDcCommonData->_useVersion = _CDFA_VERSION_10;
  5069. PRINTF_FUNC("********** (CDFA 1.0) Connector Id: %d Final Cost ********** \n", gunIndex);
  5070. jsonDescription = json_tokener_parse(
  5071. (char*)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description);
  5072. isFind = true;
  5073. }
  5074. }
  5075. else
  5076. {
  5077. //========================
  5078. // CDFA 2.0 Structure
  5079. //========================
  5080. if (strcmp((char*)ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preQrCodeText, "") == EQUAL &&
  5081. strcmp((char*)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].priceText, "") != EQUAL &&
  5082. strcmp((char*)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].qrCodeText, "") != EQUAL)
  5083. {
  5084. PRINTF_FUNC("***************** (CDFA 2.0) FinalCost**************** \n");
  5085. ShmDcCommonData->_useVersion = _CDFA_VERSION_20;
  5086. strcpy((char*)ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preQrCodeText,
  5087. (char*)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].qrCodeText);
  5088. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._ctepTotalCost = ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].cost;
  5089. if ((ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].cost - ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._ctepEnergyCost) >= 0)
  5090. {
  5091. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preParkingFee = ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].cost - ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._ctepEnergyCost;
  5092. }
  5093. else
  5094. {
  5095. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._ctepEnergyCost = ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._ctepTotalCost;
  5096. ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preParkingFee = 0;
  5097. }
  5098. }
  5099. }
  5100. }
  5101. if (isFind)
  5102. {
  5103. if (ShmDcCommonData->_useVersion == _CDFA_VERSION_NOODOE)
  5104. {
  5105. ExtractNoodoeInfor(gunIndex + acGunIndex, connectionFee, sessionFee, occupancyFee, totalCost, accountBalance);
  5106. }
  5107. else if (ShmDcCommonData->_useVersion == _CDFA_VERSION_10)
  5108. {
  5109. ExtractCtep10Infor(gunIndex + acGunIndex, &jsonDescription);
  5110. json_object_put (jsonDescription);
  5111. }
  5112. else if (ShmDcCommonData->_useVersion == _CDFA_VERSION_20)
  5113. {
  5114. if (strcmp((char*)ShmDcCommonData->cdfaInfor[gunIndex + acGunIndex]._preTimestamp, "") != EQUAL)
  5115. ExtractCtep20Infor(gunIndex + acGunIndex);
  5116. }
  5117. }
  5118. }
  5119. }
  5120. }
  5121. //===============================================
  5122. // ocpp key
  5123. //===============================================
  5124. bool ocpp_authorizeRemoteTxReqChk_key()
  5125. {
  5126. bool result = false;
  5127. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5128. {
  5129. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
  5130. result = true;
  5131. }
  5132. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5133. {
  5134. }
  5135. return result;
  5136. }
  5137. //===============================================
  5138. // Check reservation date is expired
  5139. //===============================================
  5140. int opcc_chk_reserve_expired(byte gun_index)
  5141. {
  5142. int result = NO;
  5143. byte acDirIndex = 0;
  5144. if (gun_index == 1)
  5145. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5146. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5147. {
  5148. if(chargingInfo[gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  5149. PRINTF_FUNC("%s \n", ShmOCPP16Data->ReserveNow[gun_index + acDirIndex].ExpiryDate);
  5150. if (CheckTimeOut(ShmOCPP16Data->ReserveNow[gun_index + acDirIndex].ExpiryDate))
  5151. result = YES;
  5152. }
  5153. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5154. {
  5155. if(chargingInfo[gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  5156. PRINTF_FUNC("%s \n", ShmOCPP20Data->ReserveNow[gun_index + acDirIndex].expiryDateTime);
  5157. if (CheckTimeOut(ShmOCPP20Data->ReserveNow[gun_index + acDirIndex].expiryDateTime))
  5158. result = YES;
  5159. }
  5160. return result;
  5161. }
  5162. //===============================================
  5163. // AC OCPP routine
  5164. //===============================================
  5165. void ocpp_ac_chk_availability_cmd()
  5166. {
  5167. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5168. byte type = 0;
  5169. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  5170. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5171. {
  5172. if (ShmOCPP16Data->CsMsg.bits[hasDc].ChangeAvailabilityReq == YES)
  5173. {
  5174. PRINTF_FUNC("***************ChkOcppStatus : AC OcppChangeAvailability******************** \n");
  5175. DEBUG_ERROR_MSG("***************ChkOcppStatus : AC OcppChangeAvailability******************** \n");
  5176. ShmOCPP16Data->CsMsg.bits[hasDc].ChangeAvailabilityReq = NO;
  5177. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[hasDc].Type, "Operative") == EQUAL)
  5178. {
  5179. if (isDb_ready)
  5180. DB_Update_Operactive(localDb, hasDc, true);
  5181. type = 1;
  5182. }
  5183. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[hasDc].Type, "Inoperative") == EQUAL)
  5184. {
  5185. if (isDb_ready)
  5186. DB_Update_Operactive(localDb, hasDc, false);
  5187. type = 2;
  5188. }
  5189. }
  5190. }
  5191. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5192. {
  5193. if (ShmOCPP20Data->CsMsg.bits[hasDc].ChangeAvailabilityReq == YES)
  5194. {
  5195. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5196. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5197. ShmOCPP20Data->CsMsg.bits[hasDc].ChangeAvailabilityReq = NO;
  5198. if(strcmp((char *)ShmOCPP20Data->ChangeAvailability[hasDc].operationalStatus, "Operative") == EQUAL)
  5199. {
  5200. if (isDb_ready)
  5201. DB_Update_Operactive(localDb, hasDc, true);
  5202. type = 1;
  5203. }
  5204. else if (strcmp((char *)ShmOCPP20Data->ChangeAvailability[hasDc].operationalStatus, "Inoperative") == EQUAL)
  5205. {
  5206. if (isDb_ready)
  5207. DB_Update_Operactive(localDb, hasDc, false);
  5208. type = 2;
  5209. }
  5210. }
  5211. }
  5212. if (type == 1)
  5213. {
  5214. ac_chargingInfo[0]->IsAvailable = YES;
  5215. }
  5216. else if (type == 2)
  5217. {
  5218. ac_chargingInfo[0]->IsAvailable = NO;
  5219. }
  5220. }
  5221. void ocpp_ac_chk_unlock_cmd()
  5222. {
  5223. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5224. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  5225. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5226. {
  5227. if (ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorReq == YES)
  5228. {
  5229. ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorReq = NO;
  5230. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5231. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  5232. {
  5233. // Unlocked - 充電中,需停止充電
  5234. strcpy((char *)ShmOCPP16Data->StopTransaction[hasDc].StopReason, "UnlockCommand");
  5235. ac_chargingInfo[0]->StopChargeFlag = YES;
  5236. }
  5237. strcpy((char *)ShmOCPP16Data->UnlockConnector[hasDc].ResponseStatus, "Unlocked");
  5238. ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorConf = YES;
  5239. }
  5240. }
  5241. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5242. {
  5243. if (ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorReq == YES)
  5244. {
  5245. ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorReq = NO;
  5246. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5247. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  5248. {
  5249. // Unlocked - 充電中,需停止充電
  5250. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].transactionInfo.stoppedReason, "UnlockCommand");
  5251. ac_chargingInfo[0]->StopChargeFlag = YES;
  5252. }
  5253. strcpy((char*)ShmOCPP20Data->UnlockConnector[hasDc].Response_status, "Unlocked");
  5254. ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorConf = YES;
  5255. }
  5256. }
  5257. }
  5258. void ocpp_ac_chk_profileConf_cmd()
  5259. {
  5260. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5261. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5262. {
  5263. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf == YES)
  5264. {
  5265. ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf = NO;
  5266. }
  5267. }
  5268. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5269. {
  5270. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf == YES)
  5271. {
  5272. ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf = NO;
  5273. }
  5274. }
  5275. }
  5276. void ocpp_ac_chargingProfile_process()
  5277. {
  5278. int _time = 0;
  5279. int _startCount = NO_DEFINE;
  5280. int _maxCount = 0;
  5281. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5282. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5283. {
  5284. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingProfileKind, "Absolute") == EQUAL &&
  5285. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingProfileId == YES)
  5286. {
  5287. _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.StartSchedule);
  5288. if (_time > -1)
  5289. {
  5290. _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod);
  5291. _startCount = NO_DEFINE;
  5292. for (byte _count = 0; _count < _maxCount; _count++)
  5293. {
  5294. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  5295. if (_time >= ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  5296. {
  5297. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  5298. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  5299. {
  5300. _startCount = _count;
  5301. }
  5302. }
  5303. }
  5304. if (_startCount < _maxCount)
  5305. {
  5306. //PRINTF_FUNC("AC - Gun_%d, Get Profile - Profile Index = %d \n", hasDc, _startCount);
  5307. //PRINTF_FUNC("Profile Limit = %.2f \n", ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  5308. if (ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit >= 1 &&
  5309. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit <= 5)
  5310. {
  5311. ac_chargingInfo[0]->ChargingProfileCurrent = AC_MINIMUM_DUTY;
  5312. }
  5313. else if (ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit == 0)
  5314. {
  5315. ac_chargingInfo[0]->ChargingProfileCurrent = AC_STOP_DUTY;
  5316. }
  5317. else
  5318. {
  5319. ac_chargingInfo[0]->ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit;
  5320. }
  5321. }
  5322. else
  5323. {
  5324. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5325. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5326. }
  5327. }
  5328. else
  5329. {
  5330. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5331. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5332. }
  5333. }
  5334. else
  5335. {
  5336. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5337. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5338. }
  5339. }
  5340. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5341. {
  5342. if (strcmp((char *)ShmOCPP20Data->SmartChargingProfile[hasDc].chargingProfileKind, "Absolute") == EQUAL &&
  5343. ShmOCPP20Data->SmartChargingProfile[hasDc].id == YES)
  5344. {
  5345. _time = GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].startSchedule);
  5346. _maxCount = ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod);
  5347. _startCount = NO_DEFINE;
  5348. for (byte _count = 0; _count < _maxCount; _count++)
  5349. {
  5350. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  5351. if (_time >= ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].startPeriod)
  5352. {
  5353. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].limit >= MIN_OUTPUT_CUR) ||
  5354. ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].limit > MIN_OUTPUT_CUR)
  5355. {
  5356. _startCount = _count;
  5357. }
  5358. }
  5359. }
  5360. if (_startCount < _maxCount)
  5361. {
  5362. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", 1, _startCount);
  5363. //PRINTF_FUNC("Profile Limit = %.2f \n", ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit);
  5364. if (ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit >= 1 &&
  5365. ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit <= 5)
  5366. {
  5367. ac_chargingInfo [0]->ChargingProfileCurrent = AC_MINIMUM_DUTY;
  5368. }
  5369. else if (ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit == 0)
  5370. {
  5371. ac_chargingInfo [0]->ChargingProfileCurrent = AC_STOP_DUTY;
  5372. }
  5373. else
  5374. {
  5375. ac_chargingInfo [0]->ChargingProfileCurrent = ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit;
  5376. }
  5377. }
  5378. else
  5379. {
  5380. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5381. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5382. }
  5383. }
  5384. else
  5385. {
  5386. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5387. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5388. }
  5389. }
  5390. else
  5391. {
  5392. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5393. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5394. }
  5395. if (ac_chargingInfo[0]->ChargingProfileCurrent >= 0 &&
  5396. _AcChargingProfileCurBuffer != ac_chargingInfo[0]->ChargingProfileCurrent)
  5397. {
  5398. _AcChargingProfileCurBuffer = ac_chargingInfo[0]->ChargingProfileCurrent;
  5399. PRINTF_FUNC ( "AC Profile (duty) : ChargingProfileCurrent = %.2f \n", ac_chargingInfo[0]->ChargingProfileCurrent);
  5400. }
  5401. }
  5402. void ocpp_ac_lift_profileReq_cmd(byte gunIndex)
  5403. {
  5404. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5405. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5406. {
  5407. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf == NO)
  5408. {
  5409. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileReq == NO)
  5410. ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileReq = YES;
  5411. }
  5412. }
  5413. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5414. {
  5415. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf == NO)
  5416. {
  5417. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileReq == NO)
  5418. ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileReq = YES;
  5419. }
  5420. }
  5421. }
  5422. int opcc_ac_chk_reserve_expired()
  5423. {
  5424. int result = NO;
  5425. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5426. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5427. {
  5428. if(ac_chargingInfo [0]->SystemStatus != SYS_MODE_RESERVATION)
  5429. PRINTF_FUNC("%s \n", ShmOCPP16Data->ReserveNow[hasDc].ExpiryDate);
  5430. if (CheckTimeOut(ShmOCPP16Data->ReserveNow[hasDc].ExpiryDate))
  5431. result = YES;
  5432. }
  5433. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5434. {
  5435. if(ac_chargingInfo [0]->SystemStatus != SYS_MODE_RESERVATION)
  5436. PRINTF_FUNC("%s \n", ShmOCPP20Data->ReserveNow[hasDc].expiryDateTime);
  5437. if (CheckTimeOut(ShmOCPP20Data->ReserveNow[hasDc].expiryDateTime))
  5438. result = YES;
  5439. }
  5440. return result;
  5441. }
  5442. void ocpp_ac_chk_reserved_cmd()
  5443. {
  5444. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5445. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5446. {
  5447. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE &&
  5448. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowReq == YES)
  5449. {
  5450. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5451. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowReq = NO;
  5452. if ( ! opcc_ac_chk_reserve_expired ())
  5453. {
  5454. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP16Data->ReserveNow [hasDc].ReservationId );
  5455. PRINTF_FUNC ( "IdTag = %s \n", ShmOCPP16Data->ReserveNow [hasDc].IdTag );
  5456. PRINTF_FUNC ( "*************ChkOcppStatus : (AC) OcppReservedStatus End****************** \n" );
  5457. ac_chargingInfo [0]->ReservationId = ShmOCPP16Data->ReserveNow [hasDc].ReservationId;
  5458. ac_chargingInfo [0]->SystemStatus = SYS_MODE_RESERVATION;
  5459. strcpy ( (char *) ShmDcCommonData->_reserved_ac_UserId[0],
  5460. (char *) ShmOCPP16Data->ReserveNow [hasDc].IdTag );
  5461. }
  5462. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowConf = YES;
  5463. }
  5464. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_RESERVATION &&
  5465. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationReq == YES)
  5466. {
  5467. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationReq = NO;
  5468. if (ShmOCPP16Data->ReserveNow [hasDc].ReservationId == ac_chargingInfo [0]->ReservationId)
  5469. {
  5470. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5471. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP16Data->ReserveNow [hasDc].ReservationId );
  5472. PRINTF_FUNC ( "***********ChkOcppStatus : (AC) Cancel OcppReservedStatus End**************** \n" );
  5473. ac_chargingInfo [0]->ReservationId = NO_RESERVE;
  5474. ac_chargingInfo [0]->SystemStatus = SYS_MODE_IDLE;
  5475. strcpy ( (char *) ShmDcCommonData->_reserved_ac_UserId[0], "" );
  5476. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationConf = YES;
  5477. }
  5478. }
  5479. }
  5480. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5481. {
  5482. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE &&
  5483. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowReq == YES)
  5484. {
  5485. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5486. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowReq = NO;
  5487. if ( ! opcc_ac_chk_reserve_expired ())
  5488. {
  5489. PRINTF_FUNC ( "id = %d \n", ShmOCPP20Data->ReserveNow [hasDc].id );
  5490. PRINTF_FUNC ( "idToken = %s \n", ShmOCPP20Data->ReserveNow [hasDc].idToken.idToken );
  5491. PRINTF_FUNC ( "*************ChkOcppStatus : (AC) OcppReservedStatus End****************** \n" );
  5492. ac_chargingInfo [0]->ReservationId = ShmOCPP20Data->ReserveNow [hasDc].id;
  5493. ac_chargingInfo [0]->SystemStatus = SYS_MODE_RESERVATION;
  5494. strncpy ( (char *) ShmDcCommonData->_reserved_UserId,
  5495. (char *) ShmOCPP20Data->ReserveNow [hasDc].idToken.idToken, 30 );
  5496. }
  5497. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowConf = YES;
  5498. }
  5499. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_RESERVATION &&
  5500. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationReq == YES)
  5501. {
  5502. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationReq = NO;
  5503. if (ShmOCPP20Data->ReserveNow [hasDc].id == ac_chargingInfo [0]->ReservationId)
  5504. {
  5505. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5506. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP20Data->ReserveNow [hasDc].id );
  5507. PRINTF_FUNC ( "***********ChkOcppStatus : (AC) Cancel OcppReservedStatus End**************** \n" );
  5508. ac_chargingInfo [0]->ReservationId = NO_RESERVE;
  5509. ac_chargingInfo [0]->SystemStatus = SYS_MODE_IDLE;
  5510. strcpy ( (char *) ShmDcCommonData->_reserved_UserId, "" );
  5511. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationConf = YES;
  5512. }
  5513. }
  5514. }
  5515. }
  5516. //===============================================
  5517. // OCPP routine
  5518. //===============================================
  5519. void ocpp_process_start()
  5520. {
  5521. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  5522. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  5523. {
  5524. if(system("pidof -s OcppBackend > /dev/null") != 0 &&
  5525. system("pidof -s OcppBackend20 > /dev/null") != 0)
  5526. {
  5527. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE)
  5528. CloseWatchDog();
  5529. DEBUG_ERROR_MSG("OcppBackend not running, restart it.\r\n");
  5530. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5531. system("/root/OcppBackend &");
  5532. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5533. system("/root/OcppBackend20 &");
  5534. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE)
  5535. CreateWatchdog();
  5536. }
  5537. else
  5538. {
  5539. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5540. {
  5541. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  5542. {
  5543. DEBUG_ERROR_MSG("OcppBackend watch dog timeout task restart.\n");
  5544. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  5545. system("pkill OcppBackend");
  5546. }
  5547. }
  5548. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5549. {
  5550. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  5551. {
  5552. DEBUG_ERROR_MSG("OcppBackend20 watch dog timeout task restart.\n");
  5553. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  5554. system("pkill OcppBackend20");
  5555. }
  5556. }
  5557. }
  5558. }
  5559. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  5560. {
  5561. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  5562. {
  5563. DEBUG_ERROR_MSG("OcppBackendPH not running, restart it.\r\n");
  5564. system("/root/OcppBackendPH &");
  5565. }
  5566. }
  5567. }
  5568. void ocpp_auto_response_BootNotification()
  5569. {
  5570. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5571. {
  5572. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  5573. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  5574. }
  5575. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5576. {
  5577. if (ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES)
  5578. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = NO;
  5579. }
  5580. }
  5581. bool ocpp_is_resPass_StartTransactionConf(byte gun_index)
  5582. {
  5583. bool result = false;
  5584. byte acDirIndex = 0;
  5585. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5586. if (gun_index == 1)
  5587. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5588. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5589. {
  5590. result = ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf;
  5591. ShmDcCommonData->startTransactionID[gun_index] = ShmOCPP16Data->StartTransaction[gun_index + acDirIndex].ResponseTransactionId;
  5592. }
  5593. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5594. {
  5595. result = ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf;
  5596. }
  5597. return result;
  5598. }
  5599. void ocpp_auto_response_StartTransationConf(byte gun_index)
  5600. {
  5601. byte acDirIndex = 0;
  5602. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5603. if (gun_index == 1)
  5604. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5605. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5606. {
  5607. if (ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf)
  5608. ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf = NO;
  5609. }
  5610. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5611. {
  5612. if (ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf)
  5613. ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf = NO;
  5614. }
  5615. }
  5616. void ocpp_auto_response_StopTransationConf(byte gun_index)
  5617. {
  5618. byte acDirIndex = 0;
  5619. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5620. if (gun_index == 1)
  5621. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5622. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5623. {
  5624. if(ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf)
  5625. ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf = NO;
  5626. }
  5627. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5628. {
  5629. }
  5630. }
  5631. void ocpp_auto_response_StopTransationConfPH(byte gun_index)
  5632. {
  5633. byte acDirIndex = 0;
  5634. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5635. if (gun_index == 1)
  5636. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5637. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  5638. {
  5639. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5640. {
  5641. if(ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf)
  5642. ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf = NO;
  5643. }
  5644. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5645. {
  5646. }
  5647. }
  5648. }
  5649. void ocpp_copy_stopReason2maintainServer(byte gun_index)
  5650. {
  5651. byte acDirIndex = 0;
  5652. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5653. if (gun_index == 1)
  5654. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5655. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  5656. {
  5657. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index + acDirIndex].StopReason, ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason));
  5658. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index + acDirIndex].IdTag, ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].IdTag));
  5659. }
  5660. }
  5661. void ocpp_auto_response_StartTransationConfPH(byte gun_index)
  5662. {
  5663. byte acDirIndex = 0;
  5664. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5665. if (gun_index == 1)
  5666. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5667. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  5668. {
  5669. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5670. {
  5671. if (ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf)
  5672. ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf = NO;
  5673. }
  5674. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5675. {
  5676. }
  5677. }
  5678. }
  5679. void ocpp_auto_response_ReserveConf(byte gun_index)
  5680. {
  5681. byte acDirIndex = 0;
  5682. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5683. if (gun_index == 1)
  5684. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5685. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5686. ShmOCPP16Data->CsMsg.bits[gun_index + acDirIndex].ReserveNowConf = YES;
  5687. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5688. ShmOCPP20Data->CsMsg.bits[gun_index + acDirIndex].ReserveNowConf = YES;
  5689. }
  5690. bool ocpp_chk_authrization_cmd()
  5691. {
  5692. char buf2[16] = "";
  5693. memset(buf2, 0, ARRAY_SIZE(buf2));
  5694. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5695. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  5696. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5697. sprintf(buf2, "%s", ShmOCPP20Data->Authorize.Response_idTokenInfo.status);
  5698. // 因為無法得知實際的長度,所以只能用搜尋的方式
  5699. if(strcmp(buf2, "Accepted") == EQUAL)
  5700. {
  5701. return true;
  5702. }
  5703. return false;
  5704. }
  5705. bool opcc_sub_get_reset_req()
  5706. {
  5707. bool result = false;
  5708. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5709. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  5710. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5711. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  5712. return result;
  5713. }
  5714. void ocpp_sub_run_reset(bool canReset)
  5715. {
  5716. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5717. {
  5718. if(canReset && strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  5719. {
  5720. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  5721. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  5722. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  5723. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  5724. sleep(3);
  5725. system("reboot -f");
  5726. }
  5727. else if (canReset && strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  5728. {
  5729. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  5730. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  5731. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  5732. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  5733. sleep(3);
  5734. KillAllTask();
  5735. CloseWatchDog();
  5736. system("/usr/bin/run_evse_restart.sh");
  5737. }
  5738. }
  5739. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5740. {
  5741. if(canReset && strcmp((char *)ShmOCPP20Data->Reset.type, "Immediate") == EQUAL)
  5742. {
  5743. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  5744. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  5745. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  5746. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  5747. sleep(3);
  5748. system("reboot -f");
  5749. }
  5750. else if (canReset && strcmp((char *)ShmOCPP20Data->Reset.type, "OnIdle") == EQUAL)
  5751. {
  5752. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  5753. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  5754. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  5755. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  5756. sleep(3);
  5757. KillAllTask();
  5758. CloseWatchDog();
  5759. system("/usr/bin/run_evse_restart.sh");
  5760. }
  5761. }
  5762. }
  5763. void ocpp_chk_reset_cmd(byte isforce)
  5764. {
  5765. if (opcc_sub_get_reset_req())
  5766. {
  5767. bool canReset = true;
  5768. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  5769. {
  5770. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5771. {
  5772. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5773. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  5774. {
  5775. // Reset - AC 先停止充電
  5776. ac_chargingInfo[0]->StopChargeFlag = YES;
  5777. GetTimespecFunc(&_resetChkTime);
  5778. }
  5779. }
  5780. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5781. {
  5782. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_REASSIGN &&
  5783. chargingInfo[_index]->SystemStatus <= SYS_MODE_TERMINATING)
  5784. {
  5785. canReset = false;
  5786. // Reset 系統先停止充電
  5787. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5788. {
  5789. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  5790. ocpp_set_stopReason_by_cmd(_index, "HardReset");
  5791. else
  5792. ocpp_set_stopReason_by_cmd(_index, "SoftReset");
  5793. }
  5794. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5795. {
  5796. if(strcmp((char *)ShmOCPP20Data->Reset.type, "Immediate") == EQUAL)
  5797. ocpp_set_stopReason_by_cmd(_index, "HardReset");
  5798. else
  5799. ocpp_set_stopReason_by_cmd(_index, "SoftReset");
  5800. }
  5801. ChargingTerminalProcess(_index);
  5802. GetTimespecFunc(&_resetChkTime);
  5803. }
  5804. }
  5805. }
  5806. if (canReset)
  5807. {
  5808. if (GetTimeoutValue(&_resetChkTime) < 0)
  5809. GetTimespecFunc(&_resetChkTime);
  5810. if (GetTimeoutValue(&_resetChkTime) >= 5)
  5811. {
  5812. ShmDcCommonData->evBoardResetFlag = YES;
  5813. // Reset -> change status to maintain.
  5814. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5815. {
  5816. SetChargerMode(_index, SYS_MODE_MAINTAIN);
  5817. }
  5818. ChangeLcmByIndex(_LCM_FIX);
  5819. }
  5820. else
  5821. canReset = false;
  5822. }
  5823. else
  5824. GetTimespecFunc(&_resetChkTime);
  5825. if (isforce == YES)
  5826. canReset = true;
  5827. ocpp_sub_run_reset(canReset);
  5828. }
  5829. }
  5830. void ocpp_chk_reserved_cmd(byte gunIndex)
  5831. {
  5832. byte acDirIndex = 0;
  5833. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5834. if (gunIndex == 1)
  5835. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5836. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5837. {
  5838. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE &&
  5839. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq == YES)
  5840. {
  5841. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5842. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq = NO;
  5843. if (!opcc_chk_reserve_expired(gunIndex))
  5844. {
  5845. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId);
  5846. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].IdTag);
  5847. PRINTF_FUNC("*************ChkOcppStatus : OcppReservedStatus End****************** \n");
  5848. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  5849. //chargingInfo[gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  5850. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex],
  5851. (char *)ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].IdTag);
  5852. }
  5853. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowConf = YES;
  5854. }
  5855. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION &&
  5856. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq == YES)
  5857. {
  5858. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq = NO;
  5859. if (ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId == chargingInfo[gunIndex]->ReservationId)
  5860. {
  5861. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5862. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId);
  5863. PRINTF_FUNC("***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n");
  5864. chargingInfo[gunIndex]->ReservationId = NO_RESERVE;
  5865. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_IDLE;
  5866. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex], "");
  5867. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationConf = YES;
  5868. }
  5869. }
  5870. }
  5871. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5872. {
  5873. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE &&
  5874. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq == YES)
  5875. {
  5876. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5877. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq = NO;
  5878. if (!opcc_chk_reserve_expired(gunIndex))
  5879. {
  5880. PRINTF_FUNC("id = %d \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id);
  5881. PRINTF_FUNC("idToken = %s \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].idToken.idToken);
  5882. PRINTF_FUNC("*************ChkOcppStatus : OcppReservedStatus End****************** \n");
  5883. chargingInfo[gunIndex]->ReservationId = ShmOCPP20Data->ReserveNow[gunIndex].id;
  5884. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  5885. strncpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex],
  5886. (char *)ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].idToken.idToken, 30);
  5887. }
  5888. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowConf = YES;
  5889. }
  5890. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION &&
  5891. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq == YES)
  5892. {
  5893. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq = NO;
  5894. if (ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id == chargingInfo[gunIndex]->ReservationId)
  5895. {
  5896. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5897. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id);
  5898. PRINTF_FUNC("***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n");
  5899. chargingInfo[gunIndex]->ReservationId = NO_RESERVE;
  5900. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_IDLE;
  5901. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex], "");
  5902. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationConf = YES;
  5903. }
  5904. }
  5905. }
  5906. }
  5907. void ocpp_chk_availability_cmd(byte gunIndex)
  5908. {
  5909. byte type = 0; // 0 : none, 1 : operative, 2 inoperative
  5910. byte acDirIndex = 0;
  5911. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5912. if (gunIndex == 1)
  5913. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5914. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5915. {
  5916. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5917. {
  5918. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5919. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5920. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5921. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex + acDirIndex].Type, "Operative") == EQUAL)
  5922. {
  5923. if (isDb_ready)
  5924. DB_Update_Operactive(localDb, gunIndex + acDirIndex, true);
  5925. type = 1;
  5926. }
  5927. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex + acDirIndex].Type, "Inoperative") == EQUAL)
  5928. {
  5929. if (isDb_ready)
  5930. DB_Update_Operactive(localDb, gunIndex + acDirIndex, false);
  5931. type = 2;
  5932. }
  5933. }
  5934. }
  5935. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5936. {
  5937. if (ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5938. {
  5939. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5940. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5941. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5942. if(strcmp((char *)ShmOCPP20Data->ChangeAvailability[gunIndex + acDirIndex].operationalStatus, "Operative") == EQUAL)
  5943. {
  5944. if (isDb_ready)
  5945. DB_Update_Operactive(localDb, gunIndex + acDirIndex, true);
  5946. type = 1;
  5947. }
  5948. else if (strcmp((char *)ShmOCPP20Data->ChangeAvailability[gunIndex + acDirIndex].operationalStatus, "Inoperative") == EQUAL)
  5949. {
  5950. if (isDb_ready)
  5951. DB_Update_Operactive(localDb, gunIndex + acDirIndex, false);
  5952. type = 2;
  5953. }
  5954. }
  5955. }
  5956. if (type == 1)
  5957. {
  5958. chargingInfo[gunIndex]->IsAvailable = YES;
  5959. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5960. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION ||
  5961. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5962. {
  5963. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  5964. }
  5965. }
  5966. else if (type == 2)
  5967. {
  5968. chargingInfo[gunIndex]->IsAvailable = NO;
  5969. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5970. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION ||
  5971. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5972. {
  5973. SetChargerMode(gunIndex, SYS_MODE_MAINTAIN);
  5974. }
  5975. }
  5976. }
  5977. void ocpp_chk_unlock_cmd(byte gunIndex)
  5978. {
  5979. byte acDirIndex = 0;
  5980. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5981. if (gunIndex == 1)
  5982. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5983. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5984. {
  5985. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5986. {
  5987. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5988. if ((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5989. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) ||
  5990. (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  5991. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  5992. {
  5993. // Unlocked - 充電中,需停止充電
  5994. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "UnlockCommand");
  5995. ChargingTerminalProcess(gunIndex);
  5996. }
  5997. if (chargingInfo[gunIndex]->Type == _Type_Chademo ||
  5998. chargingInfo[gunIndex]->Type == _Type_GB)
  5999. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  6000. else
  6001. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  6002. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorConf = YES;
  6003. }
  6004. }
  6005. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6006. {
  6007. if (ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq == YES)
  6008. {
  6009. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq = NO;
  6010. if ((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  6011. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) ||
  6012. (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  6013. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  6014. {
  6015. // Unlocked - 充電中,需停止充電
  6016. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "UnlockCommand");
  6017. ChargingTerminalProcess(gunIndex);
  6018. }
  6019. if (chargingInfo[gunIndex]->Type == _Type_Chademo ||
  6020. chargingInfo[gunIndex]->Type == _Type_GB)
  6021. strcpy((char*)ShmOCPP20Data->UnlockConnector[gunIndex + acDirIndex].Response_status, "Unlocked");
  6022. else
  6023. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  6024. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorConf = YES;
  6025. }
  6026. }
  6027. }
  6028. bool ocpp_chk_remoteStop_cmd(byte gunIndex)
  6029. {
  6030. bool result = false;
  6031. byte acDirIndex = 0;
  6032. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6033. if (gunIndex == 1)
  6034. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6035. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6036. result = ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStopTransactionReq;
  6037. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6038. result = ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStopTransactionReq;
  6039. if (result)
  6040. {
  6041. DEBUG_INFO_MSG("Remote Stop by OCPP (%d) \n", gunIndex + acDirIndex);
  6042. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6043. {
  6044. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "Remote");
  6045. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStopTransactionReq = NO;
  6046. }
  6047. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6048. {
  6049. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "Remote");
  6050. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStopTransactionReq = NO;
  6051. }
  6052. }
  6053. return result;
  6054. }
  6055. bool ocpp_sub_get_remote_start_transaction_req(byte gun_index)
  6056. {
  6057. bool result = false;
  6058. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6059. {
  6060. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  6061. }
  6062. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6063. {
  6064. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
  6065. }
  6066. if (result && ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6067. {
  6068. memcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag,
  6069. (char *)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag,
  6070. ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  6071. }
  6072. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6073. {
  6074. memcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken,
  6075. (char *)ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken,
  6076. ARRAY_SIZE(ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken));
  6077. }
  6078. return result;
  6079. }
  6080. void ocpp_sub_set_remote_start_transaction_req(byte gunIndex, bool result)
  6081. {
  6082. byte acDirIndex = 0;
  6083. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6084. if (gunIndex == 1)
  6085. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6086. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6087. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStartTransactionReq = result;
  6088. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6089. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStartTransactionReq = result;
  6090. }
  6091. void ocpp_chk_remoteStart_cmd()
  6092. {
  6093. if(!isDetectPlugin())
  6094. {
  6095. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  6096. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6097. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++)
  6098. {
  6099. if (ocpp_sub_get_remote_start_transaction_req(acDirIndex))
  6100. {
  6101. if (ac_chargingInfo[ac_index]->SystemStatus == SYS_MODE_IDLE ||
  6102. ac_chargingInfo[ac_index]->SystemStatus == SYS_MODE_RESERVATION)
  6103. {
  6104. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  6105. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  6106. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  6107. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  6108. //ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  6109. DetectPluginStart();
  6110. return;
  6111. }
  6112. //ocpp_sub_set_remote_start_transaction_req(acDirIndex, NO);
  6113. }
  6114. }
  6115. byte threeGunIndex = 0;
  6116. byte dcIndex = 0;
  6117. bool isGunUsingStatus = false;
  6118. byte scheduleIndex = 0;
  6119. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6120. {
  6121. // 如果有 AC 槍,且 DC 槍也有兩把
  6122. if (acDirIndex == 1 && _index == 1)
  6123. threeGunIndex = 1;
  6124. if (ocpp_sub_get_remote_start_transaction_req(_index + threeGunIndex))
  6125. dcIndex = _index;
  6126. if (chargingInfo[_index]->SystemStatus != SYS_MODE_IDLE)
  6127. {
  6128. isGunUsingStatus = true;
  6129. }
  6130. if (chargingInfo[_index]->schedule.isTriggerStart)
  6131. scheduleIndex = _index;
  6132. }
  6133. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  6134. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  6135. {
  6136. if (dcIndex == 0)
  6137. threeGunIndex = 0;
  6138. //ocpp_sub_set_remote_start_transaction_req(dcIndex + threeGunIndex, NO);
  6139. return;
  6140. }
  6141. if (dcIndex == 0)
  6142. threeGunIndex = 0;
  6143. if (ocpp_sub_get_remote_start_transaction_req(dcIndex + threeGunIndex))
  6144. {
  6145. if (chargingInfo[dcIndex]->SystemStatus == SYS_MODE_IDLE ||
  6146. chargingInfo[dcIndex]->SystemStatus == SYS_MODE_RESERVATION)
  6147. {
  6148. if (ocpp_authorizeRemoteTxReqChk_key())
  6149. {
  6150. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId,
  6151. (char *)ShmOCPP16Data->RemoteStartTransaction[dcIndex + threeGunIndex].IdTag);
  6152. ShmDcCommonData->remoteStartAuthFlag[dcIndex] = YES;
  6153. }
  6154. else
  6155. {
  6156. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  6157. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  6158. ChangeGunSelectByIndex(dcIndex);
  6159. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  6160. DetectPluginStart();
  6161. }
  6162. }
  6163. //ocpp_sub_set_remote_start_transaction_req(dcIndex + threeGunIndex, NO);
  6164. }
  6165. else if (chargingInfo[scheduleIndex]->schedule.isTriggerStart)
  6166. {
  6167. if (chargingInfo[scheduleIndex]->SystemStatus == SYS_MODE_IDLE ||
  6168. chargingInfo[scheduleIndex]->SystemStatus == SYS_MODE_RESERVATION)
  6169. {
  6170. chargingInfo[scheduleIndex]->RemoteStartFlag = YES;
  6171. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  6172. DetectPluginStart();
  6173. }
  6174. chargingInfo[scheduleIndex]->schedule.isTriggerStart = NO;
  6175. }
  6176. }
  6177. }
  6178. bool ocpp_remote_start_wait_cmd()
  6179. {
  6180. bool result = false;
  6181. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6182. {
  6183. result = ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  6184. }
  6185. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6186. {
  6187. result = ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  6188. }
  6189. return result;
  6190. }
  6191. void ocpp_chk_update_cmd()
  6192. {
  6193. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6194. {
  6195. if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  6196. {
  6197. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  6198. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  6199. {
  6200. DEBUG_INFO_MSG("Backend : update start. \n");
  6201. sleep(2);
  6202. memcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing", sizeof("Installing"));
  6203. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  6204. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6205. {
  6206. SetChargerMode(_index, SYS_MODE_UPDATE);
  6207. }
  6208. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  6209. {
  6210. ac_chargingInfo[_index]->SystemStatus = SYS_MODE_UPDATE;
  6211. }
  6212. CloseWatchDog();
  6213. sleep(2);
  6214. KillTask();
  6215. byte updateResult = CheckUpdateProcess();
  6216. if (updateResult == PASS)
  6217. {
  6218. DEBUG_INFO_MSG("Backend : update complete. \n");
  6219. memcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed", sizeof("Installed"));
  6220. }
  6221. else if (updateResult == MODELNAME_FAIL)
  6222. {
  6223. DEBUG_INFO_MSG("Backend : model name is none match. \n");
  6224. memcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed", sizeof("InstallationFailed"));
  6225. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  6226. sleep(5);
  6227. KillAllTask();
  6228. system("/usr/bin/run_evse_restart.sh");
  6229. return;
  6230. }
  6231. else
  6232. {
  6233. DEBUG_INFO_MSG("Backend : update fail. \n");
  6234. memcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed", sizeof("InstallationFailed"));
  6235. }
  6236. memcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed", sizeof("Installed"));
  6237. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  6238. sleep(5);
  6239. system("reboot -f");
  6240. }
  6241. }
  6242. }
  6243. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6244. {
  6245. if(ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES)
  6246. {
  6247. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = NO;
  6248. if (strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded") == EQUAL)
  6249. {
  6250. DEBUG_INFO_MSG("Backend : update start. \n");
  6251. sleep(2);
  6252. memcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installing", sizeof("Installing"));
  6253. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  6254. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6255. {
  6256. SetChargerMode(_index, SYS_MODE_UPDATE);
  6257. }
  6258. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  6259. {
  6260. ac_chargingInfo[_index]->SystemStatus = SYS_MODE_UPDATE;
  6261. }
  6262. CloseWatchDog();
  6263. sleep(2);
  6264. KillTask();
  6265. byte updateResult = CheckUpdateProcess();
  6266. if (updateResult == PASS)
  6267. {
  6268. DEBUG_INFO_MSG("Backend : update complete. \n");
  6269. memcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed", sizeof("Installed"));
  6270. }
  6271. else if (updateResult == MODELNAME_FAIL)
  6272. {
  6273. DEBUG_INFO_MSG("Backend : model name is none match. \n");
  6274. memcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed", sizeof("InstallationFailed"));
  6275. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  6276. sleep(5);
  6277. KillAllTask();
  6278. system("/usr/bin/run_evse_restart.sh");
  6279. return;
  6280. }
  6281. else
  6282. {
  6283. DEBUG_INFO_MSG("Backend : update fail. \n");
  6284. memcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed", sizeof("InstallationFailed"));
  6285. }
  6286. memcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed", sizeof("Installed"));
  6287. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  6288. sleep(5);
  6289. system("reboot -f");
  6290. }
  6291. }
  6292. }
  6293. }
  6294. bool ocpp_chk_invalid_id_cmd(byte gunIndex)
  6295. {
  6296. byte acDirIndex = 0;
  6297. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6298. if (gunIndex == 1)
  6299. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6300. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6301. {
  6302. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  6303. {
  6304. if (strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Blocked") == EQUAL ||
  6305. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Expired") == EQUAL ||
  6306. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Invalid") == EQUAL)
  6307. return true;
  6308. }
  6309. }
  6310. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6311. {
  6312. if (strstr((char *) ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE"))
  6313. {
  6314. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Blocked") == EQUAL ||
  6315. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Expired") == EQUAL ||
  6316. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Invalid") == EQUAL ||
  6317. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NoCredit") == EQUAL ||
  6318. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAllowedTypeEVSE") == EQUAL ||
  6319. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAtThisLocation") == EQUAL ||
  6320. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAtThisTime") == EQUAL ||
  6321. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Unknown") == EQUAL)
  6322. return true;
  6323. }
  6324. }
  6325. return false;
  6326. }
  6327. bool ocpp_chk_profileConf_cmd(byte gunIndex)
  6328. {
  6329. byte acDirIndex = 0;
  6330. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6331. if (gunIndex == 1)
  6332. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6333. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6334. {
  6335. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == YES)
  6336. {
  6337. ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf = NO;
  6338. return true;
  6339. }
  6340. }
  6341. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6342. {
  6343. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == YES)
  6344. {
  6345. ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf = NO;
  6346. return true;
  6347. }
  6348. }
  6349. return false;
  6350. }
  6351. void ocpp_chk_notificationMessage_cmd(byte gunIndex)
  6352. {
  6353. byte acDirIndex = 0;
  6354. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6355. if (gunIndex == 1)
  6356. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6357. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6358. {
  6359. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].TriggerMessageReq == YES)
  6360. {
  6361. if (chargingInfo[gunIndex]->Type == _Type_Chademo &&
  6362. (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  6363. chargingInfo[gunIndex]->SystemStatus ==SYS_MODE_RESERVATION ||
  6364. chargingInfo[gunIndex]->SystemStatus ==SYS_MODE_MAINTAIN))
  6365. {
  6366. if (ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag == NO)
  6367. {
  6368. PRINTF_FUNC("Trigger msg (StatusNotification) = %d (chademo) \n", gunIndex);
  6369. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = 0;
  6370. GetTimespecFunc(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  6371. ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag = YES;
  6372. }
  6373. }
  6374. else if (chargingInfo[gunIndex]->Type == _Type_CCS ||
  6375. chargingInfo[gunIndex]->Type == _Type_GB ||
  6376. chargingInfo[gunIndex]->Type == _Type_AC)
  6377. {
  6378. PRINTF_FUNC("Trigger msg (StatusNotification) = %d (ccs) \n", gunIndex);
  6379. }
  6380. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].TriggerMessageReq = NO;
  6381. }
  6382. }
  6383. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6384. {
  6385. }
  6386. if (ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag)
  6387. {
  6388. int _timeBuf = GetTimeoutValue(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  6389. if (_timeBuf < 0)
  6390. {
  6391. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = 0;
  6392. GetTimespecFunc(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  6393. }
  6394. if (_timeBuf > 30 ||
  6395. (_timeBuf > 2 &&
  6396. chargingInfo[gunIndex]->ConnectorPlugIn))
  6397. {
  6398. PRINTF_FUNC("Trigger msg - timeout or Plugin. \n");
  6399. ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag = NO;
  6400. }
  6401. else
  6402. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = (_timeBuf / uSEC_VAL);
  6403. }
  6404. }
  6405. void ocpp_lift_profileReq_cmd(byte gunIndex)
  6406. {
  6407. byte acDirIndex = 0;
  6408. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6409. if (gunIndex == 1)
  6410. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6411. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6412. {
  6413. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == NO)
  6414. {
  6415. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq == NO)
  6416. ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq = YES;
  6417. }
  6418. }
  6419. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6420. {
  6421. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == NO)
  6422. {
  6423. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq == NO)
  6424. ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq = YES;
  6425. }
  6426. }
  6427. }
  6428. void ocpp_set_startTransation_cmd(byte gunIndex)
  6429. {
  6430. byte _OcppGunIndex = gunIndex;
  6431. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6432. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6433. _OcppGunIndex = 2;
  6434. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6435. {
  6436. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6437. strcpy((char *)chargingInfo[gunIndex]->StartUserId, (char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  6438. else
  6439. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6440. if (strcmp((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, "") == EQUAL)
  6441. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6442. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  6443. PRINTF_FUNC("Gun = %d, IdTag = %s \n", _OcppGunIndex, chargingInfo[gunIndex]->StartUserId);
  6444. }
  6445. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6446. {
  6447. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6448. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6449. else
  6450. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)chargingInfo[gunIndex]->StartUserId);
  6451. ShmOCPP20Data->CpMsg.bits[_OcppGunIndex].TransactionEventReq = YES;
  6452. PRINTF_FUNC("Gun = %d, IdTag = %s \n", _OcppGunIndex, ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6453. }
  6454. }
  6455. void ocpp_set_startTransationPH_cmd(byte gunIndex)
  6456. {
  6457. byte _OcppGunIndex = gunIndex;
  6458. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6459. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6460. _OcppGunIndex = 2;
  6461. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  6462. {
  6463. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6464. {
  6465. if (strcmp((char *)ShmOCPP16DataPH->StartTransaction[_OcppGunIndex].IdTag, "") == EQUAL)
  6466. strcpy((char *)ShmOCPP16DataPH->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6467. ShmOCPP16DataPH->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  6468. }
  6469. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6470. {
  6471. }
  6472. }
  6473. }
  6474. void ocpp_set_stopTransation_cmd(byte gunIndex)
  6475. {
  6476. byte _OcppGunIndex = gunIndex;
  6477. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6478. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6479. _OcppGunIndex = 2;
  6480. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6481. {
  6482. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6483. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  6484. else
  6485. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6486. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  6487. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  6488. }
  6489. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6490. {
  6491. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6492. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6493. else
  6494. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)chargingInfo[gunIndex]->StartUserId);
  6495. PRINTF_FUNC("IdTag = %s \n", ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6496. ShmOCPP20Data->CpMsg.bits[_OcppGunIndex].TransactionEventReq = YES;
  6497. }
  6498. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'P')
  6499. ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gunIndex].ActionCmd = _DC_METER_TRANSACTION_ACTION_STOP;
  6500. }
  6501. void ocpp_set_stopTransationPH_cmd(byte gunIndex)
  6502. {
  6503. byte _OcppGunIndex = gunIndex;
  6504. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6505. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6506. _OcppGunIndex = 2;
  6507. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  6508. {
  6509. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6510. {
  6511. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6512. strcpy((char *)ShmOCPP16DataPH->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16DataPH->StopTransaction[_OcppGunIndex].IdTag);
  6513. else
  6514. strcpy((char *)ShmOCPP16DataPH->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6515. ShmOCPP16DataPH->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  6516. }
  6517. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6518. {
  6519. }
  6520. }
  6521. }
  6522. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString)
  6523. {
  6524. // 參數 gunIndex 已包含 AC 槍
  6525. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6526. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, errString);
  6527. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6528. {
  6529. }
  6530. }
  6531. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason)
  6532. {
  6533. byte acDirIndex = 0;
  6534. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6535. if (gunIndex == 1)
  6536. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6537. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6538. {
  6539. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "") == EQUAL)
  6540. {
  6541. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, reason);
  6542. }
  6543. }
  6544. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6545. {
  6546. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "") == EQUAL)
  6547. {
  6548. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, reason);
  6549. }
  6550. }
  6551. }
  6552. void ocpp_set_authorizeReq_cmd(byte value)
  6553. {
  6554. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6555. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = value;
  6556. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6557. {
  6558. sprintf((char*)ShmOCPP20Data->Authorize.idToken.type, "ISO14443");
  6559. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = value;
  6560. }
  6561. }
  6562. void ocpp_set_authorizeConf_cmd(byte value)
  6563. {
  6564. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6565. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = value;
  6566. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6567. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = value;
  6568. }
  6569. void ocpp_set_errCode_cmd(byte gunIndex)
  6570. {
  6571. byte acDirIndex = 0;
  6572. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6573. if (gunIndex == 1)
  6574. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6575. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6576. {
  6577. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  6578. {
  6579. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "InternalError");
  6580. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, (char *)chargingInfo[gunIndex]->ConnectorAlarmCode);
  6581. }
  6582. else if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  6583. {
  6584. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "OtherError");
  6585. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, (char *)chargingInfo[gunIndex]->EvConnAlarmCode);
  6586. }
  6587. else
  6588. {
  6589. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError");
  6590. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "");
  6591. }
  6592. }
  6593. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6594. {
  6595. }
  6596. }
  6597. void ocpp_clear_errorCode_cmd(byte gunIndex)
  6598. {
  6599. byte acDirIndex = 0;
  6600. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6601. if (gunIndex == 1)
  6602. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6603. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6604. {
  6605. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError") != EQUAL)
  6606. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError");
  6607. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "") != EQUAL)
  6608. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "");
  6609. }
  6610. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6611. {
  6612. }
  6613. }
  6614. void ocpp_default_errorCode_cmd(byte gunIndex)
  6615. {
  6616. byte acDirIndex = 0;
  6617. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6618. if (gunIndex == 1)
  6619. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6620. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6621. {
  6622. if (strcmp ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "" ) == EQUAL)
  6623. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "NoError" );
  6624. }
  6625. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6626. {
  6627. }
  6628. }
  6629. void ocpp_clear_idTag_cmd(byte gun_index)
  6630. {
  6631. byte acDirIndex = 0;
  6632. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6633. if (gun_index == 1)
  6634. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6636. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index + acDirIndex].ResponseIdTagInfo.Status, "");
  6637. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6638. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].Response_idTokenInfo.status, "");
  6639. }
  6640. void ocpp_clear_stopReason(byte gun_index)
  6641. {
  6642. byte acDirIndex = 0;
  6643. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6644. if (gun_index == 1)
  6645. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6646. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6647. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason, "");
  6648. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6649. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].transactionInfo.stoppedReason, "");
  6650. }
  6651. void ocpp_clear_stopTansaction_Idtag(byte gun_index)
  6652. {
  6653. byte acDirIndex = 0;
  6654. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6655. if (gun_index == 1)
  6656. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6657. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6658. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].IdTag, "");
  6659. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6660. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].idToken.idToken, "");
  6661. }
  6662. unsigned short _ocpp_get_connect_timeout()
  6663. {
  6664. uint16_t result = (TIMEOUT_SPEC_HANDSHAKING / 1000);
  6665. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6666. {
  6667. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  6668. result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  6669. }
  6670. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6671. {
  6672. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
  6673. result = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  6674. }
  6675. return result;
  6676. }
  6677. bool ocpp_get_authorize_conf()
  6678. {
  6679. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6680. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  6681. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6682. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  6683. return false;
  6684. }
  6685. bool ocpp_get_startTransation_req(byte gunIndex)
  6686. {
  6687. bool result = false;
  6688. byte acDirIndex = 0;
  6689. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6690. if (gunIndex == 1)
  6691. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6692. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6693. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6694. {
  6695. result = ShmOCPP16Data->CpMsg.bits[gunIndex + acDirIndex].StartTransactionReq;
  6696. }
  6697. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6698. {
  6699. result = ShmOCPP20Data->CpMsg.bits[gunIndex + acDirIndex].TransactionEventReq;
  6700. }
  6701. return result;
  6702. }
  6703. void ocpp_get_freeVendor(byte gunIndex)
  6704. {
  6705. byte _OcppGunIndex = gunIndex;
  6706. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6707. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6708. _OcppGunIndex = 2;
  6709. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6710. {
  6711. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, "");
  6712. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag,
  6713. (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
  6714. }
  6715. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6716. {
  6717. if (strlen((char *)ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value) > 0)
  6718. {
  6719. }
  6720. else
  6721. {
  6722. }
  6723. }
  6724. }
  6725. void ocpp_parsing_DT_defaultPrice_cmd()
  6726. {
  6727. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6728. {
  6729. if(strcmp(MyDefaultPriceString, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData) != EQUAL)
  6730. {
  6731. DefaultPriceHandlerOcpp16((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  6732. memcpy(MyDefaultPriceString, ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, sizeof(MyDefaultPriceString));
  6733. }
  6734. }
  6735. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6736. {
  6737. //ShmOCPP20Data->CostUpdated
  6738. }
  6739. }
  6740. void ocpp_parsing_DT_userPrice_cmd()
  6741. {
  6742. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6743. {
  6744. if (strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken) > 0 &&
  6745. strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.price) > 0)
  6746. {
  6747. // 需要一直讀取 因為不確定下發下來的 idToken 給哪一把槍用
  6748. UserPriceHandlerOcpp16((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken,
  6749. (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  6750. }
  6751. }
  6752. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6753. {
  6754. //ShmOCPP20Data->CostUpdated
  6755. }
  6756. }
  6757. void ocpp_parsing_DT_running_final_cmd()
  6758. {
  6759. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6760. {
  6761. RunningFinalCostHandlerOcpp16();
  6762. }
  6763. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6764. {
  6765. //ShmOCPP20Data->CostUpdated
  6766. }
  6767. }
  6768. void ocpp_chargingProfile_process(byte _index)
  6769. {
  6770. int _time = 0;
  6771. int _startCount = NO_DEFINE;
  6772. int _maxCount = 0;
  6773. byte acDirIndex = 0;
  6774. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6775. if (_index == 1)
  6776. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6777. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6778. {
  6779. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingProfileKind, "Absolute") == EQUAL &&
  6780. ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingProfileId == YES)
  6781. {
  6782. _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.StartSchedule);
  6783. if (_time > -1)
  6784. {
  6785. _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod);
  6786. _startCount = NO_DEFINE;
  6787. for (byte _count = 0; _count < _maxCount; _count++)
  6788. {
  6789. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  6790. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  6791. {
  6792. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  6793. ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  6794. {
  6795. _startCount = _count;
  6796. }
  6797. }
  6798. }
  6799. if (_startCount < _maxCount)
  6800. {
  6801. if (ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit >= (ShmPsuData->SystemAvailableCurrent / 10))
  6802. {
  6803. chargingInfo[_index]->ChargingProfilePower = -1;
  6804. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6805. }
  6806. else
  6807. {
  6808. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount);
  6809. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  6810. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 && chargingInfo[_index]->PresentChargingVoltage > 0)
  6811. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  6812. else
  6813. chargingInfo[_index]->ChargingProfileCurrent = 0;
  6814. }
  6815. }
  6816. else
  6817. {
  6818. chargingInfo[_index]->ChargingProfilePower = -1;
  6819. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6820. }
  6821. }
  6822. else
  6823. {
  6824. chargingInfo[_index]->ChargingProfilePower = -1;
  6825. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6826. }
  6827. }
  6828. else
  6829. {
  6830. chargingInfo[_index]->ChargingProfilePower = -1;
  6831. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6832. }
  6833. if (strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == EQUAL &&
  6834. ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES)
  6835. {
  6836. _time = GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  6837. if (_time > -1)
  6838. {
  6839. _maxCount = ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  6840. _startCount = NO_DEFINE;
  6841. for (byte _count = 0; _count < _maxCount; _count++)
  6842. {
  6843. if (_time >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  6844. {
  6845. if ((_count == 0 && ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  6846. ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  6847. {
  6848. _startCount = _count;
  6849. }
  6850. }
  6851. }
  6852. //PRINTF_FUNC("_startCount = %d, Max Profile Limit = %f \n", _startCount, ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  6853. if (_startCount < _maxCount)
  6854. {
  6855. if (ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit >= (ShmPsuData->SystemAvailableCurrent / 10))
  6856. {
  6857. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6858. }
  6859. else
  6860. {
  6861. float maxChargingProfilePow = ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  6862. if (_chargingCount > 0)
  6863. maxChargingProfilePow /= _chargingCount;
  6864. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = maxChargingProfilePow;
  6865. }
  6866. }
  6867. else
  6868. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6869. }
  6870. }
  6871. else
  6872. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6873. }
  6874. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6875. {
  6876. if (strcmp((char *)ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingProfileKind, "Absolute") == EQUAL &&
  6877. ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].id == YES)
  6878. {
  6879. _time = GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].startSchedule);
  6880. _maxCount = ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod);
  6881. _startCount = NO_DEFINE;
  6882. for (byte _count = 0; _count < _maxCount; _count++)
  6883. {
  6884. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  6885. if (_time >= ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].startPeriod)
  6886. {
  6887. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].limit >= MIN_OUTPUT_CUR) ||
  6888. ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].limit > MIN_OUTPUT_CUR)
  6889. {
  6890. _startCount = _count;
  6891. }
  6892. }
  6893. }
  6894. if (_startCount < _maxCount)
  6895. {
  6896. if (ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit >= (ShmPsuData->SystemAvailableCurrent / 10))
  6897. {
  6898. chargingInfo[_index]->ChargingProfilePower = -1;
  6899. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6900. }
  6901. else
  6902. {
  6903. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount);
  6904. //PRINTF_FUNC("Profile Limit = %f \n", ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit);
  6905. chargingInfo[_index]->ChargingProfilePower = ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit * AC_OUTPUT_VOL;
  6906. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 && chargingInfo[_index]->PresentChargingVoltage > 0)
  6907. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  6908. else
  6909. chargingInfo[_index]->ChargingProfileCurrent = 0;
  6910. }
  6911. }
  6912. else
  6913. {
  6914. chargingInfo[_index]->ChargingProfilePower = -1;
  6915. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6916. }
  6917. }
  6918. else
  6919. {
  6920. chargingInfo[_index]->ChargingProfilePower = -1;
  6921. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6922. }
  6923. }
  6924. else
  6925. {
  6926. chargingInfo[_index]->ChargingProfilePower = -1;
  6927. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6928. }
  6929. if (ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower != -1)
  6930. {
  6931. chargingInfo[_index]->ChargingProfilePower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  6932. if (_ChargingMaxProfilePwBuffer[_index] != ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower)
  6933. {
  6934. _ChargingMaxProfilePwBuffer[_index] = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  6935. PRINTF_FUNC("Profile : MaxChargingProfilePower = %.2f \n", ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower);
  6936. }
  6937. }
  6938. if (chargingInfo[_index]->ChargingProfilePower >= 0 &&
  6939. _ChargingProfilePwBuffer[_index] != chargingInfo[_index]->ChargingProfilePower)
  6940. {
  6941. _ChargingProfilePwBuffer[_index] = chargingInfo[_index]->ChargingProfilePower;
  6942. PRINTF_FUNC("Profile : ChargingProfilePower = %.2f \n", chargingInfo[_index]->ChargingProfilePower);
  6943. PRINTF_FUNC("Profile : ChargingProfileCurrent = %.2f \n", chargingInfo[_index]->ChargingProfileCurrent);
  6944. }
  6945. }
  6946. void ocpp_ocmf_information(byte _index)
  6947. {
  6948. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6949. {
  6950. memcpy((char *)ShmOCPP16Data->OcmfData[_index].DataString,
  6951. (char *)ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[_index].transactionOCMF,
  6952. sizeof(ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[_index].transactionOCMF));
  6953. memcpy((char *)ShmOCPP16Data->OcmfData[_index].PublicKey,
  6954. (char *)ShmSysConfigAndInfo->SysInfo.DcMeterStatusInfo[_index].publicKeyOcmf,
  6955. sizeof(ShmSysConfigAndInfo->SysInfo.DcMeterStatusInfo[_index].publicKeyOcmf));
  6956. ShmOCPP16Data->OcmfData[_index].SendOcmfDataReq = YES;
  6957. }
  6958. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6959. {
  6960. }
  6961. }
  6962. void CheckOcppProfileConfirm(byte gun_index)
  6963. {
  6964. if ((chargingInfo [gun_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo [gun_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  6965. (chargingInfo [gun_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo [gun_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  6966. {
  6967. ocpp_chk_profileConf_cmd ( gun_index );
  6968. }
  6969. }
  6970. void CheckAcOcppProfileConfirm()
  6971. {
  6972. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING &&
  6973. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  6974. {
  6975. ocpp_ac_chk_profileConf_cmd();
  6976. }
  6977. }
  6978. //===============================================
  6979. // wifi schedule
  6980. //===============================================
  6981. bool WifiScheduleStop(byte gunIndex)
  6982. {
  6983. bool result = false;
  6984. result = chargingInfo[gunIndex]->schedule.isTriggerStop;
  6985. chargingInfo[gunIndex]->schedule.isTriggerStop = NO;
  6986. return result;
  6987. }
  6988. //===============================================
  6989. // SQLite3 related routine
  6990. //===============================================
  6991. int NetworkDB_Open(sqlite3 *db)
  6992. {
  6993. int result = PASS;
  6994. char* errMsg = NULL;
  6995. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  6996. "idx integer primary key AUTOINCREMENT, "
  6997. "occurDatetime text NOT NULL, "
  6998. "isInternet text NOT NULL, "
  6999. "isOcppConnected text NOT NULL, "
  7000. "isEth0Internet text NOT NULL, "
  7001. "isMlan0Internet text NOT NULL, "
  7002. "isPpp0Internet text NOT NULL, "
  7003. "rssiMlan0 text NOT NULL, "
  7004. "rssiPpp0 text NOT NULL"
  7005. ");";
  7006. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  7007. if(sqlite3_open(NETWORK_DB_FILE, &db))
  7008. {
  7009. result = FAIL;
  7010. PRINTF_FUNC( "Can't open database (Net) : %s\n", sqlite3_errmsg(db));
  7011. sqlite3_close(db);
  7012. }
  7013. else
  7014. {
  7015. PRINTF_FUNC( "Local network status record database open successfully.\n");
  7016. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  7017. {
  7018. result = FAIL;
  7019. PRINTF_FUNC( "Create local network status record table error message: %s\n", errMsg);
  7020. }
  7021. else
  7022. {
  7023. PRINTF_FUNC( "Opened local network status record table successfully\n");
  7024. }
  7025. sqlite3_close(db);
  7026. }
  7027. return result;
  7028. }
  7029. int DB_Open(sqlite3 *db)
  7030. {
  7031. int result = PASS;
  7032. char* errMsg = NULL;
  7033. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  7034. "idx integer primary key AUTOINCREMENT, "
  7035. "reservationId text, "
  7036. "transactionId text, "
  7037. "startMethod text, "
  7038. "userId text, "
  7039. "dateTimeStart text, "
  7040. "dateTimeStop text,"
  7041. "socStart text, "
  7042. "socStop text, "
  7043. "chargeEnergy text, "
  7044. "stopReason text,"
  7045. "vincode text"
  7046. ");";
  7047. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  7048. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  7049. "`item` TEXT NOT NULL, "
  7050. "`connector` INTEGER NOT NULL, "
  7051. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  7052. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  7053. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  7054. "`occurDatetime` TEXT NOT NULL, "
  7055. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  7056. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  7057. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  7058. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  7059. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  7060. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  7061. "`connector` INTEGER NOT NULL, "
  7062. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  7063. char* createDcMeterSql="CREATE TABLE IF NOT EXISTS ocmf_record("
  7064. "idx integer primary key AUTOINCREMENT, "
  7065. "transactionId text, "
  7066. "status INTEGER NOT NULL, "
  7067. "connector INTEGER NOT NULL, "
  7068. "publicKey text, "
  7069. "ocmfInfo text"
  7070. ");";
  7071. // char * createTestSql = "CREATE TABLE IF NOT EXISTS `for_test` ( "
  7072. // "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  7073. // "`connector` INTEGER NOT NULL, "
  7074. // "`testID` TEXT, "
  7075. // "`checkStatus` INTEGER NOT NULL "
  7076. // ");";
  7077. if(sqlite3_open(DB_FILE, &db))
  7078. {
  7079. result = FAIL;
  7080. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  7081. sqlite3_close(db);
  7082. }
  7083. else
  7084. {
  7085. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  7086. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  7087. {
  7088. result = FAIL;
  7089. PRINTF_FUNC( "Create local charging record table error message: %s\n", errMsg);
  7090. }
  7091. else
  7092. {
  7093. PRINTF_FUNC( "Opened local charging record table successfully\n");
  7094. }
  7095. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  7096. {
  7097. result = FAIL;
  7098. PRINTF_FUNC( "Create local config table error message: %s\n", errMsg);
  7099. }
  7100. else
  7101. {
  7102. PRINTF_FUNC( "Opened local config table successfully\n");
  7103. }
  7104. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  7105. {
  7106. result = FAIL;
  7107. PRINTF_FUNC( "Create local record table error message: %s\n", errMsg);
  7108. }
  7109. else
  7110. {
  7111. PRINTF_FUNC( "Opened local record table successfully\n");
  7112. }
  7113. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  7114. {
  7115. result = FAIL;
  7116. PRINTF_FUNC( "Create reboot record table error message: %s\n", errMsg);
  7117. }
  7118. else
  7119. {
  7120. PRINTF_FUNC( "Opened reboot record table successfully\n");
  7121. }
  7122. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  7123. {
  7124. result = FAIL;
  7125. PRINTF_FUNC( "Create power consumption record table error message: %s\n", errMsg);
  7126. }
  7127. else
  7128. {
  7129. PRINTF_FUNC( "Opened power consumption record table successfully\n");
  7130. }
  7131. if (sqlite3_exec(db, createDcMeterSql, 0, 0, &errMsg) != SQLITE_OK)
  7132. {
  7133. result = FAIL;
  7134. PRINTF_FUNC( "Create OCMF record table error message: %s\n", errMsg);
  7135. }
  7136. else
  7137. {
  7138. PRINTF_FUNC( "Opened OCMF record table successfully\n");
  7139. }
  7140. // if (sqlite3_exec(db, createTestSql, 0, 0, &errMsg) != SQLITE_OK)
  7141. // {
  7142. // result = FAIL;
  7143. // printf( "Create test table error message: %s\n", errMsg);
  7144. // }
  7145. // else
  7146. // {
  7147. // printf( "Opened test table successfully\n");
  7148. // }
  7149. sqlite3_close(db);
  7150. }
  7151. return result;
  7152. }
  7153. int DB_Insert_Record(sqlite3 *db, int gun_index)
  7154. {
  7155. int result = PASS;
  7156. char* errMsg = NULL;
  7157. char insertSql[1024];
  7158. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  7159. {
  7160. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, vincode) "
  7161. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  7162. chargingInfo[gun_index]->ReservationId,
  7163. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  7164. chargingInfo[gun_index]->StartMethod,
  7165. chargingInfo[gun_index]->StartUserId,
  7166. chargingInfo[gun_index]->StartDateTime,
  7167. chargingInfo[gun_index]->StopDateTime,
  7168. chargingInfo[gun_index]->EvBatteryStartSoc,
  7169. chargingInfo[gun_index]->EvBatterySoc,
  7170. chargingInfo[gun_index]->PresentChargedEnergy,
  7171. ShmOCPP16Data->StopTransaction[gun_index].StopReason,
  7172. chargingInfo[gun_index]->EVCCID);
  7173. }
  7174. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  7175. {
  7176. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  7177. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  7178. chargingInfo[gun_index]->ReservationId,
  7179. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  7180. chargingInfo[gun_index]->StartMethod,
  7181. chargingInfo[gun_index]->StartUserId,
  7182. chargingInfo[gun_index]->StartDateTime,
  7183. chargingInfo[gun_index]->StopDateTime,
  7184. chargingInfo[gun_index]->EvBatteryStartSoc,
  7185. chargingInfo[gun_index]->EvBatterySoc,
  7186. chargingInfo[gun_index]->PresentChargedEnergy,
  7187. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  7188. }
  7189. if(sqlite3_open(DB_FILE, &db))
  7190. {
  7191. result = FAIL;
  7192. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  7193. sqlite3_close(db);
  7194. }
  7195. else
  7196. {
  7197. PRINTF_FUNC( "Local charging record database open successfully.\n");
  7198. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  7199. {
  7200. result = FAIL;
  7201. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  7202. }
  7203. else
  7204. {
  7205. PRINTF_FUNC( "Insert local charging record successfully\n");
  7206. }
  7207. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  7208. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  7209. {
  7210. result = FAIL;
  7211. PRINTF_FUNC( "delete local charging error message: %s\n", errMsg);
  7212. }
  7213. else
  7214. {
  7215. PRINTF_FUNC( "delete local charging record successfully\n");
  7216. }
  7217. sqlite3_close(db);
  7218. }
  7219. return result;
  7220. }
  7221. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  7222. {
  7223. uint8_t result = false;
  7224. char* errMsg = NULL;
  7225. char sqlStr[1024];
  7226. srand(time(NULL));
  7227. if(sqlite3_open(DB_FILE, &db))
  7228. {
  7229. result = FAIL;
  7230. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  7231. sqlite3_close(db);
  7232. }
  7233. else
  7234. {
  7235. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  7236. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  7237. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  7238. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  7239. {
  7240. result = FAIL;
  7241. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  7242. }
  7243. else
  7244. {
  7245. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  7246. }
  7247. sqlite3_close(db);
  7248. }
  7249. return result;
  7250. }
  7251. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  7252. {
  7253. uint8_t result = true;
  7254. char* errMsg = NULL;
  7255. char sqlStr[1024];
  7256. char **rs;
  7257. int rows, cols;
  7258. sprintf(sqlStr, "select * from config where item='IsAvailable' and connector=%d;", gun_index);
  7259. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  7260. if(sqlite3_open(DB_FILE, &db))
  7261. {
  7262. result = FAIL;
  7263. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  7264. sqlite3_close(db);
  7265. }
  7266. else
  7267. {
  7268. //PRINTF_FUNC( "Local config query database open successfully.\r\n");
  7269. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  7270. if(rows>0)
  7271. {
  7272. for(int idxRow=1;idxRow<=rows;idxRow++)
  7273. {
  7274. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  7275. {
  7276. result = false;
  7277. }
  7278. PRINTF_FUNC("Query connector-%d isOperactive: %s\r\n", gun_index, rs[(idxRow*cols)+3]);
  7279. }
  7280. }
  7281. else
  7282. {
  7283. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.\r\n", gun_index);
  7284. }
  7285. sqlite3_free_table(rs);
  7286. sqlite3_close(db);
  7287. }
  7288. return result;
  7289. }
  7290. void DB_Update_PowerConsumption(sqlite3 *db, uint8_t gun_index, float value)
  7291. {
  7292. char* errMsg = NULL;
  7293. char sqlStr[1024];
  7294. srand(time(NULL));
  7295. if(sqlite3_open(DB_FILE, &db))
  7296. {
  7297. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  7298. sqlite3_close(db);
  7299. }
  7300. else
  7301. {
  7302. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  7303. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  7304. {
  7305. PRINTF_FUNC( "update power_consumption error message: %s\n", errMsg);
  7306. }
  7307. else
  7308. {
  7309. //PRINTF_FUNC("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  7310. }
  7311. sqlite3_close(db);
  7312. }
  7313. }
  7314. float DB_Get_PowerConsumption(sqlite3 *db, uint8_t gun_index)
  7315. {
  7316. float result = 0;
  7317. char* errMsg = NULL;
  7318. char sqlStr[1024];
  7319. char **rs;
  7320. int rows, cols;
  7321. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  7322. if (sqlite3_open(DB_FILE, &db))
  7323. {
  7324. result = FAIL;
  7325. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  7326. sqlite3_close(db);
  7327. }
  7328. else
  7329. {
  7330. //PRINTF_FUNC("Local config query database open successfully.\r\n");
  7331. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  7332. if (rows > 0)
  7333. {
  7334. for (int idxRow = 1; idxRow <= rows; idxRow++)
  7335. {
  7336. result = atof(rs[(idxRow * cols) + 2]);
  7337. }
  7338. }
  7339. else
  7340. {
  7341. PRINTF_FUNC( "Query connector-%d fail, set default value to consumption.\r\n", gun_index);
  7342. }
  7343. sqlite3_free_table(rs);
  7344. sqlite3_close(db);
  7345. }
  7346. return result;
  7347. }
  7348. int DB_Reboot_Record(sqlite3 *db)
  7349. {
  7350. int result = PASS;
  7351. char* errMsg = NULL;
  7352. char insertSql[256];
  7353. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  7354. if(sqlite3_open(DB_FILE, &db))
  7355. {
  7356. result = FAIL;
  7357. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  7358. sqlite3_close(db);
  7359. }
  7360. else
  7361. {
  7362. PRINTF_FUNC( "Local charging record database open successfully.\n");
  7363. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  7364. {
  7365. result = FAIL;
  7366. PRINTF_FUNC( "Insert reboot record error message: %s\n", errMsg);
  7367. }
  7368. else
  7369. {
  7370. PRINTF_FUNC( "Insert reboot record successfully\n");
  7371. }
  7372. sprintf(insertSql, "delete from reboot_record where idx < (select idx from reboot_record order by idx desc limit 1)-2000;");
  7373. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  7374. {
  7375. result = FAIL;
  7376. PRINTF_FUNC( "delete reboot record error message: %s\n", errMsg);
  7377. }
  7378. else
  7379. {
  7380. PRINTF_FUNC( "delete reboot record successfully\n");
  7381. }
  7382. sqlite3_close(db);
  7383. }
  7384. return result;
  7385. }
  7386. int DB_Insert_ocmf(sqlite3 *db, uint8_t gun_index)
  7387. {
  7388. int result = PASS;
  7389. char* errMsg = NULL;
  7390. char insertSql[2048];
  7391. memset(insertSql, 0, sizeof(insertSql));
  7392. sprintf(insertSql, "insert into ocmf_record (transactionId, status, connector) values('%d', 0, '%d');",
  7393. ShmDcCommonData->startTransactionID[gun_index],
  7394. gun_index);
  7395. if (sqlite3_open(DB_FILE, &db))
  7396. {
  7397. result = FAIL;
  7398. PRINTF_FUNC("Can't open database: %s\n", sqlite3_errmsg(db));
  7399. sqlite3_close(db);
  7400. }
  7401. else
  7402. {
  7403. if (sqlite3_exec(db, insertSql, 0, 0, & errMsg) != SQLITE_OK)
  7404. {
  7405. result = FAIL;
  7406. PRINTF_FUNC("Insert ocmf error message : %s\n", errMsg);
  7407. }
  7408. else
  7409. {
  7410. PRINTF_FUNC("Insert ocmf successfully\n");
  7411. }
  7412. sqlite3_close(db);
  7413. }
  7414. return result;
  7415. }
  7416. void DB_Update_ocmf(sqlite3 *db, uint8_t gun_index)
  7417. {
  7418. char* errMsg = NULL;
  7419. char insertSql[4096];
  7420. memset(insertSql, 0, sizeof(insertSql));
  7421. sprintf(insertSql, "update ocmf_record set status=1, publicKey='%s', ocmfInfo='%s' where status=0 and transactionId=%d and connector=%d;",
  7422. ShmSysConfigAndInfo->SysInfo.DcMeterStatusInfo[gun_index].publicKeyOcmf,
  7423. ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].transactionOCMF,
  7424. ShmDcCommonData->startTransactionID[gun_index],
  7425. gun_index);
  7426. if (sqlite3_open(DB_FILE, &db))
  7427. {
  7428. PRINTF_FUNC("Can't open database: %s\n", sqlite3_errmsg(db));
  7429. sqlite3_close(db);
  7430. }
  7431. else
  7432. {
  7433. if (sqlite3_exec(db, insertSql, 0, 0, & errMsg) != SQLITE_OK)
  7434. {
  7435. PRINTF_FUNC("Update ocmf error message : %s\n", errMsg);
  7436. }
  7437. else
  7438. {
  7439. PRINTF_FUNC("Update ocmf successfully \n");
  7440. }
  7441. sqlite3_close(db);
  7442. }
  7443. }
  7444. void DB_Update_ocmf_Record(sqlite3 *db, uint8_t gun_index, int _trid, bool isGet)
  7445. {
  7446. char* errMsg = NULL;
  7447. char insertSql[4096];
  7448. memset(insertSql, 0, sizeof(insertSql));
  7449. if (isGet)
  7450. {
  7451. sprintf(insertSql, "update ocmf_record set status=1, ocmfInfo='%s' where status=0 and transactionId=%d and connector=%d;",
  7452. ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].transactionOCMF,
  7453. _trid,
  7454. gun_index);
  7455. }
  7456. else
  7457. {
  7458. sprintf(insertSql, "update ocmf_record set status=2 where status=0 and transactionId=%d and connector=%d;",
  7459. _trid,
  7460. gun_index);
  7461. }
  7462. if (sqlite3_open(DB_FILE, &db))
  7463. {
  7464. PRINTF_FUNC("Can't open database: %s\n", sqlite3_errmsg(db));
  7465. sqlite3_close(db);
  7466. }
  7467. else
  7468. {
  7469. if (sqlite3_exec(db, insertSql, 0, 0, & errMsg) != SQLITE_OK)
  7470. {
  7471. PRINTF_FUNC("Update ocmf error message : %s\n", errMsg);
  7472. }
  7473. else
  7474. {
  7475. PRINTF_FUNC("Update ocmf_Record successfully \n");
  7476. }
  7477. sqlite3_close(db);
  7478. }
  7479. }
  7480. int DB_Get_OcmfTrid(sqlite3 *db, uint8_t gun_index)
  7481. {
  7482. int result = 0;
  7483. char* errMsg = NULL;
  7484. char sqlStr[2048];
  7485. char **rs;
  7486. int rows, cols;
  7487. memset(sqlStr, 0, sizeof(sqlStr));
  7488. sprintf(sqlStr, "select * from ocmf_record where status=0 and connector=%d and transactionId!=0;", gun_index);
  7489. if (sqlite3_open(DB_FILE, &db))
  7490. {
  7491. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  7492. sqlite3_close(db);
  7493. }
  7494. else
  7495. {
  7496. //PRINTF_FUNC("Local config query database open successfully.\r\n");
  7497. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  7498. if (rows > 0)
  7499. {
  7500. for (int idxRow = 1; idxRow <= rows; idxRow++)
  7501. {
  7502. result = atoi(rs[(idxRow * cols) + 1]);
  7503. break;
  7504. }
  7505. }
  7506. sqlite3_free_table(rs);
  7507. sqlite3_close(db);
  7508. }
  7509. return result;
  7510. }
  7511. int DB_Insert_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  7512. {
  7513. int result = PASS;
  7514. char* errMsg = NULL;
  7515. char insertSql[1024];
  7516. sprintf(insertSql, "insert into for_test (connector, testID, checkStatus) values(%d, %s, 0);",
  7517. gun_index, textID);
  7518. if(sqlite3_open(DB_FILE, &db))
  7519. {
  7520. result = FAIL;
  7521. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  7522. sqlite3_close(db);
  7523. }
  7524. else
  7525. {
  7526. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  7527. {
  7528. result = FAIL;
  7529. PRINTF_FUNC( "Insert sdlu test error message : %s\n", errMsg);
  7530. }
  7531. else
  7532. {
  7533. PRINTF_FUNC( "Insert sdlu test successfully\n");
  7534. }
  7535. sqlite3_close(db);
  7536. }
  7537. return result;
  7538. }
  7539. int DB_delete_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  7540. {
  7541. int result = PASS;
  7542. char* errMsg = NULL;
  7543. char insertSql[1024];
  7544. sprintf(insertSql, "delete from for_test where testID = %s", textID);
  7545. if (sqlite3_open(DB_FILE, &db))
  7546. {
  7547. result = FAIL;
  7548. PRINTF_FUNC("Can't open database: %s\n", sqlite3_errmsg(db));
  7549. sqlite3_close(db);
  7550. }
  7551. else
  7552. {
  7553. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  7554. {
  7555. result = FAIL;
  7556. PRINTF_FUNC( "Test message 2: %s\n", errMsg);
  7557. }
  7558. else
  7559. {
  7560. PRINTF_FUNC( "Test message 2 successfully\n");
  7561. }
  7562. sqlite3_close(db);
  7563. }
  7564. return result;
  7565. }
  7566. int DB_sql_test(sqlite3 *db, uint8_t gun_index)
  7567. {
  7568. int result = PASS;
  7569. char* errMsg = NULL;
  7570. char insertSql[1024];
  7571. char valueStr[128];
  7572. char **rs;
  7573. int rows, cols;
  7574. sprintf(insertSql, "select testID from for_test where checkStatus=0;");
  7575. if (sqlite3_open(DB_FILE, &db))
  7576. {
  7577. result = FAIL;
  7578. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  7579. sqlite3_close(db);
  7580. }
  7581. else
  7582. {
  7583. PRINTF_FUNC("Local for_test query database open successfully.\r\n");
  7584. sqlite3_get_table(db, insertSql, &rs, &rows, &cols, &errMsg);
  7585. if (rows > 0)
  7586. {
  7587. for (int idxRow = 1; idxRow <= rows; idxRow++)
  7588. {
  7589. strcpy(valueStr, rs[(idxRow * cols)]);
  7590. printf("valueStr = %s \n", valueStr);
  7591. }
  7592. }
  7593. else
  7594. {
  7595. PRINTF_FUNC("select testID from for_test fail \r\n");
  7596. }
  7597. sqlite3_free_table(rs);
  7598. sqlite3_close(db);
  7599. }
  7600. return result;
  7601. }
  7602. //===============================================
  7603. // Config process
  7604. //===============================================
  7605. void AddPlugInTimes(byte gunIndex)
  7606. {
  7607. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  7608. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  7609. else if(chargingInfo[gunIndex]->Type == _Type_CCS)
  7610. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  7611. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  7612. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  7613. }
  7614. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  7615. {
  7616. char cmdBuf[32];
  7617. struct timeb csuTime;
  7618. struct tm *tmCSU;
  7619. ftime(&csuTime);
  7620. tmCSU = localtime(&csuTime.time);
  7621. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  7622. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  7623. tmCSU->tm_sec);
  7624. if (isStart)
  7625. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  7626. else
  7627. {
  7628. if (strcmp((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00") != EQUAL)
  7629. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  7630. }
  7631. }
  7632. void zipLogFiles()
  7633. {
  7634. const char* logPath = "/Storage/SystemLog";
  7635. // 獲取目錄
  7636. DIR* pDir = opendir(logPath);
  7637. if (pDir != NULL)
  7638. {
  7639. struct timeb csuTime;
  7640. struct tm *tmCSU;
  7641. ftime(&csuTime);
  7642. tmCSU = localtime(&csuTime.time);
  7643. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  7644. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  7645. // tmCSU->tm_sec);
  7646. // Read items inside the folder
  7647. struct dirent* pEntry = NULL;
  7648. while ((pEntry = readdir(pDir)) != NULL)
  7649. {
  7650. if (strcmp(pEntry->d_name, ".") != 0 &&
  7651. strcmp(pEntry->d_name, "..") != 0 &&
  7652. strncmp(pEntry->d_name, "[", 1) == 0 &&
  7653. strstr(pEntry->d_name, "tar") < 0)
  7654. {
  7655. char yearC[5];
  7656. unsigned short year = 0;
  7657. char monthC[3];
  7658. unsigned short month = 0;
  7659. yearC[4] = '\0';
  7660. strncpy(yearC, pEntry->d_name + 1, 4);
  7661. monthC[2] = '\0';
  7662. strncpy(monthC, pEntry->d_name + 6, 2);
  7663. year = atoi(yearC);
  7664. month = atoi(monthC);
  7665. if (year != 0)
  7666. {
  7667. if (year < tmCSU->tm_year + 1900 ||
  7668. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  7669. {
  7670. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  7671. char file[256];
  7672. memset(file, 0x00, sizeof(file));
  7673. strcat(file, "tar zcvf ");
  7674. strcat(file, logPath);
  7675. strncat(file, "/", 1);
  7676. strcat(file, pEntry->d_name);
  7677. strcat(file, ".tar");
  7678. strncat(file, " ", 1);
  7679. strcat(file, logPath);
  7680. strncat(file, "/", 1);
  7681. strcat(file, pEntry->d_name);
  7682. PRINTF_FUNC("zip = %s \n", file);
  7683. system(file);
  7684. }
  7685. }
  7686. }
  7687. }
  7688. }
  7689. // Close folder
  7690. closedir(pDir);
  7691. }
  7692. void ChangeGunSelectByIndex(byte sel)
  7693. {
  7694. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  7695. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  7696. }
  7697. void CheckIsAlternatvieByModelName()
  7698. {
  7699. if(ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'W')
  7700. {
  7701. // 壁掛
  7702. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  7703. }
  7704. else
  7705. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  7706. }
  7707. void StopProcessingLoop()
  7708. {
  7709. for (;;)
  7710. {
  7711. CheckTask();
  7712. CheckFactoryConfigFunction();
  7713. CheckFwUpdateFunction();
  7714. ocpp_chk_reset_cmd(YES);
  7715. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  7716. {
  7717. // 經過底下的 function 後,ShmSysConfigAndInfo->SysWarningInfo.Level 會再被更新一次
  7718. ChkPrimaryStatus();
  7719. ButtonTriggerEvent();
  7720. ReviewCriticalAlarm();
  7721. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_NORMAL &&
  7722. ShmDcCommonData->rebootCount < 1)
  7723. {
  7724. PRINTF_FUNC("Soft reboot for retry self-test (Primary). \n");
  7725. SetupRebootCount(1);
  7726. KillAllTask();
  7727. sleep(3);
  7728. system("/usr/bin/run_evse_restart.sh");
  7729. return;
  7730. }
  7731. }
  7732. sleep(1);
  7733. }
  7734. }
  7735. bool IsConnectorWholeIdle()
  7736. {
  7737. bool result = true;
  7738. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  7739. {
  7740. if (chargingInfo[count]->SystemStatus != SYS_MODE_IDLE &&
  7741. chargingInfo[count]->SystemStatus != SYS_MODE_RESERVATION)
  7742. {
  7743. result = false;
  7744. break;
  7745. }
  7746. }
  7747. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  7748. {
  7749. if (ac_chargingInfo[count]->SystemStatus != SYS_MODE_IDLE &&
  7750. ac_chargingInfo[count]->IsErrorOccur == NO)
  7751. {
  7752. result = false;
  7753. break;
  7754. }
  7755. }
  7756. return result;
  7757. }
  7758. void ClearAlarmCodeWhenAcOff()
  7759. {
  7760. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  7761. {
  7762. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  7763. }
  7764. }
  7765. //==========================================
  7766. // Check task processing
  7767. //==========================================
  7768. void CheckTask()
  7769. {
  7770. if(reset4gStep == RESET_4G_STEP_NONE && _isUsing4G)
  7771. {
  7772. if(system("pidof -s Module_4g > /dev/null") != 0)
  7773. {
  7774. DEBUG_ERROR_MSG("Module_4g not running, restart it.\r\n");
  7775. system("/root/Module_4g &");
  7776. }
  7777. }
  7778. if(reset4gStep == RESET_4G_STEP_NONE && _isUsingWifi)
  7779. {
  7780. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  7781. {
  7782. DEBUG_ERROR_MSG("Module_Wifi not running, restart it.\r\n");
  7783. system("/root/Module_Wifi &");
  7784. }
  7785. }
  7786. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'P')
  7787. {
  7788. if(system("pidof -s Module_DcMeter > /dev/null") != 0)
  7789. {
  7790. DEBUG_ERROR_MSG("Module_DcMeter not running, restart it.\r\n");
  7791. system("/root/Module_DcMeter &");
  7792. }
  7793. }
  7794. if (_verifyMechanism == VERIFY_MECHANISM_BAZEL8)
  7795. {
  7796. if(system("pidof -s Module_Payment_Bazel8 > /dev/null") != 0)
  7797. {
  7798. DEBUG_ERROR_MSG("Module_Payment_Bazel8 not running, restart it.\r\n");
  7799. system("/root/Module_Payment_Bazel8 &");
  7800. }
  7801. }
  7802. ocpp_process_start();
  7803. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  7804. {
  7805. DEBUG_ERROR_MSG("Module_ProduceUtils not running, restart it.\r\n");
  7806. system ("/root/Module_ProduceUtils &");
  7807. }
  7808. }
  7809. void Check267Alarm()
  7810. {
  7811. bool canRun = true;
  7812. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7813. {
  7814. if((chargingInfo[_index]->SystemStatus > SYS_MODE_IDLE && chargingInfo[_index]->SystemStatus < SYS_MODE_TERMINATING) ||
  7815. (chargingInfo[_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7816. {
  7817. canRun = false;
  7818. break;
  7819. }
  7820. }
  7821. if (canRun)
  7822. {
  7823. if ( ! ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  7824. {
  7825. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  7826. }
  7827. }
  7828. }
  7829. void CheckPaymentID()
  7830. {
  7831. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0 &&
  7832. !isPaymentBusy)
  7833. {
  7834. isPaymentBusy = true;
  7835. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isReq = true;
  7836. }
  7837. else if (ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes)
  7838. {
  7839. ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isRes = false;
  7840. if (ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isResultPass)
  7841. {
  7842. isPaymentBusy = false;
  7843. PRINTF_FUNC ( "txnId = %s \n", ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId );
  7844. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  7845. ShmSysConfigAndInfo->SysInfo.bazel8.txResp.txnId);
  7846. }
  7847. else
  7848. isPaymentBusy = false;
  7849. PRINTF_FUNC ( "isWaitRes = %d \n", ShmSysConfigAndInfo->SysInfo.bazel8.cmdPreAuth.isWaitRes );
  7850. }
  7851. }
  7852. void CheckSystemTaskAlive()
  7853. {
  7854. unsigned char lostId = CheckSystemTask(ShmSysConfigAndInfo->SysInfo.SystemPage);
  7855. if (lostId != 0)
  7856. {
  7857. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost == NO)
  7858. {
  7859. EmcOccurThenStopCharging();
  7860. sleep(2);
  7861. if (lostId == _SYSTEM_TASK_LOST_ITEM_MAIN) PRINTF_FUNC ( "System task lost (CSU). \n" );
  7862. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVCOMM)
  7863. {
  7864. PRINTF_FUNC ( "System task lost (EVComm). \n" );
  7865. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun ++)
  7866. {
  7867. chargingInfo [gun]->ConnectorPlugIn = NO;
  7868. }
  7869. }
  7870. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PSUCOMM) PRINTF_FUNC ( "System task lost (PSU Task). \n" );
  7871. else if (lostId == _SYSTEM_TASK_LOST_ITEM_SMARTBOX) PRINTF_FUNC ( "System task lost (SmartBox Task). \n" );
  7872. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVENTLOG) PRINTF_FUNC ( "System task lost (Event log). \n" );
  7873. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PRIMARYCOMM) PRINTF_FUNC ( "System task lost (Primary). \n" );
  7874. else if (lostId == _SYSTEM_TASK_LOST_ITEM_LCMCONTROL) PRINTF_FUNC ( "System task lost (LCM Comm). \n" );
  7875. else if (lostId == _SYSTEM_TASK_LOST_ITEM_INTERCOMM) PRINTF_FUNC("System task lost (Internal Comm). \n");
  7876. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = YES;
  7877. }
  7878. }
  7879. else
  7880. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = NO;
  7881. }
  7882. void InitialDHCP()
  7883. {
  7884. char tmpbuf[256];
  7885. memset(tmpbuf,0,256);
  7886. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  7887. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  7888. system(tmpbuf);
  7889. }
  7890. void Run4gResetProc()
  7891. {
  7892. if (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == _SYS_4G_MODE_ENABLE ||
  7893. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_STATION)
  7894. {
  7895. if (!ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  7896. !ShmSysConfigAndInfo->SysInfo.InternetConn)
  7897. {
  7898. if (reset4gStep == RESET_4G_STEP_NONE)
  7899. {
  7900. if (_4gResetChkFlag >= 300)
  7901. {
  7902. PRINTF_FUNC("Reset 4G/Wifi module. \n");
  7903. reset4gStep = RESET_4G_STEP_DELETE_TASK;
  7904. }
  7905. else
  7906. _4gResetChkFlag++;
  7907. }
  7908. }
  7909. else
  7910. _4gResetChkFlag = 0;
  7911. switch(reset4gStep)
  7912. {
  7913. case RESET_4G_STEP_DELETE_TASK:
  7914. {
  7915. PRINTF_FUNC("Killall 4G / Wifi task. \n");
  7916. system("killall Module_4g &");
  7917. system("killall Module_Wifi &");
  7918. reset4gStep = RESET_4G_STEP_GPIO_HIGH;
  7919. }
  7920. break;
  7921. case RESET_4G_STEP_GPIO_HIGH:
  7922. {
  7923. PRINTF_FUNC("Disable 4G/Wifi. GPIO-104 -> High. \n");
  7924. system("echo 1 > /sys/class/gpio/gpio104/value");
  7925. reset4gStep = RESET_4G_STEP_GPIO_LOW_COMP;
  7926. }
  7927. break;
  7928. case RESET_4G_STEP_GPIO_LOW_COMP:
  7929. {
  7930. PRINTF_FUNC("Enable 4G/Wifi. GPIO-104 -> Low. \n");
  7931. system("echo 0 > /sys/class/gpio/gpio104/value");
  7932. reset4gStep = RESET_4G_STEP_NONE;
  7933. _4gResetChkFlag = 0;
  7934. }
  7935. break;
  7936. }
  7937. }
  7938. else
  7939. {
  7940. if (reset4gStep != RESET_4G_STEP_NONE)
  7941. system("echo 0 > /sys/class/gpio/gpio104/value");
  7942. reset4gStep = RESET_4G_STEP_NONE;
  7943. _4gResetChkFlag = 0;
  7944. }
  7945. }
  7946. //==========================================
  7947. // Check Smart Charging Profile
  7948. //==========================================
  7949. int GetStartScheduleTime(unsigned char *time)
  7950. {
  7951. int result = -1;
  7952. struct tm tmScheduleStart;
  7953. struct timeb tbScheduleStart;
  7954. if((sscanf((char *)time, "%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))
  7955. {
  7956. tmScheduleStart.tm_year -= 1900;
  7957. tmScheduleStart.tm_mon -= 1;
  7958. tbScheduleStart.time = mktime(&tmScheduleStart);
  7959. tbScheduleStart.millitm = 0;
  7960. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  7961. }
  7962. return result;
  7963. }
  7964. //==========================================
  7965. // Ocmf
  7966. //==========================================
  7967. void ConfigOcmfInfo(byte gun_index)
  7968. {
  7969. strncpy((char *)ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].head.evseId,
  7970. (char *)ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  7971. char trId[32];
  7972. sprintf(trId, "%d", ShmDcCommonData->startTransactionID[gun_index]);
  7973. memcpy((char *)ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].head.transactionId, trId,
  7974. sizeof(trId));
  7975. char clientId[32] = "DC_Charger";
  7976. memcpy((char *)ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].head.clientId, clientId,
  7977. sizeof(clientId));
  7978. ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].head.tariffId = 0;
  7979. ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].head.cableId = 0;
  7980. memcpy((char *)ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].head.userData,
  7981. (char *)chargingInfo[gun_index]->StartUserId,
  7982. sizeof(chargingInfo[gun_index]->StartUserId));
  7983. }
  7984. void ConfigOcmfInfo_Record(int _trID, byte gun_index)
  7985. {
  7986. strncpy((char *)ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].head.evseId,
  7987. (char *)ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  7988. char trId[32];
  7989. sprintf(trId, "%d", _trID);
  7990. strcpy((char *)ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].head.transactionId, trId);
  7991. strcpy((char *)ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].head.clientId, "DC_Charger");
  7992. ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].head.tariffId = 0;
  7993. ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].head.cableId = 0;
  7994. strcpy((char *)ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].head.userData, "Record");
  7995. }
  7996. void ChkOcmfRecord(byte gun_index)
  7997. {
  7998. int trid = DB_Get_OcmfTrid(localDb, gun_index);
  7999. if (trid != 0 &&
  8000. trid != ShmDcCommonData->ocmfTridRecord[gun_index] &&
  8001. trid != ShmDcCommonData->startTransactionID[gun_index])
  8002. {
  8003. PRINTF_FUNC("OCMF Record Send : trid = %d\n", trid);
  8004. ShmDcCommonData->ocmfTridRecord[gun_index] = trid;
  8005. ConfigOcmfInfo_Record(ShmDcCommonData->ocmfTridRecord[gun_index], gun_index);
  8006. ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].ActionCmd = _DC_METER_TRANSACTION_ACTION_START;
  8007. }
  8008. if (ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].OcmfInfoReady == _OCMF_INFO_READY_YES)
  8009. {
  8010. PRINTF_FUNC("OCMF Record - Reture Pass \n");
  8011. ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].OcmfInfoReady = _OCMF_INFO_READY_NO;
  8012. ocpp_ocmf_information(gun_index);
  8013. DB_Update_ocmf_Record(localDb, gun_index, ShmDcCommonData->ocmfTridRecord[gun_index], true);
  8014. ShmDcCommonData->ocmfTridRecord[gun_index] = 0;
  8015. }
  8016. else if (ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].OcmfInfoReady == _OCMF_INFO_READY_FAIL)
  8017. {
  8018. PRINTF_FUNC("OCMF Record - Reture Fail \n");
  8019. ShmSysConfigAndInfo->SysInfo.DcMeterReadTransactionRecord[gun_index].OcmfInfoReady = _OCMF_INFO_READY_NO;
  8020. DB_Update_ocmf_Record(localDb, gun_index, ShmDcCommonData->ocmfTridRecord[gun_index], false);
  8021. ShmDcCommonData->ocmfTridRecord[gun_index] = 0;
  8022. }
  8023. }
  8024. //==========================================
  8025. // sub function
  8026. //==========================================
  8027. void CheckReturnToChargingConn()
  8028. {
  8029. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  8030. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  8031. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  8032. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  8033. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  8034. {
  8035. bool isReturnTimeout = false;
  8036. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  8037. {
  8038. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  8039. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  8040. {
  8041. if ((chargingInfo[count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= SYS_MODE_COMPLETE) ||
  8042. (chargingInfo[count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  8043. {
  8044. isReturnTimeout = false;
  8045. break;
  8046. }
  8047. }
  8048. else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  8049. {
  8050. if ((chargingInfo[count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= SYS_MODE_COMPLETE) ||
  8051. (chargingInfo[count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  8052. {
  8053. isReturnTimeout = true;
  8054. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  8055. }
  8056. }
  8057. }
  8058. // AC 槍
  8059. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8060. {
  8061. // 沒有選中 AC,且 AC 在充電中
  8062. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  8063. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_COMPLETE))
  8064. {
  8065. // 當前 DC 充電槍在 idle 狀態
  8066. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE ||
  8067. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  8068. {
  8069. isReturnTimeout = true;
  8070. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  8071. }
  8072. }
  8073. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  8074. ((chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_COMPLETE) ||
  8075. (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)))
  8076. {
  8077. // 當前 DC 充電槍在 idle 狀態
  8078. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE ||
  8079. ac_chargingInfo[0]->SystemStatus == SYS_MODE_RESERVATION)
  8080. {
  8081. isReturnTimeout = true;
  8082. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  8083. }
  8084. }
  8085. }
  8086. if (!isReturnTimeout)
  8087. StopSystemTimeoutDet();
  8088. }
  8089. }
  8090. bool GetStartChargingByAlterMode(byte _gun)
  8091. {
  8092. bool result = true;
  8093. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2 &&
  8094. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  8095. {
  8096. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select++)
  8097. {
  8098. if (_select != _gun)
  8099. {
  8100. if (chargingInfo[_select]->SystemStatus != SYS_MODE_IDLE &&
  8101. chargingInfo[_select]->SystemStatus != SYS_MODE_RESERVATION)
  8102. {
  8103. result = false;
  8104. break;
  8105. }
  8106. }
  8107. }
  8108. }
  8109. return result;
  8110. }
  8111. void TheEndCharging(byte gun_index)
  8112. {
  8113. chargingInfo[gun_index]->isRemoteStart = NO;
  8114. StopGunInfoTimeoutDet(gun_index);
  8115. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  8116. ChangeStartOrStopDateTime(NO, gun_index);
  8117. DB_Insert_Record(localDb, gun_index);
  8118. }
  8119. void AdjustChargerCurrent()
  8120. {
  8121. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  8122. // 設定的電流~ 如超過可輸出的電流,則 bypass
  8123. if (ShmSysConfigAndInfo->SysConfig.RatingCurrent < ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent ||
  8124. ShmSysConfigAndInfo->SysConfig.RatingCurrent == 0)
  8125. {
  8126. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent = 0;
  8127. }
  8128. PRINTF_FUNC ("System : MaxChargingPower = %d (KW), MaxChargingCurrent = %d (A)",
  8129. ShmPsuData->SystemAvailablePower / 10,
  8130. ShmPsuData->SystemAvailableCurrent / 10
  8131. );
  8132. PRINTF_FUNC ("Web Config : ChargingPower = %d (KW), ChargingCurrent = %d (A)",
  8133. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  8134. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent
  8135. );
  8136. }
  8137. bool CheckTheSystemType()
  8138. {
  8139. bool result = false;
  8140. char EvsePower[2];
  8141. EvsePower[2] = '\0';
  8142. strncpy ( EvsePower, (char *) (ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2 );
  8143. ShmDcCommonData->halfGroupCount = 1;
  8144. if (strcmp ( EvsePower, "30" ) == EQUAL)
  8145. ShmDcCommonData->systemType = _SYSTEM_TYPE_SIMPLE;
  8146. else
  8147. {
  8148. // 60KW 以上的系統
  8149. if (ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity == 0 &&
  8150. ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity == 0)
  8151. ShmDcCommonData->systemType = _SYSTEM_TYPE_STANDARD;
  8152. else
  8153. {
  8154. ShmDcCommonData->halfGroupCount = 2;
  8155. ShmDcCommonData->systemType = _SYSTEM_TYPE_SMART;
  8156. }
  8157. if ((ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity != 0 && ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity == 0) ||
  8158. (ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity == 0 && ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity != 0))
  8159. {
  8160. result = true;
  8161. }
  8162. }
  8163. return result;
  8164. }
  8165. void CheckStatusOfMeter()
  8166. {
  8167. // 橋接如果搭上~ 則兩個電表都是使用狀態
  8168. // 如橋接沒有搭上~ 則視該火線 relay 狀態為電表使用狀態
  8169. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  8170. {
  8171. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++)
  8172. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  8173. }
  8174. else
  8175. {
  8176. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++)
  8177. {
  8178. if (chargingInfo[mIndex]->RelayKPK2Status == YES || chargingInfo[mIndex]->RelayK1K2Status)
  8179. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  8180. else
  8181. ShmCsuMeterData->_meter[mIndex].isCalculation = NO;
  8182. }
  8183. }
  8184. }
  8185. void ReviewCriticalAlarm()
  8186. {
  8187. if (ShmDcCommonData->GunRelayDrivingOccur[0] == YES ||
  8188. ShmDcCommonData->GunRelayDrivingOccur[1] == YES ||
  8189. ShmDcCommonData->GunRelayWeldingOccur[0] == YES ||
  8190. ShmDcCommonData->GunRelayWeldingOccur[1] == YES ||
  8191. ShmDcCommonData->ParaRelayWeldingOccur == YES ||
  8192. ShmDcCommonData->ParaRelayDrivingOccur == YES ||
  8193. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip ||
  8194. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip ||
  8195. (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen && ShmDcCommonData->_tiltSensorStep == _TILT_SENSOR_STEP_NONE) ||
  8196. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  8197. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm ||
  8198. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU ||
  8199. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost)
  8200. {
  8201. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_CRITICAL;
  8202. }
  8203. else
  8204. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_NORMAL;
  8205. }
  8206. void CheckRelayWeldingOrDrivingFault(byte gun_index)
  8207. {
  8208. // relay welding fault then stop the charging process.
  8209. byte faultCode = RELAY_STATUS_ERROR_NONE;
  8210. if (gun_index == 0)
  8211. {
  8212. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  8213. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  8214. {
  8215. faultCode = RELAY_STATUS_ERROR_WELDING;
  8216. }
  8217. else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  8218. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  8219. {
  8220. faultCode = RELAY_STATUS_ERROR_DRIVING;
  8221. }
  8222. }
  8223. else if (gun_index == 1)
  8224. {
  8225. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  8226. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  8227. {
  8228. faultCode = RELAY_STATUS_ERROR_WELDING;
  8229. }
  8230. else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  8231. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  8232. {
  8233. faultCode = RELAY_STATUS_ERROR_DRIVING;
  8234. }
  8235. }
  8236. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2 &&
  8237. !ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  8238. {
  8239. // 橋接
  8240. if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  8241. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  8242. ShmDcCommonData->RelayCheckStatus[RELAY_PRE_STATUS] == RELAY_STATUS_ERROR_WELDING)
  8243. {
  8244. faultCode = RELAY_STATUS_ERROR_PARA_WELDING;
  8245. }
  8246. else if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  8247. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  8248. ShmDcCommonData->RelayCheckStatus[RELAY_PRE_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  8249. {
  8250. faultCode = RELAY_STATUS_ERROR_PARA_DRIVING;
  8251. }
  8252. }
  8253. if (faultCode == RELAY_STATUS_ERROR_WELDING)
  8254. {
  8255. // welding
  8256. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  8257. RecordAlarmCode(gun_index, "011011");
  8258. else if (chargingInfo[gun_index]->Type == _Type_GB)
  8259. RecordAlarmCode(gun_index, "011015");
  8260. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  8261. RecordAlarmCode(gun_index, "011013");
  8262. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = YES;
  8263. EmcOccurThenStopCharging();
  8264. }
  8265. else if (faultCode == RELAY_STATUS_ERROR_DRIVING)
  8266. {
  8267. // driving
  8268. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  8269. RecordAlarmCode(gun_index, "011012");
  8270. else if (chargingInfo[gun_index]->Type == _Type_GB)
  8271. RecordAlarmCode(gun_index, "011016");
  8272. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  8273. RecordAlarmCode(gun_index, "011014");
  8274. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = YES;
  8275. EmcOccurThenStopCharging();
  8276. }
  8277. else
  8278. {
  8279. if (chargingInfo[gun_index]->SystemStatus <= SYS_MODE_TERMINATING ||
  8280. chargingInfo[gun_index]->SystemStatus >= SYS_MODE_ALARM)
  8281. {
  8282. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = NO;
  8283. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = NO;
  8284. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  8285. {
  8286. ResetChargerAlarmCode(gun_index, "011012");
  8287. ResetChargerAlarmCode(gun_index, "011011");
  8288. }
  8289. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  8290. {
  8291. ResetChargerAlarmCode(gun_index, "011014");
  8292. ResetChargerAlarmCode(gun_index, "011013");
  8293. }
  8294. else if (chargingInfo[gun_index]->Type == _Type_GB)
  8295. {
  8296. ResetChargerAlarmCode(gun_index, "011016");
  8297. ResetChargerAlarmCode(gun_index, "011015");
  8298. }
  8299. }
  8300. }
  8301. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2)
  8302. {
  8303. if ((chargingInfo[0]->SystemStatus <= SYS_MODE_TERMINATING ||
  8304. chargingInfo[0]->SystemStatus >= SYS_MODE_ALARM) &&
  8305. (chargingInfo[1]->SystemStatus <= SYS_MODE_TERMINATING ||
  8306. chargingInfo[1]->SystemStatus >= SYS_MODE_ALARM))
  8307. {
  8308. if (faultCode == RELAY_STATUS_ERROR_PARA_WELDING)
  8309. {
  8310. // parallel welding
  8311. RecordAlarmCode ( gun_index, "011039" );
  8312. ShmDcCommonData->ParaRelayWeldingOccur = YES;
  8313. EmcOccurThenStopCharging ();
  8314. }
  8315. else if (faultCode == RELAY_STATUS_ERROR_PARA_DRIVING)
  8316. {
  8317. // parallel driving
  8318. RecordAlarmCode ( gun_index, "011040" );
  8319. ShmDcCommonData->ParaRelayDrivingOccur = YES;
  8320. EmcOccurThenStopCharging ();
  8321. }
  8322. else
  8323. {
  8324. ShmDcCommonData->ParaRelayWeldingOccur = NO;
  8325. ShmDcCommonData->ParaRelayDrivingOccur = NO;
  8326. ResetChargerAlarmCode ( gun_index, "011039" );
  8327. ResetChargerAlarmCode ( gun_index, "011040" );
  8328. }
  8329. }
  8330. }
  8331. }
  8332. void KillOcppTask()
  8333. {
  8334. system ("pkill OcppBackend");
  8335. system ("pkill OcppBackend20");
  8336. }
  8337. bool PrecheckIsPass(byte gun_index)
  8338. {
  8339. bool result = true;
  8340. // relay welding or driving 是反向
  8341. if (result)
  8342. result = !ShmDcCommonData->GunRelayWeldingOccur[gun_index];
  8343. if (result)
  8344. result = !ShmDcCommonData->ParaRelayWeldingOccur;
  8345. return result;
  8346. }
  8347. void CollectError(byte gun_index)
  8348. {
  8349. errCollect.GunErrMessage |= ShmDcCommonData->ConnectErrList[gun_index].GunErrMessage;
  8350. }
  8351. void AlarmCheck(byte type)
  8352. {
  8353. // type : Cha = 0、CCS = 1、GBT = 2
  8354. if (type == _Type_Chademo)
  8355. {
  8356. // GFD Trip
  8357. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  8358. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  8359. else
  8360. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  8361. // UVP
  8362. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  8363. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  8364. else
  8365. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  8366. // OTP
  8367. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  8368. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = YES;
  8369. else
  8370. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = NO;
  8371. // OVP
  8372. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  8373. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = YES;
  8374. else
  8375. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = NO;
  8376. // GFD Warning
  8377. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  8378. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  8379. else
  8380. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  8381. // Relay Welding
  8382. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  8383. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  8384. else
  8385. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = NO;
  8386. // Relay Driving
  8387. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  8388. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  8389. else
  8390. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  8391. // Connect temp Sensor broken
  8392. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  8393. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = YES;
  8394. else
  8395. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = NO;
  8396. // Output UCP
  8397. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 0))
  8398. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = YES;
  8399. else
  8400. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = NO;
  8401. // Parallel Relay Welding
  8402. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 3))
  8403. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  8404. else
  8405. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  8406. // Parallel Relay Driving
  8407. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 4))
  8408. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  8409. else
  8410. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  8411. // Stop By Evse Condition
  8412. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 5))
  8413. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = YES;
  8414. else
  8415. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = NO;
  8416. }
  8417. else if (type == _Type_CCS)
  8418. {
  8419. // GFD Trip
  8420. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  8421. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  8422. else
  8423. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  8424. // UVP
  8425. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  8426. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  8427. else
  8428. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  8429. // OTP
  8430. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  8431. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = YES;
  8432. else
  8433. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = NO;
  8434. // OVP
  8435. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  8436. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = YES;
  8437. else
  8438. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = NO;
  8439. // GFD Warning
  8440. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  8441. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  8442. else
  8443. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  8444. // Relay Welding
  8445. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  8446. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  8447. else
  8448. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = NO;
  8449. // Relay Driving
  8450. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  8451. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  8452. else
  8453. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  8454. // Connect temp Sensor broken
  8455. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  8456. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = YES;
  8457. else
  8458. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = NO;
  8459. // Output UCP
  8460. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 1))
  8461. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = YES;
  8462. else
  8463. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = NO;
  8464. // Parallel Relay Welding
  8465. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 3))
  8466. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  8467. else
  8468. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  8469. // Parallel Relay Driving
  8470. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 4))
  8471. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  8472. else
  8473. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  8474. // Stop By Evse Condition
  8475. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 5))
  8476. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = YES;
  8477. else
  8478. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = NO;
  8479. }
  8480. else if (type == _Type_GB)
  8481. {
  8482. // GFD Trip
  8483. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  8484. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  8485. else
  8486. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  8487. // UVP
  8488. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  8489. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  8490. else
  8491. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  8492. // OTP
  8493. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  8494. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = YES;
  8495. else
  8496. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = NO;
  8497. // OVP
  8498. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  8499. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = YES;
  8500. else
  8501. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = NO;
  8502. // GFD Warning
  8503. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  8504. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  8505. else
  8506. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  8507. // Relay Welding
  8508. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  8509. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  8510. else
  8511. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = NO;
  8512. // Relay Driving
  8513. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  8514. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  8515. else
  8516. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  8517. // Connect temp Sensor broken
  8518. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  8519. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = YES;
  8520. else
  8521. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = NO;
  8522. // Output UCP
  8523. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 2))
  8524. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = YES;
  8525. else
  8526. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = NO;
  8527. // Parallel Relay Welding
  8528. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 3))
  8529. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  8530. else
  8531. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  8532. // Parallel Relay Driving
  8533. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 4))
  8534. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  8535. else
  8536. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  8537. // Stop By Evse Condition
  8538. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 5))
  8539. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = YES;
  8540. else
  8541. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = NO;
  8542. }
  8543. }
  8544. void SystemAlarmCheck()
  8545. {
  8546. byte system = 3;
  8547. // Parallel Welding
  8548. if (DetectBitValue(errCollect.GunErrMessage >> (8 * system), 3))
  8549. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  8550. else
  8551. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  8552. // Parallel Driving
  8553. if (DetectBitValue(errCollect.GunErrMessage >> (8 * system), 4))
  8554. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  8555. else
  8556. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  8557. }
  8558. void SavePresentChargedDuration(byte gun_index)
  8559. {
  8560. int _diffTime = 0;
  8561. ftime(&endChargingTime[gun_index]);
  8562. _diffTime = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  8563. // below 0 or over 5 sec is abnormal
  8564. if (_diffTime < 0 ||
  8565. _diffTime > chargingInfo[gun_index]->PresentChargedDuration + 5)
  8566. {
  8567. _presentChargingTimeBuf[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  8568. ftime(&startChargingTime[gun_index]);
  8569. PRINTF_FUNC("TimeZone changed (%d) \n", gun_index);
  8570. }
  8571. else
  8572. chargingInfo[gun_index]->PresentChargedDuration = _presentChargingTimeBuf[gun_index] + _diffTime;
  8573. }
  8574. void ClearEnergyTimePeriod(byte gun_index)
  8575. {
  8576. for (int i = 0; i < 24; i++)
  8577. {
  8578. ShmDcCommonData->energy_time_period[gun_index][i] = 0;
  8579. }
  8580. }
  8581. void CheckOcppCostAndPrice()
  8582. {
  8583. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling &&
  8584. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  8585. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  8586. {
  8587. // 支援 Noodoe (N0 / N7) and CTEP1.0 and CTEP2.0
  8588. ocpp_parsing_DT_defaultPrice_cmd();
  8589. ocpp_parsing_DT_userPrice_cmd();
  8590. ocpp_parsing_DT_running_final_cmd();
  8591. }
  8592. }
  8593. void CheckAuthrizeByEvccid()
  8594. {
  8595. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  8596. ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID)
  8597. ShmDcCommonData->enObtainAuthorizbyCCID = YES;
  8598. else
  8599. ShmDcCommonData->enObtainAuthorizbyCCID = NO;
  8600. }
  8601. void ConnectorOTP(byte _index)
  8602. {
  8603. bool isOTP = false;
  8604. if (chargingInfo[_index]->Type == _Type_Chademo)
  8605. {
  8606. if (ShmDcCommonData->ConnectErrList[_index].GunBits.ChaConnectOTP)
  8607. isOTP = true;
  8608. }
  8609. else if (chargingInfo[_index]->Type == _Type_CCS)
  8610. {
  8611. if (ShmDcCommonData->ConnectErrList[_index].GunBits.CCSConnectOTP)
  8612. isOTP = true;
  8613. }
  8614. else if (chargingInfo[_index]->Type == _Type_GB)
  8615. {
  8616. if (ShmDcCommonData->ConnectErrList[_index].GunBits.GBTConnectOTP)
  8617. isOTP = true;
  8618. }
  8619. if (isOTP)
  8620. {
  8621. if (gunTempAllowCount[_index] >= 1)
  8622. chargingInfo[_index]->StopChargeFlag = YES;
  8623. else
  8624. gunTempAllowCount[_index] += 1;
  8625. }
  8626. }
  8627. void CheckConnectorOTP(byte _index)
  8628. {
  8629. if (chargingInfo[_index]->Type == _Type_Chademo &&
  8630. (chargingInfo[_index]->ModelType == 'K' ||
  8631. chargingInfo[_index]->ModelType == 'S' ||
  8632. chargingInfo[_index]->ModelType == 'W'))
  8633. {
  8634. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  8635. {
  8636. ResetChargerAlarmCode(_index, "011018");
  8637. if (chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  8638. RecordAlarmCode(_index, "012229");
  8639. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  8640. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  8641. ResetChargerAlarmCode(_index, "012229");
  8642. }
  8643. else
  8644. {
  8645. // 沒接上 Sensor or 異常
  8646. RecordAlarmCode(_index, "011018");
  8647. ResetChargerAlarmCode(_index, "012229");
  8648. }
  8649. }
  8650. else if (chargingInfo[_index]->Type == _Type_CCS)
  8651. {
  8652. // CCS 不管甚麼輸出都會有槍溫偵測!!~
  8653. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  8654. {
  8655. ResetChargerAlarmCode(_index, "011019");
  8656. if(chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  8657. RecordAlarmCode(_index, "012230");
  8658. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  8659. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  8660. ResetChargerAlarmCode(_index, "012230");
  8661. }
  8662. else
  8663. {
  8664. // 沒接上 Sensor or 異常
  8665. RecordAlarmCode(_index, "011019");
  8666. ResetChargerAlarmCode(_index, "012230");
  8667. }
  8668. }
  8669. else if (chargingInfo[_index]->Type == _Type_GB &&
  8670. chargingInfo[_index]->ModelType == 'B')
  8671. {
  8672. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  8673. {
  8674. ResetChargerAlarmCode(_index, "011020");
  8675. if(chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  8676. RecordAlarmCode(_index, "012231");
  8677. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  8678. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  8679. ResetChargerAlarmCode(_index, "012231");
  8680. }
  8681. else
  8682. {
  8683. // 沒接上 Sensor or 異常
  8684. RecordAlarmCode(_index, "011020");
  8685. ResetChargerAlarmCode(_index, "012231");
  8686. }
  8687. }
  8688. ConnectorOTP(_index);
  8689. }
  8690. void ChkConnectorBoardStop(byte index)
  8691. {
  8692. if (isEvBoardStopChargeFlag(index) == YES)
  8693. {
  8694. // 板端要求停止 (錯誤)
  8695. ocpp_set_stopReason_by_cmd(index, "EVDisconnected");
  8696. ChargingAlarmProcess(index);
  8697. }
  8698. else if (isEvBoardNormalStopChargeFlag(index) == YES)
  8699. {
  8700. // 板端要求停止 (正常)
  8701. ocpp_set_stopReason_by_cmd(index, "EVDisconnected");
  8702. ChargingTerminalProcess(index);
  8703. }
  8704. }
  8705. void ChkNoCharging(byte index)
  8706. {
  8707. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  8708. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  8709. {
  8710. PRINTF_FUNC("Offline Policy : No Charing... \n");
  8711. ocpp_set_stopReason_by_cmd(index, "Other");
  8712. ChargingTerminalProcess(index);
  8713. }
  8714. }
  8715. void ChkPsuErrorStop(byte index)
  8716. {
  8717. if (ShmPsuData->PsuGroup[index].GroupErrorFlag.bits.PsuFailure == YES ||
  8718. (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf && ShmPsuData->PsuGroup[0].GroupErrorFlag.bits.PsuFailure))
  8719. {
  8720. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8721. {
  8722. if (chargingInfo[gun_index]->PresentChargingCurrent <= 5 &&
  8723. ((chargingInfo[gun_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  8724. (chargingInfo[gun_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[gun_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)))
  8725. {
  8726. ocpp_set_stopReason_by_cmd(gun_index, "Other");
  8727. ChargingAlarmProcess(gun_index);
  8728. }
  8729. }
  8730. }
  8731. }
  8732. void ChkBackendStop(byte index)
  8733. {
  8734. if (ocpp_chk_invalid_id_cmd(index))
  8735. {
  8736. // Start Transaction Stop
  8737. ocpp_set_stopReason_by_cmd(index, "DeAuthorized");
  8738. ChargingTerminalProcess(index);
  8739. }
  8740. else if (ocpp_chk_remoteStop_cmd(index) == YES ||
  8741. WifiScheduleStop(index))
  8742. {
  8743. // 後臺要求停止
  8744. ChargingTerminalProcess(index);
  8745. }
  8746. }
  8747. void ChkConfigurationStop(byte index)
  8748. {
  8749. if (CheckWebConfigChargingTimeout(index) ||
  8750. CheckWebConfigChargingEnergy(index) ||
  8751. CheckWebConfigChargingSOC(index))
  8752. {
  8753. // 網頁設定
  8754. PRINTF_FUNC("CheckBackendCharging stop charging (%d) \n", index);
  8755. RecordAlarmCode(index, "023984");
  8756. ChargingTerminalProcess(index);
  8757. return;
  8758. }
  8759. if (CheckOcppChargingTimeout(index) ||
  8760. CheckOcppChargingEnergy(index) ||
  8761. CheckOcppChargingSOC(index))
  8762. {
  8763. // OCPP 設定
  8764. PRINTF_FUNC("CheckOcppCharging stop charging (%d) \n", index);
  8765. RecordAlarmCode(index, "023984");
  8766. ChargingTerminalProcess(index);
  8767. }
  8768. }
  8769. void RecordGfdResult(byte index)
  8770. {
  8771. if (chargingInfo[index]->Type == _Type_Chademo)
  8772. {
  8773. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  8774. {
  8775. // GFD 錯誤停止
  8776. RecordAlarmCode(index, "012234");
  8777. ocpp_set_stopReason_by_cmd(index, "Other");
  8778. ChargingAlarmProcess(index);
  8779. }
  8780. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  8781. {
  8782. // GFD 警告
  8783. RecordAlarmCode(index, "012296");
  8784. }
  8785. }
  8786. else if (chargingInfo[index]->Type == _Type_GB)
  8787. {
  8788. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  8789. {
  8790. // GFD 錯誤停止
  8791. RecordAlarmCode(index, "012236");
  8792. ocpp_set_stopReason_by_cmd ( index, "Other" );
  8793. ChargingAlarmProcess ( index );
  8794. }
  8795. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  8796. {
  8797. // GFD 警告
  8798. RecordAlarmCode(index, "012298");
  8799. }
  8800. }
  8801. else if (chargingInfo[index]->Type == _Type_CCS)
  8802. {
  8803. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  8804. {
  8805. // GFD 錯誤停止
  8806. RecordAlarmCode(index, "012235");
  8807. ocpp_set_stopReason_by_cmd(index, "Other");
  8808. ChargingAlarmProcess ( index );
  8809. }
  8810. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  8811. {
  8812. // GFD 警告
  8813. RecordAlarmCode(index, "012297");
  8814. }
  8815. }
  8816. }
  8817. void CcidAuthorizationProcessing(byte gun_index)
  8818. {
  8819. //LW_GBT_EVCCID
  8820. if ((chargingInfo [gun_index]->Type == _Type_CCS || chargingInfo [gun_index]->Type == _Type_GB) &&
  8821. chargingInfo [gun_index]->IsAvailable &&
  8822. ShmDcCommonData->enObtainAuthorizbyCCID &&
  8823. chargingInfo[gun_index]->ConnectorPlugIn &&
  8824. strcmp ((char *)ShmSysConfigAndInfo->SysConfig.UserId, "") == EQUAL)
  8825. {
  8826. AddPlugInTimes(gun_index);
  8827. PRINTF_FUNC("CcidAuthorizationProcessing. \n");
  8828. ChangeGunSelectByIndex(gun_index);
  8829. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8830. }
  8831. }
  8832. void CheckCcidAuthorizationFunction(byte gun_index)
  8833. {
  8834. //LW_GBT_EVCCID .. add GB gun type.
  8835. // if ((chargingInfo [gun_index]->Type == _Type_CCS || chargingInfo [gun_index]->Type == _Type_GB) &&
  8836. // ShmDcCommonData->enObtainAuthorizbyCCID)
  8837. {
  8838. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_GET &&
  8839. ShmDcCommonData->_CcidAuthProcessStep == _CCID_NONE &&
  8840. strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" ) == EQUAL)
  8841. {
  8842. PRINTF_FUNC ( "index = %d, Get for CCID (main). \n", gun_index );
  8843. ShmDcCommonData->_authWithCcidFlag [gun_index] = _CCID_CHECK;
  8844. ShmDcCommonData->_CcidAuthProcessStep = _CCID_CHECK;
  8845. //LWADD_EVCCID
  8846. if(strcmp((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[EVCCID_PREFIX].ItemData, "") != EQUAL)
  8847. {
  8848. strcpy((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[EVCCID_PREFIX].ItemData);
  8849. strcat((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)chargingInfo [gun_index]->EVCCID);
  8850. PRINTF_FUNC ( "Prfix CCID:%s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  8851. }
  8852. else
  8853. strcpy((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)chargingInfo[gun_index]->EVCCID);
  8854. }
  8855. else if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  8856. {
  8857. if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHFAIL)
  8858. {
  8859. PRINTF_FUNC ( "index = %d, _CCID_AUTHFAIL. \n", gun_index );
  8860. ShmDcCommonData->_authWithCcidFlag [gun_index] = ShmDcCommonData->_CcidAuthProcessStep;
  8861. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  8862. }
  8863. else if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHCOMP)
  8864. {
  8865. PRINTF_FUNC ( "index = %d, _CCID_AUTHCOMP. \n", gun_index );
  8866. ShmDcCommonData->_authWithCcidFlag [gun_index] = ShmDcCommonData->_CcidAuthProcessStep;
  8867. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  8868. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8869. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  8870. }
  8871. }
  8872. }
  8873. }
  8874. //LW_GBT_EVCCID
  8875. byte TransactionResponseStatus(byte _index)
  8876. {
  8877. byte _checkResult = NO_DEFINE;
  8878. _checkResult = ocpp_is_resPass_StartTransactionConf(_index);
  8879. if (_checkResult == START_TRANSATION_STATUS_PASS)
  8880. {
  8881. if (ocpp_chk_invalid_id_cmd(_index))
  8882. _checkResult = START_TRANSATION_STATUS_FAIL;
  8883. }
  8884. else
  8885. {
  8886. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  8887. {
  8888. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING ||
  8889. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8890. !ocpp_get_startTransation_req(_index) ||
  8891. strcmp((char *) chargingInfo[_index]->StartUserId, "") != EQUAL || _isTestRun)
  8892. {
  8893. _checkResult = START_TRANSATION_STATUS_PASS;
  8894. }
  8895. }
  8896. }
  8897. return _checkResult;
  8898. }
  8899. //LW_GBT_EVCCID
  8900. void SendStartTransactionReq(byte _index)
  8901. {
  8902. _isSendStartTransaction[_index] = true;
  8903. ocpp_set_startTransation_cmd(_index);
  8904. ocpp_set_startTransationPH_cmd(_index);
  8905. GetTimespecFunc(&_startTransation_time[_index]);
  8906. }
  8907. //LW_GBT_EVCCID
  8908. bool isStartTransactionResponsedFail(byte _index)
  8909. {
  8910. bool result = NO;
  8911. //(1)start transaction response was fail.
  8912. //(2)start transaction request was timeout.
  8913. if ((_isSendStartTransaction[_index] && ShmDcCommonData->startTransactionFlag[_index] == START_TRANSATION_STATUS_FAIL)
  8914. || (_isSendStartTransaction[_index] && ShmDcCommonData->startTransactionFlag[_index] == START_TRANSATION_STATUS_WAIT
  8915. && GetTimeoutValue(&_startTransation_time[_index]) > 45))
  8916. {
  8917. result = YES;
  8918. }
  8919. return result;
  8920. }
  8921. bool isCcidEnterToCharging(byte gun_index)
  8922. {
  8923. bool result = false;
  8924. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  8925. {
  8926. if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHCOMP)
  8927. {
  8928. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8929. {
  8930. if (chargingInfo [_index]->Type == _Type_CCS &&
  8931. chargingInfo [_index]->IsAvailable &&
  8932. ShmDcCommonData->_authWithCcidFlag [_index] == _CCID_CHECK)
  8933. {
  8934. if (_index != gun_index)
  8935. result = true;
  8936. break;
  8937. }
  8938. }
  8939. if (!result)
  8940. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  8941. }
  8942. }
  8943. return result;
  8944. }
  8945. bool IsNoneSelectAcModule()
  8946. {
  8947. bool result = false;
  8948. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0 ||
  8949. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  8950. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE))
  8951. {
  8952. result = true;
  8953. }
  8954. return result;
  8955. }
  8956. bool IsSelectAcModule()
  8957. {
  8958. bool result = false;
  8959. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  8960. return result;
  8961. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  8962. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  8963. {
  8964. result = true;
  8965. }
  8966. return result;
  8967. }
  8968. void DisplayPageChkByAcModule()
  8969. {
  8970. if (IsSelectAcModule())
  8971. {
  8972. if (ac_chargingInfo [0]->ConnectorPlugIn == AC_SYS_A)
  8973. {
  8974. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8975. }
  8976. else
  8977. {
  8978. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8979. }
  8980. }
  8981. }
  8982. void GetSystemDate()
  8983. {
  8984. time_t timep;
  8985. struct tm *p;
  8986. time ( & timep );
  8987. p = gmtime ( & timep );
  8988. sprintf ( (char *) ShmDcCommonData->_sys_date, "%d/%.2d/%.2d",
  8989. (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday );
  8990. if (p->tm_hour >= 12)
  8991. {
  8992. sprintf ( (char *) ShmDcCommonData->_sys_period, "p.m" );
  8993. sprintf ( (char *) ShmDcCommonData->_sys_time, "%.2d:%.2d", p->tm_hour - 12, p->tm_min);
  8994. }
  8995. else
  8996. {
  8997. sprintf ( (char *) ShmDcCommonData->_sys_period, "a.m" );
  8998. sprintf ( (char *) ShmDcCommonData->_sys_time, "%.2d:%.2d", p->tm_hour, p->tm_min);
  8999. }
  9000. }
  9001. void UpdateDcMeterPowerConsumption(byte _index)
  9002. {
  9003. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'P')
  9004. {
  9005. if (chargingInfo[_index]->PowerConsumption != ShmSysConfigAndInfo->SysInfo.DcMeterInfo[_index].totlizeImportEnergy)
  9006. chargingInfo[_index]->PowerConsumption = ShmSysConfigAndInfo->SysInfo.DcMeterInfo[_index].totlizeImportEnergy;
  9007. }
  9008. else if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
  9009. ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L')
  9010. {
  9011. if(chargingInfo[_index]->PowerConsumption != ShmCsuMeterData->_meter[_index]._curTotalCharging / 100)
  9012. chargingInfo[_index]->PowerConsumption = ShmCsuMeterData->_meter[_index]._curTotalCharging / 100;
  9013. }
  9014. }
  9015. int PresentChargedAcEnergyClear(unsigned char gun_index)
  9016. {
  9017. int result = FAIL;
  9018. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  9019. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod)*sizeof(float));
  9020. result = PASS;
  9021. return result;
  9022. }
  9023. void CheckTpcTimeout()
  9024. {
  9025. int _tpcTimebuf = 0;
  9026. _tpcTimebuf = GetTimeoutValue(&_tcpDetectStatus_time);
  9027. if (_tpcTimebuf < 0)
  9028. GetTimespecFunc(&_tcpDetectStatus_time);
  9029. else
  9030. {
  9031. if (_tpcTimebuf >= TPC_PLUG_DETECT_TIMEOUT)
  9032. {
  9033. _DetectTpcPlugInTimeout();
  9034. }
  9035. }
  9036. }
  9037. int ElapsedTime(uint8_t *start)
  9038. {
  9039. int time = 0;
  9040. struct ParsingResult
  9041. {
  9042. int result;
  9043. int scanedElement;
  9044. int year;
  9045. int month;
  9046. int mday;
  9047. int hour;
  9048. int min;
  9049. int sec;
  9050. int tz_hour;
  9051. int tz_min;
  9052. float minSec;
  9053. }parsingResult;
  9054. struct tm tmStart;
  9055. struct timeb tbStart;
  9056. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  9057. if(strstr((char*)start, ".") != NULL)
  9058. {
  9059. // Original data with mini second
  9060. if(strstr((char*)start, "Z") != NULL)
  9061. {
  9062. // Original data with Z
  9063. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  9064. &parsingResult.year, &parsingResult.month, &parsingResult.mday,
  9065. &parsingResult.hour, &parsingResult.min, &parsingResult.sec, &parsingResult.minSec);
  9066. }
  9067. else
  9068. {
  9069. // Original data without Z
  9070. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  9071. &parsingResult.year, &parsingResult.month, &parsingResult.mday, &parsingResult.hour,
  9072. &parsingResult.min, &parsingResult.sec, &parsingResult.minSec, &parsingResult.tz_hour,
  9073. &parsingResult.tz_min);
  9074. }
  9075. }
  9076. else
  9077. {
  9078. // Original data without mini second
  9079. if(strstr((char*)start, "Z") != NULL)
  9080. {
  9081. // Original data with Z
  9082. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  9083. &parsingResult.year, &parsingResult.month, &parsingResult.mday,
  9084. &parsingResult.hour, &parsingResult.min, &parsingResult.sec);
  9085. }
  9086. else
  9087. {
  9088. // Original data without Z
  9089. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  9090. &parsingResult.year, &parsingResult.month, &parsingResult.mday, &parsingResult.hour,
  9091. &parsingResult.min, &parsingResult.sec, &parsingResult.tz_hour, &parsingResult.tz_min);
  9092. }
  9093. }
  9094. if(parsingResult.scanedElement >= 6)
  9095. {
  9096. tmStart.tm_year = parsingResult.year - 1900;
  9097. tmStart.tm_mon = parsingResult.month - 1;
  9098. tmStart.tm_mday = parsingResult.mday;
  9099. tmStart.tm_hour = parsingResult.hour;
  9100. tmStart.tm_min = parsingResult.min;
  9101. tmStart.tm_sec = parsingResult.sec;
  9102. tmStart.tm_gmtoff = 0;
  9103. tbStart.time = mktime(&tmStart);
  9104. tbStart.timezone = 0;
  9105. tbStart.millitm = 0;
  9106. tbStart.time -= (parsingResult.tz_hour * 3600) + (parsingResult.tz_min * 60 * (parsingResult.tz_hour >= 0 ? 1 : -1));
  9107. if(DiffTimebWithNowSec(tbStart) <= 0)
  9108. time = 0;
  9109. else
  9110. time = DiffTimebWithNowSec(tbStart);
  9111. }
  9112. else
  9113. {
  9114. PRINTF_FUNC("OCMF - Start date parsing error.\n");
  9115. }
  9116. return time;
  9117. }
  9118. void SaveTheParentId(byte gun_index)
  9119. {
  9120. // Save the parentID
  9121. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  9122. {
  9123. memcpy(chargingInfo[gun_index]->ParentIdTag,
  9124. ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag,
  9125. ARRAY_SIZE(ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag));
  9126. }
  9127. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  9128. {
  9129. }
  9130. }
  9131. bool CheckTheParentId(byte gun_index)
  9132. {
  9133. bool result = false;
  9134. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  9135. {
  9136. if (strcmp((char *)chargingInfo[gun_index]->ParentIdTag, "") != EQUAL)
  9137. {
  9138. PRINTF_FUNC("IdTag = %s\n", ShmOCPP16Data->Authorize.IdTag);
  9139. PRINTF_FUNC("ParentIdTag = %s \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
  9140. if (strcmp((char *)chargingInfo[gun_index]->ParentIdTag, (char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag) == EQUAL &&
  9141. strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag) > 0)
  9142. {
  9143. memcpy(chargingInfo[gun_index]->StartUserId, ShmOCPP16Data->Authorize.IdTag,
  9144. sizeof(ShmOCPP16Data->Authorize.IdTag));
  9145. result = true;
  9146. }
  9147. }
  9148. }
  9149. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  9150. {
  9151. }
  9152. return result;
  9153. }
  9154. int main(void)
  9155. {
  9156. if(CreateShareMemory() == 0)
  9157. {
  9158. #ifdef SystemLogMessage
  9159. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  9160. #endif
  9161. if(ShmStatusCodeData!=NULL)
  9162. {
  9163. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  9164. }
  9165. sleep(5);
  9166. system("reboot -f");
  9167. sleep(5);
  9168. system("reboot -f");
  9169. }
  9170. PRINTF_FUNC("===== Boot and Initialization start ===== \n");
  9171. if (!InitialSystemDefaultConfig())
  9172. {
  9173. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  9174. StopProcessingLoop();
  9175. }
  9176. PRINTF_FUNC("CheckConnectorTypeStatus and CheckIsAlternatvie \n");
  9177. CheckGunTypeFromHw();
  9178. CheckIsAlternatvieByModelName();
  9179. InitialShareMemoryInfo();
  9180. PRINTF_FUNC("InitialShareMemoryInfo OK. \n");
  9181. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  9182. PRINTF_FUNC("ChargerType (IEC or UL) = IEC \n");
  9183. else
  9184. PRINTF_FUNC("ChargerType (IEC or UL) = UL \n");
  9185. ChangeLcmByIndex(_LCM_INIT);
  9186. if (!CheckConnectorTypeStatus() && !DEBUG_ALSTON)
  9187. isModelNameMatch = false;
  9188. Initialization();
  9189. PRINTF_FUNC("===== Initial Complete ===== \n");
  9190. PRINTF_FUNC("===== Spawn all Task and self test start ===== \n");
  9191. SpawnTask();
  9192. if (!isModelNameMatch)
  9193. {
  9194. // Module Name 與硬體對應不正確
  9195. PRINTF_FUNC("*** Module Name & HW info none match : Please check if the Model name and HW are matched. ***\n");
  9196. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  9197. ChangeLcmByIndex(_LCM_FIX);
  9198. sleep(3);
  9199. KillTask();
  9200. StopProcessingLoop();
  9201. }
  9202. PRINTF_FUNC("Module Name & HW info correct. \n");
  9203. CreateTimeoutFork();
  9204. if (!DEBUG_ALSTON)
  9205. {
  9206. PRINTF_FUNC("Self test run..\n");
  9207. SelfTestRun();
  9208. }
  9209. else
  9210. {
  9211. sleep(3);
  9212. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  9213. }
  9214. StopSystemTimeoutDet();
  9215. if (!DEBUG_ALSTON &&
  9216. (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  9217. ShmPsuData->Work_Step == _NO_WORKING ||
  9218. ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES ||
  9219. strcmp((char *)ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ") == EQUAL))
  9220. {
  9221. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  9222. {
  9223. if (!DisplaySelfTestFailReason() &&
  9224. ShmDcCommonData->rebootCount < 1)
  9225. {
  9226. PRINTF_FUNC("=======Soft reboot for retry self-test======= \n");
  9227. SetupRebootCount(1);
  9228. sleep(3);
  9229. KillAllTask();
  9230. system("/usr/bin/run_evse_restart.sh");
  9231. }
  9232. else if (ShmDcCommonData->rebootCount < 1)
  9233. {
  9234. PRINTF_FUNC("=======Soft reboot for retry self-test (Normal)======= \n");
  9235. SetupRebootCount(1);
  9236. sleep(3);
  9237. KillAllTask();
  9238. system("/usr/bin/run_evse_restart.sh");
  9239. }
  9240. else
  9241. SetupRebootCount(0);
  9242. }
  9243. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  9244. {
  9245. ChargingAlarmProcess(gun_index);
  9246. }
  9247. sleep(3);
  9248. KillTaskExceptDetectTask();
  9249. StopProcessingLoop();
  9250. }
  9251. else
  9252. {
  9253. SetupRebootCount(0);
  9254. }
  9255. PRINTF_FUNC("===== Self Test Complete ===== \n");
  9256. PRINTF_FUNC("===== Finally Create DB ===== \n");
  9257. // Local DB
  9258. if(DB_Open(localDb) != PASS)
  9259. {
  9260. PRINTF_FUNC("DB_Open fail. \n");
  9261. isDb_ready = false;
  9262. }
  9263. else
  9264. {
  9265. isDb_ready = true;
  9266. for (int _acIndex = 0; _acIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _acIndex++)
  9267. {
  9268. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  9269. ac_chargingInfo[0]->IsAvailable = DB_Get_Operactive(localDb, _acIndex + hasDc);
  9270. }
  9271. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  9272. {
  9273. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  9274. chargingInfo[_index]->PowerConsumption = DB_Get_PowerConsumption(localDb, _index);
  9275. }
  9276. DB_Reboot_Record(localDb);
  9277. }
  9278. //lwmark
  9279. for (int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  9280. {
  9281. if (chargingInfo [_index]->IsAvailable == NO)
  9282. SetChargerMode ( _index, SYS_MODE_MAINTAIN );
  9283. else
  9284. SetChargerMode ( _index, SYS_MODE_IDLE );
  9285. }
  9286. if (NetworkDB_Open(networkDb) != PASS)
  9287. {
  9288. PRINTF_FUNC("NetworkDB_Open fail. \n");
  9289. }
  9290. ChangeLcmByIndex(_LCM_IDLE);
  9291. sleep(1);
  9292. CreateRfidFork();
  9293. sleep(1);
  9294. if (!DEBUG_ALSTON)
  9295. {
  9296. PRINTF_FUNC("===== Create chk system Task ===== \n");
  9297. CreateCheckSystemTaskFork();
  9298. }
  9299. PRINTF_FUNC("===== Create DB End ===== \n");
  9300. if (CheckTheSystemType())
  9301. {
  9302. PRINTF_FUNC("Can't confirm the system type. (reboot) \n");
  9303. sleep(3);
  9304. KillAllTask();
  9305. system("/usr/bin/run_evse_restart.sh");
  9306. }
  9307. PRINTF_FUNC("===== Charger info ===== ");
  9308. PRINTF_FUNC("SW Version = %s", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  9309. PRINTF_FUNC("Bootloader = %s", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  9310. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  9311. InfoSystemType();
  9312. InfoLcmVersion();
  9313. InfoFwSlotStatusLog();
  9314. AdjustChargerCurrent();
  9315. PRINTF_FUNC("===== Charger info End ===== ");
  9316. GetTimespecFunc(&_cmdMainPriority_time);
  9317. _ocppProfileChkFlag_1 = 0;
  9318. _ocppProfileChkFlag_2 = 0;
  9319. KillOcppTask();
  9320. int _mainTimebuf = 0;
  9321. for (;;)
  9322. {
  9323. if (ShmDcCommonData->_isAutoRunTest != _isTestRun)
  9324. {
  9325. if (ShmDcCommonData->_isAutoRunTest == YES)
  9326. {
  9327. GetTimespecFunc(&_autoRuntestTime);
  9328. }
  9329. _isTestRun = ShmDcCommonData->_isAutoRunTest;
  9330. }
  9331. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9332. {
  9333. ocpp_ac_chk_availability_cmd();
  9334. ocpp_ac_chk_unlock_cmd();
  9335. }
  9336. ocpp_auto_response_BootNotification();
  9337. ocpp_chk_reset_cmd(NO);
  9338. ButtonTriggerEvent();
  9339. if (!DEBUG_ALSTON)
  9340. ChkPrimaryStatus();
  9341. if ((IsConnectorWholeIdle() ||
  9342. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX))
  9343. {
  9344. CheckFactoryConfigFunction();
  9345. CheckFwUpdateFunction();
  9346. }
  9347. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  9348. {
  9349. sleep(1);
  9350. continue;
  9351. }
  9352. // OCPP 邏輯
  9353. ocpp_chk_remoteStart_cmd();
  9354. // 讀卡邏輯
  9355. ScannerCardProcess();
  9356. // 當 AC 沒有搭上時,清除一些錯誤碼
  9357. ClearAlarmCodeWhenAcOff();
  9358. // 確認是否要回到充電中的槍畫面邏輯判斷
  9359. CheckReturnToChargingConn();
  9360. if ((_acgunIndex > 0 && isDetectPlugin() && !isCardScan))
  9361. {
  9362. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  9363. }
  9364. _mainTimebuf = GetTimeoutValue(&_cmdMainPriority_time);
  9365. if (ShmDcCommonData->_tpcPlugDetectState == _TPC_PLUG_DETECT_STATUS_WAIT)
  9366. CheckTpcTimeout();
  9367. if (_mainTimebuf < 0)
  9368. GetTimespecFunc(&_cmdMainPriority_time);
  9369. if (_mainTimebuf >= 5)
  9370. {
  9371. if (ShmPsuData->PsuStopChargeFlag)
  9372. Check267Alarm();
  9373. CheckTask();
  9374. Run4gResetProc();
  9375. GetSystemDate();
  9376. bool isBothCharging = true;
  9377. byte chargingCount = 0;
  9378. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9379. {
  9380. if (_verifyMechanism == VERIFY_MECHANISM_BAZEL8)
  9381. {
  9382. if ((chargingInfo[_index]->SystemStatus == SYS_MODE_IDLE ||
  9383. chargingInfo[_index]->SystemStatus == SYS_MODE_RESERVATION ||
  9384. chargingInfo[_index]->SystemStatus == SYS_MODE_MAINTAIN) &&
  9385. chargingInfo[_index]->ConnectorPlugIn)
  9386. {
  9387. CheckPaymentID();
  9388. }
  9389. }
  9390. UpdateDcMeterPowerConsumption(_index);
  9391. if ((chargingInfo[_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  9392. (chargingInfo[_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  9393. {
  9394. ocpp_chargingProfile_process(_index);
  9395. if (chargingInfo[_index]->SystemStatus == SYS_MODE_CHARGING)
  9396. {
  9397. if (_index == 0 && _ocppProfileChkFlag_1 >= 12)
  9398. {
  9399. ocpp_lift_profileReq_cmd ( _index );
  9400. _ocppProfileChkFlag_1 = 0;
  9401. }
  9402. else if (_index == 1 && _ocppProfileChkFlag_2 >= 12)
  9403. {
  9404. ocpp_lift_profileReq_cmd ( _index );
  9405. _ocppProfileChkFlag_2 = 0;
  9406. }
  9407. }
  9408. else if (chargingInfo[_index]->SystemStatus != SYS_MODE_CHARGING)
  9409. {
  9410. if (_index == 0)
  9411. {
  9412. ocpp_lift_profileReq_cmd ( _index );
  9413. _ocppProfileChkFlag_1 = 0;
  9414. }
  9415. else if (_index == 1)
  9416. {
  9417. ocpp_lift_profileReq_cmd ( _index );
  9418. _ocppProfileChkFlag_2 = 0;
  9419. }
  9420. }
  9421. else
  9422. {
  9423. if (_index == 0)
  9424. _ocppProfileChkFlag_1++;
  9425. else if (_index == 1)
  9426. _ocppProfileChkFlag_2++;
  9427. }
  9428. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  9429. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  9430. {
  9431. DB_Update_PowerConsumption(localDb, _index, chargingInfo[_index]->PowerConsumption);
  9432. }
  9433. chargingCount++;
  9434. }
  9435. // OTP
  9436. CheckConnectorOTP(_index);
  9437. if (ShmSmartBoxData->ConnectorStatus[_index].ConnectorStaus != _CONNECTOR_STATUS_USING)
  9438. isBothCharging = false;
  9439. // OCMF
  9440. ChkOcmfRecord(_index);
  9441. // Reserved
  9442. if (chargingInfo[_index]->ReservationId != NO_RESERVE)
  9443. chargingInfo[_index]->SystemStatus = SYS_MODE_RESERVATION;
  9444. if (chargingInfo[_index]->SystemStatus == SYS_MODE_RESERVATION)
  9445. {
  9446. if (opcc_chk_reserve_expired(_index))
  9447. {
  9448. chargingInfo[_index]->SystemStatus = SYS_MODE_IDLE;
  9449. }
  9450. }
  9451. }
  9452. _chargingCount = chargingCount;
  9453. if (isBothCharging)
  9454. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_BOTH;
  9455. else
  9456. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_NORMAL;
  9457. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  9458. {
  9459. if ((ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING) ||
  9460. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  9461. {
  9462. ocpp_ac_chargingProfile_process();
  9463. }
  9464. if ((ac_chargingInfo[_index]->SystemStatus >= SYS_MODE_PREPARING &&
  9465. ac_chargingInfo[_index]->SystemStatus <= SYS_MODE_CHARGING))
  9466. {
  9467. if (ac_chargingInfo[_index]->SystemStatus == SYS_MODE_CHARGING &&
  9468. _ac_ocppProfileChkFlag == 12)
  9469. {
  9470. ocpp_ac_lift_profileReq_cmd(_index);
  9471. _ac_ocppProfileChkFlag = 0;
  9472. }
  9473. else if (ac_chargingInfo[_index]->SystemStatus != SYS_MODE_CHARGING)
  9474. {
  9475. ocpp_ac_lift_profileReq_cmd(_index);
  9476. _ac_ocppProfileChkFlag = 0;
  9477. }
  9478. else
  9479. _ac_ocppProfileChkFlag++;
  9480. }
  9481. }
  9482. CheckOcppCostAndPrice();
  9483. CheckAuthrizeByEvccid();
  9484. GetTimespecFunc(&_cmdMainPriority_time);
  9485. CreateWatchdog();
  9486. }
  9487. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
  9488. ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L')
  9489. CheckStatusOfMeter();
  9490. // 確認當前錯誤 Level = 2 ?
  9491. ReviewCriticalAlarm();
  9492. errCollect.GunErrMessage = 0;
  9493. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9494. {
  9495. CheckAcOcppProfileConfirm();
  9496. ocpp_ac_chk_reserved_cmd();
  9497. }
  9498. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  9499. {
  9500. //CheckGpioInStatus();
  9501. // 確認系統的錯誤狀態
  9502. CheckSystemErrorFunction(gun_index);
  9503. // 確認 Relay Welding or Driving Fault
  9504. CheckRelayWeldingOrDrivingFault(gun_index);
  9505. // 收集各槍的錯誤狀態
  9506. CollectError(gun_index);
  9507. ocpp_chk_reserved_cmd(gun_index);
  9508. ocpp_chk_availability_cmd(gun_index);
  9509. ocpp_chk_unlock_cmd(gun_index);
  9510. ocpp_default_errorCode_cmd(gun_index);
  9511. // check charging profile confirm flag
  9512. CheckOcppProfileConfirm(gun_index);
  9513. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  9514. switch(chargingInfo[gun_index]->SystemStatus)
  9515. {
  9516. case SYS_MODE_IDLE:
  9517. case SYS_MODE_RESERVATION:
  9518. case SYS_MODE_MAINTAIN:
  9519. case SYS_MODE_FAULT:
  9520. {
  9521. if (isModeChange(gun_index))
  9522. {
  9523. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  9524. {
  9525. PRINTF_FUNC("================= SYS_MODE_IDLE (%x) ================ \n", gun_index);
  9526. chargingInfo[gun_index]->ReservationId = NO_RESERVE;
  9527. }
  9528. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  9529. {
  9530. PRINTF_FUNC("================= SYS_MODE_RESERVATION (%x) ================ \n", gun_index);
  9531. PRINTF_FUNC("================= Id : %s ================ \n", ShmDcCommonData->_reserved_UserId[gun_index]);
  9532. }
  9533. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_MAINTAIN)
  9534. {
  9535. PRINTF_FUNC("================= SYS_MODE_MAINTAIN (%x) ================ \n", gun_index);
  9536. }
  9537. chargingInfo[gun_index]->PresentChargedDuration = 0;
  9538. chargingInfo[gun_index]->RemainChargingDuration = 0;
  9539. chargingInfo[gun_index]->PresentChargingVoltage = 0;
  9540. chargingInfo[gun_index]->PresentChargingCurrent = 0;
  9541. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "00:00:00");
  9542. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "00:00:00");
  9543. memset((char *)chargingInfo[gun_index]->StartUserId, 0, sizeof(chargingInfo[gun_index]->StartUserId));
  9544. memset((char *)chargingInfo[gun_index]->EVCCID, 0, sizeof(chargingInfo[gun_index]->EVCCID));
  9545. _isSendStartTransaction[gun_index] = false;
  9546. ocpp_auto_response_StopTransationConf(gun_index);
  9547. ocpp_auto_response_StopTransationConfPH(gun_index);
  9548. ClearEnergyTimePeriod(gun_index);
  9549. ocpp_clear_stopReason(gun_index);
  9550. ocpp_clear_stopTansaction_Idtag(gun_index);//LWADD
  9551. ocpp_auto_response_StartTransationConf(gun_index);
  9552. ocpp_auto_response_StartTransationConfPH(gun_index);
  9553. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_WAIT;
  9554. ShmDcCommonData->_authWithCcidFlag[gun_index] = _CCID_NONE;
  9555. ShmDcCommonData->startTransactionID[gun_index] = 0;
  9556. ShmDcCommonData->_useVersion = _CDFA_VERSION_NONE;
  9557. ShmDcCommonData->cdfaInfor[gun_index]._ctepEnergyCost = 0;
  9558. ShmDcCommonData->cdfaInfor[gun_index]._ctepParkingFee = 0;
  9559. ShmDcCommonData->cdfaInfor[gun_index]._ctepTotalCost = 0;
  9560. ShmDcCommonData->cdfaInfor[gun_index]._prekWhPrice = 0;
  9561. ShmDcCommonData->cdfaInfor[gun_index]._prehourPrice = 0;
  9562. ShmDcCommonData->cdfaInfor[gun_index]._preEnergyCost = 0;
  9563. ShmDcCommonData->cdfaInfor[gun_index]._preParkingFee = 0;
  9564. ShmDcCommonData->remoteStartAuthFlag[gun_index] = NO;
  9565. memset((char *)ShmDcCommonData->cdfaInfor[gun_index]._preTimestamp, 0, sizeof(ShmDcCommonData->cdfaInfor[gun_index]._preTimestamp));
  9566. memset((char *)ShmDcCommonData->cdfaInfor[gun_index]._preQrCodeText, 0, sizeof(ShmDcCommonData->cdfaInfor[gun_index]._preQrCodeText));
  9567. _ChargingProfilePwBuffer[gun_index] = 0;
  9568. _ChargingMaxProfilePwBuffer[gun_index] = 0;
  9569. ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].ActionCmd = _DC_METER_TRANSACTION_ACTION_NONE;
  9570. PresentChargedAcEnergyClear(gun_index);
  9571. for(byte _vCount = 0; _vCount < VOLTAGE_SAMPLE_COUNT; _vCount++)
  9572. {
  9573. ShmDcCommonData->_outputVoltageSapmle[gun_index][_vCount] = 0;
  9574. }
  9575. }
  9576. if (chargingInfo[gun_index]->deratingByConnOtp.deratingIndex != 0)
  9577. chargingInfo[gun_index]->deratingByConnOtp.deratingIndex = 0;
  9578. //lwmark
  9579. if (chargingInfo [gun_index]->IsAvailable == NO &&
  9580. chargingInfo[gun_index]->SystemStatus != SYS_MODE_MAINTAIN)
  9581. {
  9582. SetChargerMode(gun_index, SYS_MODE_MAINTAIN);
  9583. }
  9584. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  9585. {
  9586. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  9587. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  9588. else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_IDLE &&
  9589. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_RESERVATION &&
  9590. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_MAINTAIN &&
  9591. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_FAULT)
  9592. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9593. ClearDetectPluginFlag();
  9594. ocpp_set_errCode_cmd(gun_index);
  9595. SetChargerMode(gun_index, SYS_MODE_FAULT);
  9596. }
  9597. else
  9598. {
  9599. if (PrecheckIsPass(gun_index))
  9600. {
  9601. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_FAULT)
  9602. {
  9603. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9604. SetChargerMode(gun_index, SYS_MODE_IDLE);
  9605. }
  9606. if (chargingInfo[gun_index]->ReservationId == NO_RESERVE)
  9607. CcidAuthorizationProcessing(gun_index);
  9608. //
  9609. {
  9610. // Idle 正常程序起點
  9611. // 判斷是否有啟用檢查插槍
  9612. if(isDetectPlugin())
  9613. {
  9614. // 卡號驗證成功後,等待充電槍插入充電車
  9615. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  9616. {
  9617. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  9618. chargingInfo[gun_index]->IsAvailable)
  9619. {
  9620. PRINTF_FUNC("Conn %d Start Charging : Remote \n", gun_index);
  9621. chargingInfo[gun_index]->RemoteStartFlag = NO;
  9622. chargingInfo[gun_index]->isRemoteStart = YES;
  9623. ChangeGunSelectByIndex(gun_index);
  9624. AddPlugInTimes(gun_index);
  9625. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  9626. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  9627. ClearDetectPluginFlag();
  9628. continue;
  9629. }
  9630. }
  9631. else if (chargingInfo[gun_index]->ReservedStartFlag == YES)
  9632. {
  9633. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  9634. chargingInfo[gun_index]->IsAvailable)
  9635. {
  9636. chargingInfo[gun_index]->ReservedStartFlag = NO;
  9637. ChangeGunSelectByIndex(gun_index);
  9638. AddPlugInTimes(gun_index);
  9639. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmDcCommonData->_reserved_UserId[gun_index]);
  9640. PRINTF_FUNC("Conn %d Start Charging : Reserved CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  9641. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  9642. strcpy((char *)ShmDcCommonData->_reserved_UserId[gun_index], "");
  9643. // 當前操作的槍號,進入 Preparing
  9644. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  9645. ClearDetectPluginFlag();
  9646. continue;
  9647. }
  9648. }
  9649. else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE)
  9650. {
  9651. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  9652. chargingInfo[gun_index]->IsAvailable &&
  9653. chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  9654. {
  9655. ChangeGunSelectByIndex(gun_index);
  9656. AddPlugInTimes(gun_index);
  9657. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  9658. SaveTheParentId(gun_index);
  9659. PRINTF_FUNC("Conn %d Start Charging : RFID CardNumber = %s, ParentId = %s \n",
  9660. gun_index, chargingInfo[gun_index]->StartUserId,
  9661. chargingInfo[gun_index]->ParentIdTag);
  9662. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  9663. // 當前操作的槍號,進入 Preparing
  9664. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  9665. ClearDetectPluginFlag();
  9666. continue;
  9667. }
  9668. }
  9669. if (!isCardScan &&
  9670. (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected ||
  9671. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)))
  9672. {
  9673. // LCM => Waiting for plugging
  9674. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  9675. }
  9676. }
  9677. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  9678. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  9679. {
  9680. PRINTF_FUNC("Conn %d Start Charging : Plug and charing \n", gun_index);
  9681. bool isCanStartChargingFlag = GetStartChargingByAlterMode(gun_index);
  9682. if (isCanStartChargingFlag)
  9683. {
  9684. ChangeGunSelectByIndex(gun_index);
  9685. AddPlugInTimes(gun_index);
  9686. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  9687. ClearDetectPluginFlag();
  9688. ocpp_get_freeVendor(gun_index);
  9689. continue;
  9690. }
  9691. }
  9692. else
  9693. {
  9694. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9695. {
  9696. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9697. ocpp_remote_start_wait_cmd()))
  9698. {
  9699. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  9700. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  9701. {
  9702. if (ac_chargingInfo[0]->ConnectorPlugIn == AC_SYS_A)
  9703. {
  9704. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  9705. }
  9706. else
  9707. {
  9708. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9709. }
  9710. }
  9711. else
  9712. {
  9713. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  9714. }
  9715. }
  9716. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  9717. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_WAIT_FOR_PLUG)
  9718. {
  9719. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9720. }
  9721. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  9722. }
  9723. }
  9724. } // Idle 正常程序終點
  9725. }
  9726. else
  9727. {
  9728. SetChargerMode(gun_index, SYS_MODE_FAULT);
  9729. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  9730. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  9731. }
  9732. ReleaseSysAlarmCode(gun_index);
  9733. }
  9734. }
  9735. break;
  9736. case SYS_MODE_MODE_REASSIGN_CHECK:
  9737. {
  9738. if (isModeChange(gun_index))
  9739. {
  9740. PRINTF_FUNC("================= SYS_MODE_MODE_REASSIGN_CHECK (%x) ================ \n", gun_index);
  9741. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9742. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  9743. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  9744. StopSystemTimeoutDet();
  9745. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE;
  9746. chargingInfo[gun_index]->ReservationId = NO_RESERVE;
  9747. ocpp_sub_set_remote_start_transaction_req(gun_index, NO);
  9748. }
  9749. ocpp_clear_errorCode_cmd(gun_index);
  9750. if (ShmSmartBoxData->ConnectorStatus[gun_index].ConnectorStaus == _CONNECTOR_STATUS_WAIT)
  9751. {
  9752. PRINTF_FUNC("Conn_%d wait for Psu Module. \n", gun_index);
  9753. SetChargerMode(gun_index, SYS_MODE_REASSIGN);
  9754. }
  9755. else if (ShmSmartBoxData->ConnectorStatus[gun_index].ConnectorStaus == _CONNECTOR_STATUS_USING)
  9756. {
  9757. PRINTF_FUNC("Conn_%d start charging with Psu Module. \n", gun_index);
  9758. SetChargerMode(gun_index, SYS_MODE_PREPARING);
  9759. }
  9760. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9761. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9762. {
  9763. if (IsNoneSelectAcModule())
  9764. {
  9765. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9766. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9767. {
  9768. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9769. }
  9770. }
  9771. else
  9772. {
  9773. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9774. ocpp_remote_start_wait_cmd ()))
  9775. {
  9776. DisplayPageChkByAcModule();
  9777. }
  9778. }
  9779. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9780. }
  9781. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9782. {
  9783. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9784. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9785. }
  9786. }
  9787. break;
  9788. case SYS_MODE_REASSIGN:
  9789. {
  9790. if (isModeChange(gun_index))
  9791. {
  9792. PRINTF_FUNC("================== SYS_MODE_REASSIGN (%x) ================ \n", gun_index);
  9793. }
  9794. ocpp_clear_errorCode_cmd(gun_index);
  9795. if (ShmSmartBoxData->ConnectorStatus[gun_index].ConnectorStaus == _CONNECTOR_STATUS_USING)
  9796. SetChargerMode(gun_index, SYS_MODE_PREPARING);
  9797. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9798. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9799. {
  9800. if (IsNoneSelectAcModule())
  9801. {
  9802. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9803. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9804. {
  9805. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9806. }
  9807. }
  9808. else
  9809. {
  9810. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9811. ocpp_remote_start_wait_cmd ()))
  9812. {
  9813. DisplayPageChkByAcModule();
  9814. }
  9815. }
  9816. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9817. }
  9818. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9819. {
  9820. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9821. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9822. }
  9823. }
  9824. break;
  9825. case SYS_MODE_PREPARING:
  9826. {
  9827. if (isModeChange(gun_index))
  9828. {
  9829. PRINTF_FUNC("=================== SYS_MODE_PREPARING (%x) =============== \n", gun_index);
  9830. StopGunInfoTimeoutDet(gun_index);
  9831. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  9832. }
  9833. ocpp_clear_errorCode_cmd(gun_index);
  9834. if (chargingInfo[gun_index]->AvailableChargingPower > 0 &&
  9835. ShmPsuData->Work_Step == _WORK_CHARGING)
  9836. {
  9837. SetChargerMode(gun_index, SYS_MODE_PREPARE_FOR_EV);
  9838. }
  9839. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9840. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9841. {
  9842. if (IsNoneSelectAcModule())
  9843. {
  9844. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9845. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9846. {
  9847. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9848. }
  9849. }
  9850. else
  9851. {
  9852. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9853. ocpp_remote_start_wait_cmd ()))
  9854. {
  9855. DisplayPageChkByAcModule();
  9856. }
  9857. }
  9858. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9859. }
  9860. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9861. {
  9862. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9863. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9864. }
  9865. }
  9866. break;
  9867. case SYS_MODE_PREPARE_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  9868. {
  9869. if (isModeChange(gun_index))
  9870. {
  9871. PRINTF_FUNC("================== SYS_MODE_PREPARE_FOR_EV (%x) ================ \n", gun_index);
  9872. _presentChargingTimeBuf[gun_index] = 0;
  9873. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  9874. StopGunInfoTimeoutDet(gun_index);
  9875. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  9876. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_WAIT;
  9877. // Start Transation
  9878. PRINTF_FUNC("StartUserId = %s \n", chargingInfo[gun_index]->StartUserId);
  9879. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9880. strcmp((char *)chargingInfo[gun_index]->StartUserId, "") != EQUAL ||
  9881. chargingInfo[gun_index]->isRemoteStart == YES)
  9882. {
  9883. SendStartTransactionReq(gun_index);
  9884. }
  9885. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  9886. }
  9887. ocpp_clear_errorCode_cmd(gun_index);
  9888. //LW_GBT_EVCCID
  9889. if(chargingInfo [gun_index]->Type == _Type_CCS
  9890. && ShmDcCommonData->enObtainAuthorizbyCCID)
  9891. {
  9892. CheckCcidAuthorizationFunction(gun_index);
  9893. if (!_isSendStartTransaction[gun_index])
  9894. {
  9895. if (ShmDcCommonData->_authWithCcidFlag[gun_index] == _CCID_AUTHCOMP)
  9896. {
  9897. PRINTF_FUNC("_CCID_AUTHCOMP \n");
  9898. SendStartTransactionReq(gun_index);
  9899. }
  9900. else if (ShmDcCommonData->_authWithCcidFlag[gun_index] == _CCID_AUTHFAIL)
  9901. {
  9902. PRINTF_FUNC("_CCID_AUTHFAIL \n");
  9903. ChargingTerminalProcess(gun_index);
  9904. }
  9905. }
  9906. }
  9907. //LW_GBT_EVCCID
  9908. ShmDcCommonData->startTransactionFlag[gun_index] = TransactionResponseStatus(gun_index);
  9909. // Offline policy - No charging
  9910. ChkNoCharging(gun_index);
  9911. // 小板停止
  9912. ChkConnectorBoardStop(gun_index);
  9913. // psu 停止
  9914. ChkPsuErrorStop(gun_index);
  9915. if (ocpp_chk_remoteStop_cmd(gun_index) == YES ||
  9916. WifiScheduleStop(gun_index) || isStartTransactionResponsedFail(gun_index) == YES)//LW_GBT_EVCCID
  9917. {
  9918. // 後臺要求停止
  9919. ChargingTerminalProcess(gun_index);
  9920. if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_FAIL)
  9921. PRINTF_FUNC("StartTransaction req fail. (%d) \n", gun_index);
  9922. else if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_WAIT)
  9923. PRINTF_FUNC("StartTransaction req timeout. (%d) \n", gun_index);
  9924. }
  9925. // 檢查車端的槍鎖是否為鎖上
  9926. if (isEvGunLocked(gun_index) == YES)
  9927. {
  9928. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'P')
  9929. {
  9930. ConfigOcmfInfo(gun_index);
  9931. DB_Insert_ocmf(localDb, gun_index);
  9932. ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].ActionCmd = _DC_METER_TRANSACTION_ACTION_START;
  9933. }
  9934. SetChargerMode(gun_index, SYS_MODE_PREPARE_FOR_EVSE);
  9935. }
  9936. // LCM => Pre-charging
  9937. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9938. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9939. {
  9940. if (IsNoneSelectAcModule())
  9941. {
  9942. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  9943. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  9944. { }
  9945. else
  9946. {
  9947. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9948. }
  9949. }
  9950. else
  9951. {
  9952. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9953. ocpp_remote_start_wait_cmd ()))
  9954. {
  9955. DisplayPageChkByAcModule();
  9956. }
  9957. }
  9958. if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_NONE)
  9959. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9960. }
  9961. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9962. {
  9963. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  9964. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  9965. {}
  9966. else
  9967. {
  9968. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9969. }
  9970. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9971. }
  9972. }
  9973. break;
  9974. case SYS_MODE_PREPARE_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  9975. {
  9976. if (isModeChange(gun_index))
  9977. {
  9978. PRINTF_FUNC("================== SYS_MODE_PREPARE_FOR_EVSE (%x) ================ \n", gun_index);
  9979. StopGunInfoTimeoutDet(gun_index);
  9980. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  9981. }
  9982. //LW_GBT_EVCCID
  9983. //GBT在做完GFD後才會取得 Vin code.
  9984. if(chargingInfo [gun_index]->Type == _Type_GB
  9985. && ShmDcCommonData->enObtainAuthorizbyCCID)
  9986. {
  9987. CheckCcidAuthorizationFunction(gun_index);
  9988. //Not ready send the Start transaction req.
  9989. if (!_isSendStartTransaction[gun_index])
  9990. {
  9991. if (ShmDcCommonData->_authWithCcidFlag[gun_index] == _CCID_AUTHCOMP)
  9992. {
  9993. PRINTF_FUNC("_CCID_AUTHCOMP \n");
  9994. SendStartTransactionReq(gun_index);
  9995. }
  9996. else if (ShmDcCommonData->_authWithCcidFlag[gun_index] == _CCID_AUTHFAIL)
  9997. {
  9998. PRINTF_FUNC("_CCID_AUTHFAIL \n");
  9999. ChargingTerminalProcess(gun_index);
  10000. }
  10001. }
  10002. //Start transaction response was failed, need to stop charging.
  10003. if(isStartTransactionResponsedFail(gun_index) == YES)
  10004. ChargingTerminalProcess(gun_index);
  10005. ShmDcCommonData->startTransactionFlag[gun_index] = TransactionResponseStatus(gun_index);
  10006. }//LW_GBT_EVCCID
  10007. ocpp_clear_errorCode_cmd(gun_index);
  10008. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  10009. {
  10010. // 檢查樁端的 GFD 結果
  10011. if (isPrechargeStatus_chademo(gun_index) >= 6)
  10012. {
  10013. // 當前操作的槍號,進入 Charging
  10014. SetChargerMode(gun_index, SYS_MODE_CHARGING);
  10015. }
  10016. }
  10017. else if (chargingInfo[gun_index]->Type == _Type_GB)
  10018. {
  10019. // 檢查樁端的 GFD 結果
  10020. //if (isPrechargeStatus_gb(gun_index) >= 6)
  10021. //LW_GBT_EVCCID
  10022. if (isPrechargeStatus_gb(gun_index) >= 10 &&
  10023. ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_PASS)
  10024. {
  10025. SetChargerMode(gun_index, SYS_MODE_CHARGING);
  10026. }
  10027. }
  10028. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  10029. {
  10030. // 檢查樁端的 GFD 結果
  10031. if ((chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  10032. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING))
  10033. {
  10034. SetChargerMode(gun_index, SYS_MODE_CCS_PRECHARGE_STEP0);
  10035. }
  10036. }
  10037. RecordGfdResult(gun_index);
  10038. // Offline policy - No charging
  10039. ChkNoCharging ( gun_index );
  10040. // 小板停止
  10041. ChkConnectorBoardStop(gun_index);
  10042. // psu 停止
  10043. ChkPsuErrorStop(gun_index);
  10044. // 後台要求停止
  10045. ChkBackendStop(gun_index);
  10046. // LCM => Pre-charging
  10047. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  10048. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  10049. {
  10050. if (IsNoneSelectAcModule())
  10051. {
  10052. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  10053. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  10054. {
  10055. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10056. }
  10057. }
  10058. else
  10059. {
  10060. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  10061. ocpp_remote_start_wait_cmd ()))
  10062. {
  10063. DisplayPageChkByAcModule();
  10064. }
  10065. }
  10066. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  10067. }
  10068. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  10069. {
  10070. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  10071. //LW_GBT_EVCCID
  10072. if(ShmDcCommonData->_authWithCcidFlag [gun_index] != _CCID_CHECK)
  10073. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10074. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10075. }
  10076. }
  10077. break;
  10078. case SYS_MODE_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  10079. {
  10080. if (isModeChange(gun_index))
  10081. {
  10082. PRINTF_FUNC("================== SYS_MODE_CHARGING (%x) ================ \n", gun_index);
  10083. StopGunInfoTimeoutDet(gun_index);
  10084. ftime(&startChargingTime[gun_index]);
  10085. ocpp_clear_idTag_cmd(gun_index);
  10086. ChangeStartOrStopDateTime(YES, gun_index);
  10087. if (_isTestRun == YES)
  10088. {
  10089. GetTimespecFunc(&_autoRuntestTime);
  10090. }
  10091. chargingInfo[gun_index]->EvBatteryStartSoc = chargingInfo[gun_index]->EvBatterySoc;
  10092. }
  10093. ocpp_clear_errorCode_cmd(gun_index);
  10094. if (_isTestRun == YES)
  10095. {
  10096. if (GetTimeoutValue(&_autoRuntestTime) > 60)
  10097. {
  10098. ChargingTerminalProcess(gun_index);
  10099. }
  10100. }
  10101. SavePresentChargedDuration(gun_index);
  10102. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  10103. {
  10104. if (ShmDcCommonData->minOutputCur != 0 &&
  10105. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  10106. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  10107. {
  10108. // UCP
  10109. RecordAlarmCode(gun_index, "012320");
  10110. ChargingTerminalProcess(gun_index);
  10111. }
  10112. }
  10113. else if (chargingInfo[gun_index]->Type == _Type_GB)
  10114. {
  10115. if (ShmDcCommonData->minOutputCur != 0 &&
  10116. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  10117. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  10118. {
  10119. // UCP
  10120. RecordAlarmCode(gun_index, "012322");
  10121. ChargingTerminalProcess(gun_index);
  10122. }
  10123. }
  10124. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  10125. {
  10126. if (ShmDcCommonData->minOutputCur != 0 &&
  10127. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  10128. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  10129. {
  10130. // UCP
  10131. RecordAlarmCode(gun_index, "012321");
  10132. ChargingTerminalProcess(gun_index);
  10133. }
  10134. }
  10135. RecordGfdResult(gun_index);
  10136. // Offline policy - No charging
  10137. ChkNoCharging(gun_index);
  10138. // 小板停止
  10139. ChkConnectorBoardStop(gun_index);
  10140. // psu 停止
  10141. ChkPsuErrorStop(gun_index);
  10142. // 後台要求停止
  10143. ChkBackendStop(gun_index);
  10144. // 設定停止
  10145. ChkConfigurationStop(gun_index);
  10146. // LCM => Charging
  10147. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  10148. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  10149. {
  10150. if (IsNoneSelectAcModule() || !stopChargingChkByCard)
  10151. {
  10152. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  10153. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  10154. { }
  10155. else
  10156. {
  10157. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10158. }
  10159. }
  10160. else
  10161. {
  10162. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  10163. ocpp_remote_start_wait_cmd ()))
  10164. {
  10165. DisplayPageChkByAcModule();
  10166. }
  10167. }
  10168. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  10169. }
  10170. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  10171. {
  10172. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  10173. if (!stopChargingChkByCard)
  10174. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10175. }
  10176. }
  10177. break;
  10178. case SYS_MODE_ALARM:
  10179. case SYS_MODE_TERMINATING:
  10180. {
  10181. if (isModeChange(gun_index))
  10182. {
  10183. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip ||
  10184. (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen && ShmDcCommonData->_tiltSensorStep == _TILT_SENSOR_STEP_NONE))
  10185. ocpp_set_stopReason_by_cmd(gun_index, "EmergencyStop");
  10186. else
  10187. ocpp_set_stopReason_by_cmd(gun_index, "Local");
  10188. ocpp_copy_stopReason2maintainServer(gun_index);
  10189. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  10190. {
  10191. PRINTF_FUNC("================== SYS_MODE_ALARM (%x) ================ \n", gun_index);
  10192. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  10193. {
  10194. ocpp_auto_response_StartTransationConf(gun_index);
  10195. ocpp_auto_response_StartTransationConfPH(gun_index);
  10196. if (_isSendStartTransaction[gun_index])
  10197. {
  10198. ocpp_set_stopTransation_cmd(gun_index);
  10199. ocpp_set_stopTransationPH_cmd(gun_index);
  10200. }
  10201. }
  10202. TheEndCharging(gun_index);
  10203. }
  10204. else
  10205. {
  10206. PRINTF_FUNC("================== SYS_MODE_TERMINATING (%x) ================ \n", gun_index);
  10207. }
  10208. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = NO;
  10209. StopGunInfoTimeoutDet(gun_index);
  10210. }
  10211. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  10212. {
  10213. ocpp_set_errCode_cmd(gun_index);
  10214. }
  10215. if (chargingInfo[gun_index]->Type == _Type_Chademo) { }
  10216. else if (chargingInfo[gun_index]->Type == _Type_GB) { }
  10217. else if (chargingInfo[gun_index]->Type == _Type_CCS) { }
  10218. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  10219. {
  10220. if (chargingInfo [gun_index]->ConnectorPlugIn == NO)
  10221. {
  10222. if (ShmDcCommonData->_returnIdleHoldFlag [gun_index] == NO &&
  10223. GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 10)
  10224. {
  10225. ShmDcCommonData->_returnIdleHoldFlag [gun_index] = YES;
  10226. StopGunInfoTimeoutDet ( gun_index );
  10227. StartGunInfoTimeoutDet ( gun_index, Timeout_EvseCompleteDet );
  10228. }
  10229. else if (ShmDcCommonData->_returnIdleHoldFlag [gun_index] == YES &&
  10230. GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 2)
  10231. {
  10232. SetChargerMode ( gun_index, SYS_MODE_IDLE );
  10233. }
  10234. }
  10235. if (_isTestRun == YES &&
  10236. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  10237. {
  10238. SetChargerMode(gun_index, SYS_MODE_IDLE);
  10239. }
  10240. }
  10241. else
  10242. {
  10243. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  10244. {
  10245. if (isEvGunLocked(gun_index) == NO ||
  10246. isPrechargeStatus_chademo(gun_index) <= 0)
  10247. {
  10248. SetChargerMode(gun_index, SYS_MODE_COMPLETE);
  10249. }
  10250. }
  10251. else if (chargingInfo[gun_index]->Type == _Type_GB)
  10252. {
  10253. if (isEvGunLocked(gun_index) == NO ||
  10254. isPrechargeStatus_gb(gun_index) <= 0)
  10255. {
  10256. SetChargerMode(gun_index, SYS_MODE_COMPLETE);
  10257. }
  10258. }
  10259. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  10260. {
  10261. if (isEvGunLocked(gun_index) == NO &&
  10262. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  10263. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14))
  10264. {
  10265. SetChargerMode(gun_index, SYS_MODE_COMPLETE);
  10266. }
  10267. }
  10268. }
  10269. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM &&
  10270. ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].OcmfInfoReady)
  10271. {
  10272. ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].OcmfInfoReady = NO;
  10273. ocpp_ocmf_information(gun_index);
  10274. DB_Update_ocmf(localDb, gun_index);
  10275. }
  10276. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  10277. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  10278. {
  10279. if (IsNoneSelectAcModule())
  10280. {
  10281. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  10282. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  10283. {
  10284. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10285. }
  10286. }
  10287. else
  10288. {
  10289. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  10290. ocpp_remote_start_wait_cmd ()))
  10291. {
  10292. DisplayPageChkByAcModule();
  10293. }
  10294. }
  10295. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  10296. }
  10297. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  10298. {
  10299. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  10300. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10301. }
  10302. }
  10303. break;
  10304. case SYS_MODE_COMPLETE:
  10305. {
  10306. if (isModeChange(gun_index))
  10307. {
  10308. PRINTF_FUNC("================== SYS_MODE_COMPLETE (%x) ================ \n", gun_index);
  10309. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  10310. {
  10311. ocpp_set_errCode_cmd(gun_index);
  10312. ocpp_auto_response_StartTransationConf(gun_index);
  10313. ocpp_auto_response_StartTransationConfPH(gun_index);
  10314. if (_isSendStartTransaction[gun_index])
  10315. {
  10316. ocpp_set_stopTransation_cmd(gun_index);
  10317. ocpp_set_stopTransationPH_cmd(gun_index);
  10318. }
  10319. }
  10320. TheEndCharging(gun_index);
  10321. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = NO;
  10322. }
  10323. ocpp_clear_errorCode_cmd(gun_index);
  10324. if (chargingInfo[gun_index]->ConnectorPlugIn == NO)
  10325. {
  10326. if (ShmDcCommonData->_returnIdleHoldFlag[gun_index] == NO &&
  10327. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  10328. {
  10329. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = YES;
  10330. StopGunInfoTimeoutDet(gun_index);
  10331. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  10332. }
  10333. else if (ShmDcCommonData->_returnIdleHoldFlag[gun_index] == YES &&
  10334. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 2)
  10335. {
  10336. SetChargerMode(gun_index, SYS_MODE_IDLE);
  10337. }
  10338. }
  10339. if (_isTestRun == YES &&
  10340. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  10341. {
  10342. SetChargerMode(gun_index, SYS_MODE_IDLE);
  10343. }
  10344. if (ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction[gun_index].OcmfInfoReady)
  10345. {
  10346. ShmSysConfigAndInfo->SysInfo.DcMeterTransactionAction [gun_index].OcmfInfoReady = NO;
  10347. ocpp_ocmf_information(gun_index);
  10348. DB_Update_ocmf(localDb, gun_index);
  10349. }
  10350. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  10351. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  10352. {
  10353. if (IsNoneSelectAcModule())
  10354. {
  10355. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  10356. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  10357. {
  10358. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10359. }
  10360. }
  10361. else
  10362. {
  10363. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  10364. ocpp_remote_start_wait_cmd ()))
  10365. {
  10366. DisplayPageChkByAcModule();
  10367. }
  10368. }
  10369. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  10370. }
  10371. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  10372. {
  10373. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  10374. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10375. }
  10376. }
  10377. break;
  10378. case SYS_MODE_CCS_PRECHARGE_STEP0:
  10379. {
  10380. if (isModeChange(gun_index))
  10381. {
  10382. PRINTF_FUNC("================== SYS_MODE_CCS_PRECHARGE_STEP0 (%x) ================ \n", gun_index);
  10383. StopGunInfoTimeoutDet(gun_index);
  10384. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  10385. }
  10386. ocpp_clear_errorCode_cmd(gun_index);
  10387. RecordGfdResult(gun_index);
  10388. // Offline policy - No charging
  10389. ChkNoCharging(gun_index);
  10390. // 小板停止
  10391. ChkConnectorBoardStop(gun_index);
  10392. // psu 停止
  10393. ChkPsuErrorStop(gun_index);
  10394. // 後台要求停止
  10395. ChkBackendStop(gun_index);
  10396. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  10397. // 切換 D+ Relay to Precharge Relay
  10398. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  10399. {
  10400. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  10401. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  10402. {
  10403. if (isPrechargeStatus_ccs(gun_index) == 39)
  10404. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PreChargeResponse (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  10405. else if (isPrechargeStatus_ccs(gun_index) == 40)
  10406. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PowerDeliveryRequest start (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  10407. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  10408. }
  10409. }
  10410. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  10411. {
  10412. SetChargerMode(gun_index, SYS_MODE_CCS_PRECHARGE_STEP1);
  10413. }
  10414. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  10415. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  10416. {
  10417. if (IsNoneSelectAcModule())
  10418. {
  10419. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  10420. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  10421. {
  10422. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10423. }
  10424. }
  10425. else
  10426. {
  10427. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  10428. ocpp_remote_start_wait_cmd ()))
  10429. {
  10430. DisplayPageChkByAcModule();
  10431. }
  10432. }
  10433. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  10434. }
  10435. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  10436. {
  10437. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  10438. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10439. }
  10440. break;
  10441. }
  10442. case SYS_MODE_CCS_PRECHARGE_STEP1:
  10443. {
  10444. if (isModeChange(gun_index))
  10445. {
  10446. PRINTF_FUNC("================== SYS_MODE_CCS_PRECHARGE_STEP1 (%x) ================ \n", gun_index);
  10447. }
  10448. ocpp_clear_errorCode_cmd(gun_index);
  10449. RecordGfdResult(gun_index);
  10450. // Offline policy - No charging
  10451. ChkNoCharging(gun_index);
  10452. // 小板停止
  10453. ChkConnectorBoardStop(gun_index);
  10454. // psu 停止
  10455. ChkPsuErrorStop(gun_index);
  10456. // 後台要求停止
  10457. ChkBackendStop(gun_index);
  10458. // 等待小板通知進入充電
  10459. // 切換 D+ Relay to Precharge Relay
  10460. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  10461. {
  10462. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  10463. SetChargerMode(gun_index, SYS_MODE_CHARGING);
  10464. }
  10465. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  10466. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  10467. {
  10468. if (IsNoneSelectAcModule())
  10469. {
  10470. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  10471. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  10472. {
  10473. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10474. }
  10475. }
  10476. else
  10477. {
  10478. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  10479. ocpp_remote_start_wait_cmd ()))
  10480. {
  10481. DisplayPageChkByAcModule();
  10482. }
  10483. }
  10484. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  10485. }
  10486. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  10487. {
  10488. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  10489. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10490. }
  10491. break;
  10492. }
  10493. }
  10494. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE ||
  10495. chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION ||
  10496. chargingInfo[gun_index]->SystemStatus == SYS_MODE_MAINTAIN ||
  10497. chargingInfo[gun_index]->SystemStatus == SYS_MODE_FAULT)
  10498. {
  10499. ocpp_chk_notificationMessage_cmd(gun_index);
  10500. }
  10501. }
  10502. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  10503. {
  10504. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  10505. }
  10506. else
  10507. {
  10508. bool dcPageRun = false;
  10509. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  10510. {
  10511. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE)
  10512. ChangeLcmByIndex(_LCM_IDLE);
  10513. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_PREPARING)
  10514. ChangeLcmByIndex(_LCM_PRE_CHARGE);
  10515. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  10516. ChangeLcmByIndex(_LCM_CHARGING);
  10517. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_TERMINATING ||
  10518. ac_chargingInfo[0]->SystemStatus == SYS_MODE_COMPLETE)
  10519. ChangeLcmByIndex(_LCM_COMPLETE);
  10520. else
  10521. dcPageRun = true;
  10522. }
  10523. else
  10524. dcPageRun = true;
  10525. if (dcPageRun)
  10526. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  10527. }
  10528. bool isCharging = false;
  10529. SystemAlarmCheck();
  10530. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10531. {
  10532. AlarmCheck(chargingInfo[gun_index]->Type);
  10533. if (_isTestRun == YES)
  10534. {
  10535. if (IsGunUsing(gun_index))
  10536. {
  10537. isCharging = true;
  10538. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_COMPLETE ||
  10539. chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  10540. {
  10541. GetTimespecFunc(&_autoRuntestTime);
  10542. }
  10543. }
  10544. }
  10545. }
  10546. if (_isTestRun == YES)
  10547. {
  10548. if (!isCharging)
  10549. {
  10550. if (GetTimeoutValue(&_autoRuntestTime) >= 60)
  10551. {
  10552. DetectPluginStart();
  10553. GetTimespecFunc(&_autoRuntestTime);
  10554. }
  10555. }
  10556. else
  10557. {
  10558. ClearDetectPluginFlag();
  10559. }
  10560. }
  10561. write(wtdFd, "a", 1);
  10562. usleep(whileLoopTime);
  10563. }
  10564. return FAIL;
  10565. }