main.c 349 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793
  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_DEFINE 255
  59. #define DEFAULT_AC_INDEX 2
  60. #define PSU_MIN_CUR 100
  61. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  62. #define NETWORK_DB_FILE "/Storage/EventLog/Eventlog.db"
  63. #define GUN_OTP_VALUE 150
  64. #define GUN_OTP_RECOVERY 140
  65. #define UNDEFINED_TEMP 255
  66. #define uSEC_VAL 1000000
  67. #define SELFTEST_TIMEOUT 60
  68. #define AUTHORIZE_TIMEOUT 15
  69. #define AUTHORIZE_COMP_TIMEOUT 3
  70. #define AUTHORIZE_FAIL_TIMEOUT 3
  71. #define AUTHORIZE_STOP_TIMEOUT 30
  72. #define RETURN_TO_CHARGING_PAGE 30
  73. #define GUN_PREPARE_TIMEOUT 30
  74. #define GUN_EV_WAIT_TIMEOUT 120
  75. #define GUN_EVSE_WAIT_TIMEOUT 60
  76. #define GUN_COMP_WAIT_TIMEOUT 3
  77. #define GUN_PRECHARGING_TIMEOUT 60
  78. #define TPC_PLUG_DETECT_TIMEOUT 60 //LW_tpc plug detect
  79. #define TIMEOUT_SPEC_HANDSHAKING 180000
  80. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  81. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  82. int whileLoopTime = 10000; // 10 ms
  83. int wtdFd = -1;
  84. byte _authorizeIndex = NO_DEFINE;
  85. bool IsAuthorizingMode();
  86. void ClearAuthorizedFlag();
  87. bool isDetectPlugin();
  88. void ClearDetectPluginFlag();
  89. int mystrcmp(unsigned char *p1, unsigned char *p2);
  90. void CheckTask();
  91. long long DiffTimebWithNow(struct timeb ST);
  92. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  93. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  94. void ChargingTerminalProcess(byte gunIndex);
  95. void ChkPrimaryStatus();
  96. void StartSystemTimeoutDet(unsigned char flag);
  97. void StopSystemTimeoutDet();
  98. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  99. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  100. int StoreLogMsg_1(const char *fmt, ...);
  101. int GetTimeoutValue(struct timespec *startTime);
  102. void gpio_set_value(unsigned int gpio, unsigned int value);
  103. void PRINTF_FUNC(char *string, ...);
  104. void ChangeGunSelectByIndex(byte sel);
  105. void ChargingAlarmProcess(byte gunIndex);
  106. void CheckSystemErrorFunction(byte index);
  107. int opcc_chk_reserve_expired(byte gunIndex);
  108. void ocpp_process_start();
  109. void ocpp_auto_response_BootNotification();
  110. void ocpp_auto_response_StartTransationConf(byte gunIndex);
  111. void ocpp_auto_response_StopTransationConf(byte gunIndex);
  112. bool ocpp_chk_authrization_cmd();
  113. void ocpp_chk_reset_cmd(byte isforce);
  114. void ocpp_chk_reserved_cmd(byte gunIndex);
  115. void ocpp_chk_availability_cmd(byte gunIndex);
  116. void ocpp_chk_unlock_cmd(byte gunIndex);
  117. bool ocpp_chk_remoteStop_cmd(byte gunIndex);
  118. void ocpp_chk_remoteStart_cmd();
  119. void ocpp_chk_update_cmd();
  120. bool ocpp_chk_invalid_id_cmd(byte gunIndex);
  121. void ocpp_set_startTransation_cmd(byte gunIndex);
  122. void ocpp_set_stopTransation_cmd(byte gunIndex);
  123. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString);
  124. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason);
  125. void ocpp_set_authorizeReq_cmd(byte value);
  126. void ocpp_set_authorizeConf_cmd(byte value);
  127. void ocpp_set_errCode_cmd(byte gunIndex);
  128. void ocpp_clear_errorCode_cmd(byte gunIndex);
  129. void ocpp_clear_idTag_cmd(byte gun_index);
  130. void ocpp_clear_stopReason(byte gun_index);
  131. bool ocpp_get_authorize_conf();
  132. unsigned short _ocpp_get_connect_timeout();
  133. void ocpp_chargingProfile_process(byte gun_index);
  134. void ocpp_set_startTransationPH_cmd(byte gunIndex);
  135. void ocpp_auto_response_StartTransationConfPH(byte gunIndex);
  136. void ocpp_set_stopTransationPH_cmd(byte gunIndex);
  137. void ocpp_auto_response_StopTransationConfPH(byte gunIndex);
  138. void ocpp_copy_stopReason2maintainServer(byte gunIndex);
  139. void RecordAlarmCode(byte gunIndex, char *code);
  140. void ReleaseSysAlarmCode(byte gunIndex);
  141. void ReviewCriticalAlarm();
  142. int DB_Open(sqlite3 *db);
  143. int DB_Insert_Record(sqlite3 *db, int gun_index);
  144. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  145. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  146. void InitialDHCP();
  147. int GetStartScheduleTime(unsigned char *time);
  148. void KillAllTask();
  149. void CheckSystemTaskAlive();
  150. void CheckConnectorOTP(byte _index);
  151. void _DetectTpcPlugInTimeout();
  152. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  153. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  154. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  155. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  156. struct StatusCodeData *ShmStatusCodeData;
  157. struct PsuData *ShmPsuData;
  158. struct CHAdeMOData *ShmCHAdeMOData;
  159. struct GBTData *ShmGBTData;
  160. struct CcsData *ShmCcsData;
  161. struct PrimaryMcuData *ShmPrimaryMcuData;
  162. struct FanModuleData *ShmFanModuleData;
  163. struct RelayModuleData *ShmRelayModuleData;
  164. struct LedModuleData *ShmLedModuleData;
  165. struct OCPP16Data *ShmOCPP16Data;
  166. struct OCPP20Data *ShmOCPP20Data;
  167. struct OCPP16Data *ShmOCPP16DataPH;
  168. struct MeterInformation *ShmCsuMeterData;
  169. struct DcCommonInformation *ShmDcCommonData;
  170. struct SmartBoxData *ShmSmartBoxData;
  171. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  172. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  173. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  174. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  175. int _presentChargingTimeBuf[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  176. struct timespec _startTransation_time[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  177. float _ChargingProfilePwBuffer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  178. float _AcChargingProfileCurBuffer;
  179. bool _isSendStartTransaction[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  180. GunErr errCollect;
  181. struct timespec _autoRuntestTime;
  182. byte _isTestRun = NO;
  183. // for initial index to check EV board type is correct
  184. byte _gunIndex = 0;
  185. byte _acgunIndex = 0;
  186. byte _chademoIndex = 0;
  187. byte _ccsIndex = 0;
  188. byte _gb_Index = 0;
  189. byte _ac_Index = 0;
  190. byte bd0_1_status = 0;
  191. byte bd0_2_status = 0;
  192. byte bd1_1_status = 0;
  193. byte bd1_2_status = 0;
  194. bool isCardScan = false;
  195. bool isModelNameMatch = true;
  196. int rfidFd = -1;
  197. char* rfidPortName = "/dev/ttyS2";
  198. char* fwVersion = "V3.01.00.0000.00";
  199. sqlite3 *localDb;
  200. bool isDb_ready;
  201. sqlite3 *networkDb;
  202. char MyDefaultPriceString[128];
  203. byte reset4gStep = RESET_4G_STEP_NONE;
  204. // 避免槍溫偵測誤判
  205. byte gunTempAllowCount[2] = {0, 0};
  206. //================================================
  207. // initial can-bus
  208. //================================================
  209. int InitCanBus()
  210. {
  211. int s0,nbytes;
  212. struct timeval tv;
  213. struct ifreq ifr0;
  214. struct sockaddr_can addr0;
  215. system("/sbin/ip link set can0 down");
  216. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  217. system("/sbin/ip link set can0 up");
  218. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  219. tv.tv_sec = 0;
  220. tv.tv_usec = 10000;
  221. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  222. {
  223. #ifdef SystemLogMessage
  224. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  225. #endif
  226. }
  227. nbytes=40960;
  228. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  229. {
  230. #ifdef SystemLogMessage
  231. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  232. #endif
  233. }
  234. nbytes=40960;
  235. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  236. {
  237. #ifdef SystemLogMessage
  238. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  239. #endif
  240. }
  241. strcpy(ifr0.ifr_name, "can0" );
  242. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  243. addr0.can_family = AF_CAN;
  244. addr0.can_ifindex = ifr0.ifr_ifindex;
  245. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  246. return s0;
  247. }
  248. //================================================
  249. // initial uart port
  250. //================================================
  251. char *_priPortName = "/dev/ttyS1";
  252. char *_485PortName = "/dev/ttyS5";
  253. int InitComPort(byte target)
  254. {
  255. int fd;
  256. struct termios tios;
  257. if(target == UPGRADE_PRI)
  258. fd = open(_priPortName, O_RDWR);
  259. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  260. fd = open(_485PortName, O_RDWR);
  261. if(fd<=0)
  262. {
  263. #ifdef SystemLogMessage
  264. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  265. #endif
  266. return -1;
  267. }
  268. ioctl (fd, TCGETS, &tios);
  269. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  270. tios.c_lflag = 0;
  271. tios.c_iflag = 0;
  272. tios.c_oflag = 0;
  273. tios.c_cc[VMIN]=0;
  274. tios.c_cc[VTIME]=(unsigned char)5;
  275. tios.c_lflag=0;
  276. tcflush(fd, TCIFLUSH);
  277. ioctl (fd, TCSETS, &tios);
  278. return fd;
  279. }
  280. //=================================
  281. // Common routine
  282. //=================================
  283. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  284. {
  285. strncpy(dest, src + start, cnt);
  286. dest[cnt] = 0;
  287. }
  288. int InitWatchDog()
  289. {
  290. int fd;
  291. int timeout = 180;
  292. system("/usr/bin/fuser -k /dev/watchdog");
  293. sleep(1);
  294. system("echo V > /dev/watchdog");
  295. sleep(1);
  296. fd = open("/dev/watchdog", O_RDWR);
  297. if(fd <= 0)
  298. {
  299. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  300. }
  301. ioctl(fd, _IOWR('W', 6, int), &timeout);
  302. return fd;
  303. }
  304. void CloseWatchDog()
  305. {
  306. if (wtdFd > 0)
  307. {
  308. close(wtdFd);
  309. sleep(1);
  310. system("/usr/bin/fuser -k /dev/watchdog");
  311. sleep(1);
  312. system("echo V > /dev/watchdog");
  313. sleep(1);
  314. }
  315. }
  316. int StoreLogMsg_1(const char *fmt, ...)
  317. {
  318. char Buf[4096+256];
  319. char buffer[4096];
  320. va_list args;
  321. struct timeb SeqEndTime;
  322. struct tm *tm;
  323. va_start(args, fmt);
  324. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  325. va_end(args);
  326. memset(Buf,0,sizeof(Buf));
  327. ftime(&SeqEndTime);
  328. SeqEndTime.time = time(NULL);
  329. tm=localtime(&SeqEndTime.time);
  330. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES ||
  331. DEBUG_ALSTON)
  332. {
  333. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  334. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  335. printf("%s \n", Buf);
  336. }
  337. else
  338. {
  339. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog_%s_Log",
  340. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  341. buffer,
  342. tm->tm_year+1900,tm->tm_mon+1,
  343. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  344. system(Buf);
  345. }
  346. return rc;
  347. }
  348. //unsigned long GetTimeoutValue(struct timeval _sour_time)
  349. //{
  350. // struct timeval _end_time;
  351. // gettimeofday(&_end_time, NULL);
  352. //
  353. // return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  354. //}
  355. int GetTimeoutValue(struct timespec *startTime)
  356. {
  357. struct timespec endTime;
  358. clock_gettime(CLOCK_MONOTONIC_COARSE, &endTime);
  359. return endTime.tv_sec - startTime->tv_sec;
  360. }
  361. void GetTimespecFunc(struct timespec *time)
  362. {
  363. clock_gettime(CLOCK_MONOTONIC_COARSE, time);
  364. }
  365. int mystrcmp(unsigned char *p1, unsigned char *p2)
  366. {
  367. while(*p1==*p2)
  368. {
  369. if(*p1=='\0' || *p2=='\0')
  370. break;
  371. p1++;
  372. p2++;
  373. }
  374. if(*p1=='\0' && *p2=='\0')
  375. return(PASS);
  376. else
  377. return(FAIL);
  378. }
  379. int DiffTimeb(struct timeb ST, struct timeb ET)
  380. {
  381. //return milli-second
  382. unsigned int StartTime,StopTime;
  383. StartTime=(unsigned int)ST.time;
  384. StopTime=(unsigned int)ET.time;
  385. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  386. return (StopTime-StartTime);
  387. }
  388. int DiffTimebWithNowSec(struct timeb ST)
  389. {
  390. //return milli-second
  391. struct timeb ET;
  392. unsigned int StartTime,StopTime;
  393. ftime(&ET);
  394. StartTime=(unsigned int)ST.time;
  395. StopTime=(unsigned int)ET.time;
  396. return (StopTime-StartTime);
  397. }
  398. int CheckTimeOut(uint8_t *start)
  399. {
  400. int result = YES;
  401. struct ParsingResult
  402. {
  403. int result;
  404. int scanedElement;
  405. int year;
  406. int month;
  407. int mday;
  408. int hour;
  409. int min;
  410. int sec;
  411. int tz_hour;
  412. int tz_min;
  413. float minSec;
  414. }parsingResult;
  415. struct tm tmStart;
  416. struct timeb tbStart;
  417. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  418. if(strstr((char*)start, ".") != NULL)
  419. {
  420. // Original data with mini second
  421. if(strstr((char*)start, "Z") != NULL)
  422. {
  423. // Original data with Z
  424. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  425. &parsingResult.year,
  426. &parsingResult.month,
  427. &parsingResult.mday,
  428. &parsingResult.hour,
  429. &parsingResult.min,
  430. &parsingResult.sec,
  431. &parsingResult.minSec);
  432. }
  433. else
  434. {
  435. // Original data without Z
  436. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  437. &parsingResult.year,
  438. &parsingResult.month,
  439. &parsingResult.mday,
  440. &parsingResult.hour,
  441. &parsingResult.min,
  442. &parsingResult.sec,
  443. &parsingResult.minSec,
  444. &parsingResult.tz_hour,
  445. &parsingResult.tz_min);
  446. }
  447. }
  448. else
  449. {
  450. // Original data without mini second
  451. if(strstr((char*)start, "Z") != NULL)
  452. {
  453. // Original data with Z
  454. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  455. &parsingResult.year,
  456. &parsingResult.month,
  457. &parsingResult.mday,
  458. &parsingResult.hour,
  459. &parsingResult.min,
  460. &parsingResult.sec);
  461. }
  462. else
  463. {
  464. // Original data without Z
  465. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  466. &parsingResult.year,
  467. &parsingResult.month,
  468. &parsingResult.mday,
  469. &parsingResult.hour,
  470. &parsingResult.min,
  471. &parsingResult.sec,
  472. &parsingResult.tz_hour,
  473. &parsingResult.tz_min);
  474. }
  475. }
  476. if(parsingResult.scanedElement >= 6)
  477. {
  478. tmStart.tm_year = parsingResult.year - 1900;
  479. tmStart.tm_mon = parsingResult.month - 1;
  480. tmStart.tm_mday = parsingResult.mday;
  481. tmStart.tm_hour = parsingResult.hour;
  482. tmStart.tm_min = parsingResult.min;
  483. tmStart.tm_sec = parsingResult.sec;
  484. tmStart.tm_gmtoff = 0;
  485. tbStart.time = mktime(&tmStart);
  486. tbStart.timezone = 0;
  487. tbStart.millitm = 0;
  488. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  489. if(DiffTimebWithNowSec(tbStart) <= 0)
  490. result = NO;
  491. else
  492. result = YES;
  493. }
  494. else
  495. {
  496. PRINTF_FUNC("Start date parsing error.\n");
  497. }
  498. return result;
  499. }
  500. void SetChargerMode(byte gun_index, byte mode)
  501. {
  502. chargingInfo[gun_index]->SystemStatus = mode;
  503. }
  504. void PRINTF_FUNC(char *string, ...)
  505. {
  506. va_list args;
  507. char buffer[4096];
  508. va_start(args, string);
  509. vsnprintf(buffer, sizeof(buffer), string, args);
  510. va_end(args);
  511. DEBUG_INFO_MSG("%s ", buffer);
  512. }
  513. long long DiffTimebWithNow(struct timeb ST)
  514. {
  515. //return milli-second
  516. struct timeb ET;
  517. long long StartTime, StopTime;
  518. ftime(&ET);
  519. StartTime = (long long)ST.time;
  520. StopTime = (long long)ET.time;
  521. return ((StopTime - StartTime)*1000) + (ET.millitm - ST.millitm);
  522. }
  523. bool CheckWebConfigChargingTimeout(byte gunIndex)
  524. {
  525. bool result = false;
  526. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  527. {
  528. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  529. result = true;
  530. }
  531. // 如果後臺斷線的要多看 offline 設定
  532. if (!result && ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  533. {
  534. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  535. {
  536. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  537. result = true;
  538. }
  539. }
  540. return result;
  541. }
  542. bool CheckWebConfigChargingEnergy(byte gunIndex)
  543. {
  544. bool result = false;
  545. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  546. {
  547. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  548. result = true;
  549. }
  550. // 如果後臺斷線的要多看 offline 設定
  551. if (!result && ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  552. {
  553. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  554. {
  555. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  556. result = true;
  557. }
  558. }
  559. return result;
  560. }
  561. bool CheckWebConfigChargingSOC(byte gunIndex)
  562. {
  563. bool result = false;
  564. if (ShmSysConfigAndInfo->SysConfig.MaxChargingSoc > 0)
  565. {
  566. if (chargingInfo[gunIndex]->EvBatterySoc >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  567. result = true;
  568. }
  569. return result;
  570. }
  571. bool CheckOcppChargingTimeout(byte gunIndex)
  572. {
  573. bool result = false;
  574. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  575. {
  576. if (ShmOCPP16Data->SessionTarget[gunIndex].targetDuration > 0)
  577. {
  578. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmOCPP16Data->SessionTarget[gunIndex].targetDuration * 60))
  579. {
  580. PRINTF_FUNC("CheckOcppChargingTimeout. G_(%d) - (%d) \n", gunIndex,
  581. chargingInfo[gunIndex]->PresentChargedDuration);
  582. result = true;
  583. }
  584. }
  585. }
  586. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  587. {
  588. if (ShmOCPP20Data->SessionTarget[gunIndex].targetDuration > 0)
  589. {
  590. if (chargingInfo [gunIndex]->PresentChargedDuration >= (ShmOCPP20Data->SessionTarget[gunIndex].targetDuration * 60))
  591. {
  592. PRINTF_FUNC ( "CheckOcppChargingTimeout. G_(%d) - (%d) \n", gunIndex,
  593. chargingInfo [gunIndex]->PresentChargedDuration );
  594. result = true;
  595. }
  596. }
  597. }
  598. return result;
  599. }
  600. bool CheckOcppChargingEnergy(byte gunIndex)
  601. {
  602. bool result = false;
  603. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  604. {
  605. if (ShmOCPP16Data->SessionTarget[gunIndex].targetEnergy > 0)
  606. {
  607. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmOCPP16Data->SessionTarget[gunIndex].targetEnergy)
  608. {
  609. PRINTF_FUNC ( "CheckOcppChargingEnergy. G_(%d) - (%f) \n", gunIndex,
  610. chargingInfo[gunIndex]->PresentChargedEnergy );
  611. result = true;
  612. }
  613. }
  614. }
  615. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  616. {
  617. if (ShmOCPP20Data->SessionTarget[gunIndex].targetEnergy > 0)
  618. {
  619. if (chargingInfo [gunIndex]->PresentChargedEnergy >= ShmOCPP20Data->SessionTarget[gunIndex].targetEnergy)
  620. {
  621. PRINTF_FUNC ( "CheckOcppChargingEnergy. G_(%d) - (%f) \n", gunIndex,
  622. chargingInfo [gunIndex]->PresentChargedEnergy );
  623. result = true;
  624. }
  625. }
  626. }
  627. return result;
  628. }
  629. bool CheckOcppChargingSOC(byte gunIndex)
  630. {
  631. bool result = false;
  632. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  633. {
  634. if (ShmOCPP16Data->SessionTarget[gunIndex].targetSoc > 0)
  635. {
  636. if (chargingInfo [gunIndex]->EvBatterySoc >= (ShmOCPP16Data->SessionTarget[gunIndex].targetSoc))
  637. {
  638. PRINTF_FUNC ( "CheckOcppChargingSOC. G_(%d) - (%d) \n", gunIndex,
  639. chargingInfo [gunIndex]->PresentChargedDuration );
  640. result = true;
  641. }
  642. }
  643. }
  644. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  645. {
  646. if (ShmOCPP20Data->SessionTarget[gunIndex].targetSoc > 0)
  647. {
  648. if (chargingInfo [gunIndex]->EvBatterySoc >= (ShmOCPP20Data->SessionTarget[gunIndex].targetSoc))
  649. {
  650. PRINTF_FUNC ( "CheckOcppChargingSOC. G_(%d) - (%d) \n", gunIndex,
  651. chargingInfo [gunIndex]->PresentChargedDuration );
  652. result = true;
  653. }
  654. }
  655. }
  656. return result;
  657. }
  658. bool IsGunUsing(byte dcGunIndex)
  659. {
  660. bool result = false;
  661. if((chargingInfo[dcGunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  662. chargingInfo[dcGunIndex]->SystemStatus <= SYS_MODE_ALARM) ||
  663. (chargingInfo[dcGunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  664. chargingInfo[dcGunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  665. {
  666. result = true;
  667. }
  668. return result;
  669. }
  670. //==========================================
  671. // Log
  672. //==========================================
  673. void InfoFwSlotStatusLog()
  674. {
  675. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  676. {
  677. if ((bd0_1_status == 0 && bd0_2_status == 1) || (bd1_1_status == 0 && bd1_2_status == 1))
  678. PRINTF_FUNC("Connector 1 : Chademo [Ver. = %s]", ShmCHAdeMOData->evse[chargingInfo[0]->type_index].version);
  679. else if ((bd0_1_status == 1 && bd0_2_status == 0) || (bd1_1_status == 1 && bd1_2_status == 0))
  680. PRINTF_FUNC("Connector 1 : CCS [Ver. = %s]", ShmCcsData->V2GMessage_DIN70121[chargingInfo[0]->type_index].version);
  681. else if ((bd0_1_status == 1 && bd0_2_status == 1) || (bd1_1_status == 1 && bd1_2_status == 1))
  682. PRINTF_FUNC("Connector 1 : GB [Ver. = %s]", ShmGBTData->evse[chargingInfo[0]->type_index].version);
  683. }
  684. else
  685. {
  686. if (bd0_1_status == 0 && bd0_2_status == 1)
  687. PRINTF_FUNC("Connector 1 : Chademo [Ver. = %s]", ShmCHAdeMOData->evse[chargingInfo[0]->type_index].version);
  688. else if (bd0_1_status == 1 && bd0_2_status == 0)
  689. PRINTF_FUNC("Connector 1 : CCS [Ver. = %s]", ShmCcsData->V2GMessage_DIN70121[chargingInfo[0]->type_index].version);
  690. else if (bd0_1_status == 1 && bd0_2_status == 1)
  691. PRINTF_FUNC("Connector 1 : GB [Ver. = %s]", ShmGBTData->evse[chargingInfo[0]->type_index].version);
  692. if (bd1_1_status == 0 && bd1_2_status == 1)
  693. PRINTF_FUNC("Connector 2 : Chademo [Ver. = %s]", ShmCHAdeMOData->evse[chargingInfo[1]->type_index].version);
  694. else if (bd1_1_status == 1 && bd1_2_status == 0)
  695. PRINTF_FUNC("Connector 2 : CCS [Ver. = %s]", ShmCcsData->V2GMessage_DIN70121[chargingInfo[1]->type_index].version);
  696. else if (bd1_1_status == 1 && bd1_2_status == 1)
  697. PRINTF_FUNC("Connector 2 : GB [Ver. = %s]", ShmGBTData->evse[chargingInfo[1]->type_index].version);
  698. }
  699. }
  700. void InfoSystemType()
  701. {
  702. if (ShmDcCommonData->systemType == _SYSTEM_TYPE_SIMPLE)
  703. PRINTF_FUNC("DW or DM Serial.");
  704. else if (ShmDcCommonData->systemType == _SYSTEM_TYPE_STANDARD)
  705. PRINTF_FUNC("DS Serial - Standard");
  706. else if (ShmDcCommonData->systemType == _SYSTEM_TYPE_SMART)
  707. PRINTF_FUNC("DS Serial - Dynamic");
  708. }
  709. void InfoLcmVersion()
  710. {
  711. PRINTF_FUNC("LCM Version = V.%03d ", ShmDcCommonData->LcmFwVersion);
  712. }
  713. void CheckHwSlotStatusLog(byte index)
  714. {
  715. if (chargingInfo[index]->Type == _Type_Chademo)
  716. {
  717. PRINTF_FUNC("Hw check : Connector %d, Type : Chademo, Evboard_id = %d \n",
  718. index, chargingInfo[index]->Evboard_id);
  719. }
  720. else if (chargingInfo[index]->Type == _Type_CCS)
  721. {
  722. PRINTF_FUNC("Hw check : Connector %d, Type : CCS, Evboard_id = %d \n",
  723. index, chargingInfo[index]->Evboard_id);
  724. }
  725. else if (chargingInfo[index]->Type == _Type_GB)
  726. {
  727. PRINTF_FUNC("Hw check : Connector %d, Type : GB, Evboard_id = %d \n",
  728. index, chargingInfo[index]->Evboard_id);
  729. }
  730. }
  731. //==========================================
  732. // Check interface status
  733. //==========================================
  734. int isInterfaceUp(const char *interface)
  735. {
  736. int result = FAIL;
  737. FILE *fp;
  738. char cmd[256];
  739. char buf[512];
  740. strcpy(cmd, "ifconfig");
  741. fp = popen(cmd, "r");
  742. if(fp != NULL)
  743. {
  744. while(fgets(buf, sizeof(buf), fp) != NULL)
  745. {
  746. if(strstr(buf, interface) > 0)
  747. {
  748. result = PASS;
  749. }
  750. }
  751. }
  752. pclose(fp);
  753. return result;
  754. }
  755. //=================================
  756. // Create all share memory
  757. //=================================
  758. int CreateShareMemory()
  759. {
  760. int MeterSMId;
  761. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  762. {
  763. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  764. return 0;
  765. }
  766. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  767. {
  768. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  769. return 0;
  770. }
  771. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  772. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  773. {
  774. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  775. return 0;
  776. }
  777. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  778. {
  779. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  780. return 0;
  781. }
  782. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  783. //creat ShmPsuData
  784. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  785. {
  786. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  787. return 0;
  788. }
  789. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  790. {
  791. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  792. return 0;
  793. }
  794. memset(ShmPsuData, 0, sizeof(struct PsuData));
  795. if(CHAdeMO_QUANTITY > 0)
  796. {
  797. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  798. {
  799. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  800. return 0;
  801. }
  802. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  803. {
  804. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  805. return 0;
  806. }
  807. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  808. }
  809. if(GB_QUANTITY > 0)
  810. {
  811. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  812. {
  813. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  814. return 0;
  815. }
  816. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  817. {
  818. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  819. return 0;
  820. }
  821. memset(ShmGBTData, 0, sizeof(struct GBTData));
  822. }
  823. //creat ShmCcsData
  824. if(CCS_QUANTITY > 0)
  825. {
  826. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  827. {
  828. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  829. return 0;
  830. }
  831. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  832. {
  833. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  834. return 0;
  835. }
  836. memset(ShmCcsData, 0, sizeof(struct CcsData));
  837. }
  838. //creat ShmPrimaryMcuData
  839. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  840. {
  841. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  842. return 0;
  843. }
  844. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  845. {
  846. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  847. return 0;
  848. }
  849. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  850. //creat ShmFanModuleData
  851. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  852. {
  853. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  854. return 0;
  855. }
  856. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  857. {
  858. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  859. return 0;
  860. }
  861. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  862. //creat ShmRelayModuleData
  863. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  864. {
  865. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  866. return 0;
  867. }
  868. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  869. {
  870. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  871. return 0;
  872. }
  873. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  874. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  875. {
  876. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmLedModuleData NG \n");
  877. return 0;
  878. }
  879. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  880. {
  881. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmLedModuleData NG \n");
  882. return 0;
  883. }
  884. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  885. //creat ShmOCPP16Data
  886. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  887. {
  888. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  889. return 0;
  890. }
  891. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  892. {
  893. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  894. return 0;
  895. }
  896. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  897. if ((MeterSMId = shmget ( ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777 )) < 0)
  898. {
  899. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmOCPP16DataPH NG \n" );
  900. return 0;
  901. }
  902. else if ((ShmOCPP16DataPH = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  903. {
  904. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmOCPP16DataPH NG \n" );
  905. return 0;
  906. }
  907. memset ( ShmOCPP16DataPH , 0, sizeof(struct OCPP16Data) );
  908. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  909. {
  910. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget OCPP20Data NG\n");
  911. return 0;
  912. }
  913. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  914. {
  915. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat OCPP20Data NG\n");
  916. return 0;
  917. }
  918. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  919. if ((MeterSMId = shmget ( ShmSmartBoxKey, sizeof(struct SmartBoxData), IPC_CREAT | 0777 )) < 0)
  920. {
  921. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmSmartBoxKey NG \n" );
  922. return 0;
  923. }
  924. else if ((ShmSmartBoxData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  925. {
  926. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmSmartBoxData NG \n" );
  927. return 0;
  928. }
  929. memset ( ShmSmartBoxData, 0, sizeof(struct SmartBoxData) );
  930. if ((MeterSMId = shmget(ShmCsuMeterKey, sizeof(struct MeterInformation), IPC_CREAT | 0777)) < 0)
  931. {
  932. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCsuMeterKey NG \n");
  933. return 0;
  934. }
  935. else if ((ShmCsuMeterData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  936. {
  937. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCsuMeterData NG \n");
  938. return 0;
  939. }
  940. memset(ShmCsuMeterData, 0, sizeof(struct MeterInformation));
  941. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  942. {
  943. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCommonKey NG \n");
  944. return 0;
  945. }
  946. else if ((ShmDcCommonData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  947. {
  948. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCommonKey NG \n");
  949. return 0;
  950. }
  951. byte buf = 0;
  952. if (ShmDcCommonData->rebootCount == 1)
  953. buf = ShmDcCommonData->rebootCount;
  954. memset(ShmDcCommonData, 0, sizeof(struct DcCommonInformation));
  955. ShmDcCommonData->rebootCount = buf;
  956. return 1;
  957. }
  958. void SetupRebootCount(byte value)
  959. {
  960. int MeterSMId;
  961. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  962. {
  963. DEBUG_ERROR_MSG("[main]RecordRebootCount:shmget ShmCommonKey NG \n");
  964. }
  965. else
  966. {
  967. ShmDcCommonData->rebootCount = value;
  968. shmctl(MeterSMId, IPC_SET, 0);
  969. }
  970. }
  971. //=================================
  972. // LCM Page
  973. //=================================
  974. void ChangeLcmByIndex(byte page_index)
  975. {
  976. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL ||
  977. page_index == _LCM_COMPLETE || page_index == _LCM_FIX || page_index == _LCM_EMC)
  978. {
  979. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0 &&
  980. (page_index == _LCM_FIX || page_index == _LCM_EMC))
  981. {
  982. bool isUpdate = false;
  983. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  984. {
  985. if (chargingInfo[_index]->SystemStatus == SYS_MODE_UPDATE)
  986. {
  987. isUpdate = true;
  988. break;
  989. }
  990. }
  991. if (!isUpdate &&
  992. ShmDcCommonData->LcmFwVersion > 0 &&
  993. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  994. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_EMC;
  995. else
  996. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_FIX;
  997. }
  998. else
  999. {
  1000. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  1001. }
  1002. }
  1003. }
  1004. //======================================================
  1005. // Peripheral initial
  1006. //======================================================
  1007. void InitGPIO()
  1008. {
  1009. /*****************0~3, 4 bank, bank x 32+ num*********************/
  1010. /***************************************************************/
  1011. /*************** GPIO 0 ***************************************/
  1012. /***************************************************************/
  1013. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  1014. system("echo 22 > /sys/class/gpio/export");
  1015. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  1016. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  1017. system("echo 23 > /sys/class/gpio/export");
  1018. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  1019. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  1020. system("echo 26 > /sys/class/gpio/export");
  1021. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  1022. system("echo 1 > /sys/class/gpio/gpio26/value");
  1023. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  1024. system("echo 27 > /sys/class/gpio/export");
  1025. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  1026. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  1027. system("echo 29 > /sys/class/gpio/export");
  1028. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  1029. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  1030. system("echo 19 > /sys/class/gpio/export");
  1031. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  1032. system("echo 1 > /sys/class/gpio/gpio19/value");
  1033. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  1034. system("echo 20 > /sys/class/gpio/export");
  1035. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  1036. /***************************************************************/
  1037. /*************** GPIO 1 ***************************************/
  1038. /***************************************************************/
  1039. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  1040. system("echo 44 > /sys/class/gpio/export");
  1041. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  1042. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  1043. system("echo 45 > /sys/class/gpio/export");
  1044. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  1045. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  1046. system("echo 46 > /sys/class/gpio/export");
  1047. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  1048. system("echo 0 > /sys/class/gpio/gpio46/value");
  1049. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  1050. system("echo 47 > /sys/class/gpio/export");
  1051. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  1052. /***************************************************************/
  1053. /*************** GPIO 2 ***************************************/
  1054. /***************************************************************/
  1055. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  1056. system("echo 89 > /sys/class/gpio/export");
  1057. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  1058. system("echo 1 > /sys/class/gpio/gpio89/value");
  1059. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  1060. system("echo 87 > /sys/class/gpio/export");
  1061. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  1062. system("echo 0 > /sys/class/gpio/gpio87/value");
  1063. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  1064. system("echo 88 > /sys/class/gpio/export");
  1065. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  1066. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  1067. system("echo 86 > /sys/class/gpio/export");
  1068. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  1069. /***************************************************************/
  1070. /*************** GPIO 3 ***************************************/
  1071. /***************************************************************/
  1072. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  1073. system("echo 111 > /sys/class/gpio/export");
  1074. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  1075. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  1076. system("echo 114 > /sys/class/gpio/export");
  1077. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  1078. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  1079. system("echo 113 > /sys/class/gpio/export");
  1080. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  1081. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  1082. system("echo 110 > /sys/class/gpio/export");
  1083. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  1084. system("echo 0 > /sys/class/gpio/gpio110/value");
  1085. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  1086. system("echo 115 > /sys/class/gpio/export");
  1087. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  1088. system("echo 0 > /sys/class/gpio/gpio115/value");
  1089. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  1090. system("echo 112 > /sys/class/gpio/export");
  1091. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  1092. system("echo 1 > /sys/class/gpio/gpio112/value");
  1093. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  1094. system("echo 116 > /sys/class/gpio/export");
  1095. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  1096. system("echo 0 > /sys/class/gpio/gpio116/value");
  1097. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  1098. system("echo 103 > /sys/class/gpio/export");
  1099. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  1100. system("echo 0 > /sys/class/gpio/gpio103/value");
  1101. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  1102. system("echo 104 > /sys/class/gpio/export");
  1103. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  1104. system("echo 0 > /sys/class/gpio/gpio104/value");
  1105. #ifdef SystemLogMessage
  1106. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK \n");
  1107. #endif
  1108. }
  1109. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1110. {
  1111. int fd,wrd;
  1112. unsigned char *buf;
  1113. unsigned int ChkSum,ChkSumOrg;
  1114. if((buf=malloc(MtdBlockSize))==NULL)
  1115. {
  1116. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  1117. if(ShmStatusCodeData!=NULL)
  1118. {
  1119. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1120. }
  1121. sleep(5);
  1122. system("reboot -f");
  1123. sleep(5);
  1124. system("reboot -f");
  1125. }
  1126. memset(buf, 0, MtdBlockSize);
  1127. //================================================
  1128. // Load configuration from mtdblock10
  1129. //================================================
  1130. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1131. //fd = open("/dev/mtdblock10", O_RDWR);
  1132. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1133. if (fd < 0)
  1134. {
  1135. free(buf);
  1136. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  1137. if(ShmStatusCodeData!=NULL)
  1138. {
  1139. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1140. }
  1141. sleep(5);
  1142. system("reboot -f");
  1143. sleep(5);
  1144. system("reboot -f");
  1145. }
  1146. wrd=read(fd, buf, MtdBlockSize);
  1147. close(fd);
  1148. if(wrd<MtdBlockSize)
  1149. {
  1150. free(buf);
  1151. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  1152. if(ShmStatusCodeData!=NULL)
  1153. {
  1154. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1155. }
  1156. sleep(5);
  1157. system("reboot -f");
  1158. sleep(5);
  1159. system("reboot -f");
  1160. }
  1161. ChkSum=0;
  1162. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1163. // {
  1164. // ChkSum+=buf[wrd];
  1165. // }
  1166. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1167. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1168. {
  1169. ChkSum+=buf[wrd];
  1170. }
  1171. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1172. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1173. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1174. //================================================
  1175. // Load configuration from mtdblock11
  1176. //================================================
  1177. if(ChkSum!=ChkSumOrg)
  1178. {
  1179. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  1180. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1181. //fd = open("/dev/mtdblock11", O_RDWR);
  1182. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1183. if (fd < 0)
  1184. {
  1185. free(buf);
  1186. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  1187. if(ShmStatusCodeData!=NULL)
  1188. {
  1189. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1190. }
  1191. sleep(5);
  1192. system("reboot -f");
  1193. sleep(5);
  1194. system("reboot -f");
  1195. }
  1196. memset(buf, 0, MtdBlockSize);
  1197. wrd=read(fd, buf,MtdBlockSize);
  1198. close(fd);
  1199. if(wrd<MtdBlockSize)
  1200. {
  1201. free(buf);
  1202. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  1203. if(ShmStatusCodeData!=NULL)
  1204. {
  1205. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1206. }
  1207. sleep(5);
  1208. system("reboot -f");
  1209. sleep(5);
  1210. system("reboot -f");
  1211. }
  1212. ChkSum=0;
  1213. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1214. // {
  1215. // ChkSum+=buf[wrd];
  1216. // }
  1217. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1218. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1219. {
  1220. ChkSum+=buf[wrd];
  1221. }
  1222. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1223. //================================================
  1224. // Load configuration from mtdblock12 (Factory default)
  1225. //================================================
  1226. if(ChkSum!=ChkSumOrg)
  1227. {
  1228. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  1229. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1230. //fd = open("/dev/mtdblock12", O_RDWR);
  1231. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1232. if (fd < 0)
  1233. {
  1234. free(buf);
  1235. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  1236. if(ShmStatusCodeData!=NULL)
  1237. {
  1238. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1239. }
  1240. sleep(5);
  1241. system("reboot -f");
  1242. sleep(5);
  1243. system("reboot -f");
  1244. }
  1245. memset(buf, 0, MtdBlockSize);
  1246. wrd=read(fd, buf,MtdBlockSize);
  1247. close(fd);
  1248. if(wrd<MtdBlockSize)
  1249. {
  1250. free(buf);
  1251. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  1252. if(ShmStatusCodeData!=NULL)
  1253. {
  1254. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1255. }
  1256. sleep(5);
  1257. system("reboot -f");
  1258. sleep(5);
  1259. system("reboot -f");
  1260. }
  1261. ChkSum=0;
  1262. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1263. // {
  1264. // ChkSum+=buf[wrd];
  1265. // }
  1266. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1267. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1268. {
  1269. ChkSum+=buf[wrd];
  1270. }
  1271. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1272. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1273. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1274. if(ChkSum!=ChkSumOrg)
  1275. {
  1276. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  1277. free(buf);
  1278. //system("cd /root;./FactoryConfig -m");
  1279. sleep(5);
  1280. char facBuf[256];
  1281. sprintf(facBuf, "cd /root;./FactoryConfig -m %s %s",
  1282. ShmSysConfigAndInfo->SysConfig.ModelName,
  1283. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  1284. system(facBuf);
  1285. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1286. system("sync");
  1287. sleep(5);
  1288. system("reboot -f");
  1289. sleep(5);
  1290. system("reboot -f");
  1291. return FAIL;
  1292. }
  1293. }
  1294. }
  1295. //load OK
  1296. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1297. free(buf);
  1298. system("rm -f /mnt/EvseConfig.bin");
  1299. // SysConfig in flash is empty (0xffffffff)
  1300. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1301. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1302. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1303. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1304. {
  1305. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1306. {
  1307. PRINTF_FUNC("Model name over length.\n");
  1308. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1309. }
  1310. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1311. {
  1312. PRINTF_FUNC("Model serial number over length.\n");
  1313. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1314. }
  1315. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1316. {
  1317. PRINTF_FUNC("System id over length.\n");
  1318. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1319. }
  1320. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1321. {
  1322. PRINTF_FUNC("Ethernet dhcp config is null.\n");
  1323. }
  1324. system("cd /root;./Module_FactoryConfig -m");
  1325. sleep(3);
  1326. system("/usr/bin/run_evse_restart.sh");
  1327. }
  1328. PRINTF_FUNC("*********************************************************** \n");
  1329. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  1330. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1331. {
  1332. memcpy((char*)ShmOCPP16Data->ChargeBoxId,
  1333. (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1334. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1335. }
  1336. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1337. {
  1338. memcpy((char*)ShmOCPP20Data->ChargeBoxId,
  1339. (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1340. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1341. }
  1342. return PASS;
  1343. }
  1344. int isRouteFail()
  1345. {
  1346. int result = YES;
  1347. FILE *fp;
  1348. char buf[512];
  1349. fp = popen("route -n", "r");
  1350. if(fp != NULL)
  1351. {
  1352. while(fgets(buf, sizeof(buf), fp) != NULL)
  1353. {
  1354. if(strstr(buf, "eth0") != NULL)
  1355. result = NO;
  1356. }
  1357. }
  1358. pclose(fp);
  1359. return result;
  1360. }
  1361. int isReachableInternet()
  1362. {
  1363. int result = FAIL;
  1364. FILE *fp;
  1365. char cmd[256];
  1366. char buf[512];
  1367. char tmp[512];
  1368. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1369. // {
  1370. // result = PASS;
  1371. // }
  1372. // else
  1373. {
  1374. strcpy(cmd, "ifconfig eth0");
  1375. fp = popen(cmd, "r");
  1376. if (fp != NULL)
  1377. {
  1378. while(fgets(buf, sizeof(buf), fp) != NULL)
  1379. {
  1380. if (strstr(buf, "inet addr:") > 0)
  1381. {
  1382. sscanf(buf, "%*s%s", tmp);
  1383. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1384. tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  1385. sscanf(buf, "%*s%*s%*s%s", tmp);
  1386. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1387. tmp, strspn(tmp, "Mask:"), strlen(tmp)-strspn(tmp, "Mask:"));
  1388. }
  1389. }
  1390. }
  1391. pclose(fp);
  1392. memset(buf, 0x00, sizeof(buf));
  1393. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1394. {
  1395. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1396. fp = popen(cmd, "r");
  1397. if(fp != NULL)
  1398. {
  1399. while(fgets(buf, sizeof(buf), fp) != NULL)
  1400. {
  1401. if(strstr(buf, "transmitted") > 0)
  1402. {
  1403. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1404. if(strstr(buf,"100%") != NULL)
  1405. {
  1406. }
  1407. else
  1408. {
  1409. result = PASS;
  1410. }
  1411. //DEBUG_INFO("%s",buf);
  1412. //DEBUG_INFO("%s\n",tmp);
  1413. }
  1414. }
  1415. }
  1416. pclose(fp);
  1417. }
  1418. }
  1419. return result;
  1420. }
  1421. void InitEthernet()
  1422. {
  1423. system("ifconfig eth0 down");// eth0 down
  1424. system("ifconfig eth1 down");// eth1 down
  1425. sleep(2);
  1426. char tmpbuf[256];
  1427. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1428. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1429. sleep(2);
  1430. //Init Eth0 for internet
  1431. memset(tmpbuf,0,256);
  1432. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1433. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1434. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1435. system(tmpbuf);
  1436. memset(tmpbuf,0,256);
  1437. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1438. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1439. system(tmpbuf);
  1440. //system("ifconfig lo up");
  1441. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1442. //Init Eth1 for administrator tool
  1443. memset(tmpbuf,0,256);
  1444. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  1445. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  1446. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  1447. system(tmpbuf);
  1448. //Run DHCP client if enabled
  1449. system("killall udhcpc");
  1450. system("rm -rf /etc/resolv.conf");
  1451. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1452. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1453. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1454. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1455. {
  1456. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1457. system(tmpbuf);
  1458. }
  1459. //Upgrade system id to /etc/hostname
  1460. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1461. system(tmpbuf);
  1462. pid_t pid = fork();
  1463. uint8_t cnt_pingDNS_Fail;
  1464. if(pid == 0)
  1465. {
  1466. for(;;)
  1467. {
  1468. if (isRouteFail())
  1469. {
  1470. //PRINTF_FUNC("eth0 not in route, restart eht0. \n");
  1471. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1472. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1473. {
  1474. InitialDHCP();
  1475. }
  1476. else
  1477. {
  1478. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1479. memset(tmpbuf,0,256);
  1480. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1481. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1482. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1483. system(tmpbuf);
  1484. memset(tmpbuf,0,256);
  1485. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1486. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1487. system(tmpbuf);
  1488. }
  1489. }
  1490. if(isReachableInternet() == PASS)
  1491. {
  1492. //ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1493. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1494. cnt_pingDNS_Fail = 0;
  1495. }
  1496. else
  1497. {
  1498. if(++cnt_pingDNS_Fail > 3)
  1499. {
  1500. //ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1501. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1502. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1503. {
  1504. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1505. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1506. system(tmpbuf);
  1507. }
  1508. else
  1509. {
  1510. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1511. memset(tmpbuf,0,256);
  1512. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1513. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1514. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1515. system(tmpbuf);
  1516. memset(tmpbuf,0,256);
  1517. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1518. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1519. system(tmpbuf);
  1520. }
  1521. cnt_pingDNS_Fail = 0;
  1522. }
  1523. }
  1524. bool ethResult = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet;
  1525. ShmSysConfigAndInfo->SysInfo.ethInternetConn = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet;
  1526. if(ethResult == YES)
  1527. {
  1528. system("/sbin/ifmetric eth0 0");
  1529. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') ||
  1530. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1531. system("/sbin/ifmetric mlan0 1");
  1532. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') ||
  1533. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1534. system("/sbin/ifmetric ppp0 2");
  1535. }
  1536. if (!ethResult &&
  1537. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1538. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1539. {
  1540. //ethResult = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn;
  1541. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1542. if (ethResult)
  1543. {
  1544. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1545. {
  1546. system("/sbin/ifmetric eth0 1");
  1547. system("/sbin/ifmetric mlan0 0");
  1548. }
  1549. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1550. system("/sbin/ifmetric ppp0 2");
  1551. }
  1552. }
  1553. if (!ethResult &&
  1554. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1555. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1556. {
  1557. //ethResult = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn;
  1558. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == YES ? NO : YES;
  1559. if (ethResult)
  1560. {
  1561. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1562. system("/sbin/ifmetric mlan0 2");
  1563. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1564. {
  1565. system("/sbin/ifmetric eth0 1");
  1566. system("/sbin/ifmetric ppp0 0");
  1567. }
  1568. }
  1569. }
  1570. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1571. sleep(5);
  1572. }
  1573. }
  1574. #ifdef SystemLogMessage
  1575. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK. \n");
  1576. #endif
  1577. }
  1578. int InitialRfidPort()
  1579. {
  1580. int uartO2 = open(rfidPortName, O_RDWR);
  1581. struct termios tios;
  1582. if (uartO2 != FAIL)
  1583. {
  1584. ioctl (uartO2, TCGETS, &tios);
  1585. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1586. tios.c_lflag = 0;
  1587. tios.c_iflag = 0;
  1588. tios.c_oflag = 0;
  1589. tios.c_cc[VMIN] = 0;
  1590. tios.c_cc[VTIME] = (unsigned char) 1;
  1591. tios.c_lflag = 0;
  1592. tcflush(uartO2, TCIFLUSH);
  1593. ioctl(uartO2, TCSETS, &tios);
  1594. }
  1595. if (uartO2 < 0)
  1596. {
  1597. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1598. }
  1599. return uartO2;
  1600. }
  1601. void GetMacAddress()
  1602. {
  1603. for (byte index = 0; index < 2; index++)
  1604. {
  1605. int fd;
  1606. struct ifreq ifr;
  1607. char tarEth[5];
  1608. char Mac[18];
  1609. sprintf(tarEth,"eth%d",index);
  1610. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1611. ifr.ifr_addr.sa_family = AF_INET;
  1612. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1613. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1614. close(fd);
  1615. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1616. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1617. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1618. if (index == 0)
  1619. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1620. else
  1621. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1622. }
  1623. }
  1624. void GetFirmwareVersion()
  1625. {
  1626. // Get CSU root file system version
  1627. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1628. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1629. for(uint8_t idx=0;idx<3;idx++)
  1630. {
  1631. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J' ||
  1632. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'K' ||
  1633. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'L' ||
  1634. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'S')
  1635. {
  1636. chademo++;
  1637. count++;
  1638. }
  1639. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1640. {
  1641. gb++;
  1642. count++;
  1643. }
  1644. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'U' ||
  1645. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'E' ||
  1646. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'T' ||
  1647. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'D' ||
  1648. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'M' ||
  1649. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'N' ||
  1650. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'R' ||
  1651. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'V' ||
  1652. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'F' ||
  1653. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'Y' ||
  1654. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'Z' )
  1655. {
  1656. ccs++;
  1657. count++;
  1658. }
  1659. }
  1660. if (count == 1)
  1661. {
  1662. if (chademo > 0)
  1663. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1664. else if (ccs > 0)
  1665. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1666. else if (gb > 0)
  1667. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1668. }
  1669. else
  1670. {
  1671. if (chademo == 1 && ccs == 1)
  1672. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1673. else if (chademo == 1 && gb == 1)
  1674. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1675. else if (ccs == 1 && gb == 1)
  1676. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1677. else if (chademo == 2)
  1678. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '7';
  1679. else if (ccs == 2)
  1680. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '8';
  1681. else if (gb == 2)
  1682. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '9';
  1683. }
  1684. // Get network option from model name
  1685. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1686. {
  1687. case 'B':
  1688. case 'U':
  1689. //Blue tooth
  1690. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1691. break;
  1692. case 'W':
  1693. // WIFI
  1694. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1695. break;
  1696. case 'T':
  1697. // 3G/4G
  1698. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1699. break;
  1700. case 'D':
  1701. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1702. break;
  1703. default:
  1704. // LAN
  1705. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1706. break;
  1707. }
  1708. // Get rating power from model name
  1709. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1710. // Get IEC or UL
  1711. char _buf[3] = {0};
  1712. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[3], 1);
  1713. if (strcmp(_buf, "E") == EQUAL || strcmp(_buf, "G") == EQUAL || strcmp(_buf, "C") == EQUAL ||
  1714. strcmp(_buf, "T") == EQUAL || strcmp(_buf, "K") == EQUAL || strcmp(_buf, "Z") == EQUAL ||
  1715. strcmp(_buf, "M") == EQUAL || strcmp(_buf, "P") == EQUAL || strcmp(_buf, "I") == EQUAL ||
  1716. strcmp(_buf, "F") == EQUAL)
  1717. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1718. else if (strcmp(_buf, "U") == EQUAL || strcmp(_buf, "B") == EQUAL || strcmp(_buf, "L") == EQUAL)
  1719. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1720. else if (strcmp(_buf, "J") == EQUAL)//LWADD
  1721. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_JARI;
  1722. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
  1723. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
  1724. if (strcmp(_buf, "PN") == EQUAL ||
  1725. strcmp(_buf, "N7") == EQUAL ||
  1726. strcmp(_buf, "NX") == EQUAL ||
  1727. strcmp(_buf, "OL") == EQUAL ||
  1728. strcmp(_buf, "DE") == EQUAL)
  1729. ShmDcCommonData->ShowLogoFlag = NO;
  1730. else
  1731. ShmDcCommonData->ShowLogoFlag = YES;
  1732. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'G')
  1733. ShmDcCommonData->chargerType = CHARGER_TYPE_SIMPLE;
  1734. else
  1735. ShmDcCommonData->chargerType = CHARGER_TYPE_STANDARD;
  1736. }
  1737. void InitialGunIndexToUnUse()
  1738. {
  1739. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  1740. {
  1741. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index = NO_DEFINE;
  1742. }
  1743. for (byte index = 0; index < CCS_QUANTITY; index++)
  1744. {
  1745. ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index = NO_DEFINE;
  1746. }
  1747. for (byte index = 0; index < GB_QUANTITY; index++)
  1748. {
  1749. ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index = NO_DEFINE;
  1750. }
  1751. for (byte index = 0; index < AC_QUANTITY; index++)
  1752. {
  1753. ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index = NO_DEFINE;
  1754. }
  1755. }
  1756. void InitialShareMemoryInfo()
  1757. {
  1758. FILE *fp;
  1759. char cmd[512];
  1760. char buf[512];
  1761. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1762. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1763. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1764. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1765. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1766. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1767. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1768. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1769. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1770. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1771. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1772. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1773. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1774. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1775. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1776. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1777. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1778. sprintf(cmd, "/bin/uname -r");
  1779. fp = popen(cmd, "r");
  1780. if(fp == NULL)
  1781. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1782. else
  1783. {
  1784. while(fgets(buf, sizeof(buf), fp) != NULL)
  1785. {
  1786. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1787. }
  1788. }
  1789. GetFirmwareVersion();
  1790. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1791. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1792. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1793. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1794. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1795. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1796. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1797. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1798. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1799. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1800. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1801. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1802. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1803. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1804. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1805. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1806. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1807. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1808. ShmSysConfigAndInfo->SysInfo.InternetConn = NO;
  1809. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = NO;
  1810. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1811. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1812. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1813. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1814. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1815. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = NO;
  1816. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1817. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1818. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1819. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1820. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail = NO;
  1821. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1822. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1823. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1824. ShmRelayModuleData->SelfTest_Comp = NO;
  1825. ShmFanModuleData->SelfTest_Comp = NO;
  1826. ShmLedModuleData->SelfTest_Comp = NO;
  1827. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_NORMAL;
  1828. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1829. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  1830. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1831. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOTTING;
  1832. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  1833. ShmFanModuleData->TestFanSpeed = 0;
  1834. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1835. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1836. InitialGunIndexToUnUse();
  1837. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1838. ShmDcCommonData->systemType = _SYSTEM_TYPE_SIMPLE;
  1839. ShmDcCommonData->CcsVersion = _CCS_VERSION_CHECK_TAG_V015S0;
  1840. ShmDcCommonData->psuKeepCommunication = NO;
  1841. ShmDcCommonData->acContactSwitch = NO;
  1842. ShmDcCommonData->ConnectErrList[0].GunErrMessage = 0;
  1843. ShmDcCommonData->ConnectErrList[1].GunErrMessage = 0;
  1844. ShmDcCommonData->LcmFwVersion = 0;
  1845. ShmDcCommonData->minOutputCur = 0;
  1846. ShmDcCommonData->evBoardResetFlag = NO;
  1847. ShmDcCommonData->enObtainAuthorizbyCCID = NO;
  1848. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  1849. ShmDcCommonData->_psuProtocol = _PSU_PROTOCOL_INFY;
  1850. ShmDcCommonData->_offlinePower = 0;
  1851. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE; //LW_tpc plug detect
  1852. ShmDcCommonData->TpcDetectFunction = NO; //LW_tpc plug detect
  1853. ShmDcCommonData->_isPsuErrorOccur = NO;
  1854. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1855. }
  1856. int Initialization()
  1857. {
  1858. // 初始化卡號驗證的 Flag
  1859. ClearAuthorizedFlag();
  1860. // 初始化插槍驗證的 Flag
  1861. ClearDetectPluginFlag();
  1862. // UART 2 for Rfid
  1863. rfidFd = InitialRfidPort();
  1864. int pinOut[2] = { 116, 115 }; //D13(Cha)、C13(CCS)
  1865. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1866. {
  1867. chargingInfo[count]->RemoteStartFlag = NO;
  1868. chargingInfo[count]->ReservedStartFlag = NO;
  1869. if (chargingInfo[count]->Type == _Type_Chademo)
  1870. {
  1871. gpio_set_value(pinOut[count], 0x00);
  1872. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1873. }
  1874. else if (chargingInfo[count]->Type == _Type_GB)
  1875. {
  1876. gpio_set_value(pinOut[count], 0x00);
  1877. ShmGBTData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1878. }
  1879. else if (chargingInfo[count]->Type == _Type_CCS)
  1880. {
  1881. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1882. gpio_set_value(pinOut[1], 0x00);
  1883. else
  1884. gpio_set_value(pinOut[count], 0x00);
  1885. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1886. }
  1887. strcpy((char *)ShmDcCommonData->_reserved_UserId[count], "");
  1888. }
  1889. byte acDirIndex = 0;
  1890. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1891. {
  1892. if (count == 1)
  1893. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  1894. ocpp_set_noErrorCode_cmd(count + acDirIndex, "NoError");
  1895. }
  1896. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  1897. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  1898. {
  1899. ac_chargingInfo[count]->RemoteStartFlag = NO;
  1900. if (ac_chargingInfo[count]->Type == _Type_AC)
  1901. {
  1902. ac_chargingInfo[count]->SelfTest_Comp = NO;
  1903. ocpp_set_noErrorCode_cmd(count + hasDc, "NoError");
  1904. }
  1905. }
  1906. PRINTF_FUNC("Information check done \n");
  1907. return PASS;
  1908. }
  1909. bool InitialSystemDefaultConfig()
  1910. {
  1911. bool result = true;
  1912. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1913. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  1914. InitGPIO();
  1915. InitEthernet();
  1916. GetMacAddress();
  1917. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1918. // sleep(3);
  1919. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1920. // sleep(1);
  1921. // system("/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up");
  1922. return result;
  1923. }
  1924. bool DisplaySelfTestFailReason()
  1925. {
  1926. bool result = false;
  1927. // RB、FB、407、EV 小板中有些板子無回應
  1928. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1929. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; result = true; }
  1930. if (ShmFanModuleData->SelfTest_Comp == NO)
  1931. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; result = true; }
  1932. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1933. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; result = true; }
  1934. // if (ShmLedModuleData->SelfTest_Comp == NO)
  1935. // { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES; }
  1936. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES)
  1937. { result = true; }
  1938. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1939. {
  1940. if (chargingInfo[index]->Type == _Type_Chademo)
  1941. {
  1942. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1943. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; result = true; }
  1944. }
  1945. else if (chargingInfo[index]->Type == _Type_GB)
  1946. {
  1947. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1948. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; result = true; }
  1949. }
  1950. else if (chargingInfo[index]->Type == _Type_CCS)
  1951. {
  1952. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1953. {
  1954. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1955. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; result = true; }
  1956. }
  1957. }
  1958. }
  1959. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1960. {
  1961. // 先借 GBT 顯示
  1962. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  1963. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; result = true; }
  1964. }
  1965. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1966. {
  1967. // AC Contact 未搭上
  1968. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1969. result = true;
  1970. }
  1971. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  1972. {
  1973. result = true;
  1974. }
  1975. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  1976. {
  1977. // PSU 通訊問題
  1978. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1979. result = true;
  1980. }
  1981. return result;
  1982. }
  1983. void SelfTestRun()
  1984. {
  1985. bool evInitFlag = false;
  1986. StartSystemTimeoutDet(Timeout_SelftestChk);
  1987. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1988. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  1989. GetTimeoutValue(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) <= 20)
  1990. {
  1991. ReviewCriticalAlarm();
  1992. ChkPrimaryStatus();
  1993. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL ||
  1994. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  1995. {
  1996. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1997. return;
  1998. }
  1999. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  2000. {
  2001. if (ShmPsuData->Work_Step == _NO_WORKING ||
  2002. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  2003. {
  2004. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2005. return;
  2006. }
  2007. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  2008. {
  2009. case _STEST_VERSION:
  2010. {
  2011. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
  2012. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  2013. {
  2014. //PRINTF_FUNC("RB pass \n");
  2015. ShmRelayModuleData->SelfTest_Comp = YES;
  2016. }
  2017. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  2018. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  2019. {
  2020. //PRINTF_FUNC("Fan pass \n");
  2021. ShmFanModuleData->SelfTest_Comp = YES;
  2022. }
  2023. if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
  2024. ShmPrimaryMcuData->version[0] != '\0')
  2025. {
  2026. //PRINTF_FUNC("407 pass \n");
  2027. ShmPrimaryMcuData->SelfTest_Comp = YES;
  2028. }
  2029. // EV 小板
  2030. if (!evInitFlag)
  2031. {
  2032. evInitFlag = YES;
  2033. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2034. {
  2035. if (chargingInfo[index]->Type == _Type_Chademo)
  2036. {
  2037. if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
  2038. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  2039. {
  2040. //PRINTF_FUNC("chademo pass \n");
  2041. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  2042. }
  2043. else
  2044. {
  2045. //PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  2046. evInitFlag = NO;
  2047. }
  2048. }
  2049. else if (chargingInfo[index]->Type == _Type_GB)
  2050. {
  2051. if (strlen((char *)ShmGBTData->evse[chargingInfo[index]->type_index].version) != 0 ||
  2052. ShmGBTData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  2053. {
  2054. //PRINTF_FUNC("GBT pass \n");
  2055. ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  2056. }
  2057. else
  2058. {
  2059. //PRINTF_FUNC("GBT fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  2060. evInitFlag = NO;
  2061. }
  2062. }
  2063. else if (chargingInfo[index]->Type == _Type_CCS)
  2064. {
  2065. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2066. {
  2067. if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
  2068. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  2069. {
  2070. //PRINTF_FUNC("ccs fw = %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  2071. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  2072. }
  2073. else
  2074. {
  2075. //PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  2076. evInitFlag = NO;
  2077. }
  2078. }
  2079. }
  2080. }
  2081. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  2082. {
  2083. if (ac_chargingInfo[index]->Type == _Type_AC)
  2084. {
  2085. if (strlen((char *)ac_chargingInfo[index]->version) != 0 ||
  2086. ac_chargingInfo[index]->version[0] != '\0')
  2087. {
  2088. ac_chargingInfo[index]->SelfTest_Comp = YES;
  2089. }
  2090. else
  2091. {
  2092. evInitFlag = NO;
  2093. }
  2094. }
  2095. }
  2096. }
  2097. if (ShmFanModuleData->SelfTest_Comp &&
  2098. ShmRelayModuleData->SelfTest_Comp &&
  2099. ShmPrimaryMcuData->SelfTest_Comp &&
  2100. evInitFlag)
  2101. {
  2102. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  2103. }
  2104. }
  2105. break;
  2106. case _STEST_AC_CONTACTOR:
  2107. {
  2108. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  2109. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  2110. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  2111. {
  2112. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  2113. PRINTF_FUNC("Communication board pass. \n");
  2114. }
  2115. }
  2116. break;
  2117. case _STEST_PSU_DETECT:
  2118. {
  2119. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  2120. {
  2121. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  2122. }
  2123. }
  2124. break;
  2125. case _STEST_PSU_CAP:
  2126. {
  2127. // 此測試是要確認當前總輸出能力
  2128. // 如果沒有 PSU 模組請 bypass
  2129. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  2130. {
  2131. sleep(1);
  2132. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  2133. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  2134. }
  2135. }
  2136. break;
  2137. }
  2138. }
  2139. else
  2140. break;
  2141. usleep(100000);
  2142. }
  2143. }
  2144. int SpawnTask()
  2145. {
  2146. sleep(2);
  2147. system("/root/Module_EventLogging &");
  2148. system("/root/Module_PrimaryComm &");
  2149. system("/root/Module_EvComm &");
  2150. system("/root/Module_LcmControl &");
  2151. system("/root/Module_InternalComm &");
  2152. system("/root/Module_PsuComm &");
  2153. system("/root/Module_SmartBox &");
  2154. system("/root/Module_ProduceUtils &");
  2155. ocpp_process_start();
  2156. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  2157. {
  2158. system("/root/Module_4g &");
  2159. }
  2160. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2161. {
  2162. system("/root/Module_Wifi &");
  2163. }
  2164. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2165. {
  2166. system("/root/Module_4g &");
  2167. system("/root/Module_Wifi &");
  2168. }
  2169. return PASS;
  2170. }
  2171. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2172. {
  2173. int result = PASS;
  2174. int fd,wrd;
  2175. unsigned int i,Chk;
  2176. unsigned char *ptr, *BufTmp;
  2177. Chk=0;
  2178. ptr=(unsigned char *)UsrData;
  2179. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2180. {
  2181. memset(BufTmp, 0, MtdBlockSize);
  2182. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2183. for(i=0; i<MtdBlockSize-4; i++)
  2184. Chk+=*(ptr+i);
  2185. memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
  2186. fd = open("/dev/mtdblock10", O_RDWR);
  2187. if (fd > 0)
  2188. {
  2189. wrd=write(fd, BufTmp, MtdBlockSize);
  2190. close(fd);
  2191. if(wrd >= MtdBlockSize)
  2192. {
  2193. fd = open("/dev/mtdblock11", O_RDWR);
  2194. if (fd > 0)
  2195. {
  2196. wrd=write(fd, BufTmp, MtdBlockSize);
  2197. close(fd);
  2198. if(wrd < MtdBlockSize)
  2199. {
  2200. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  2201. result = FAIL;
  2202. }
  2203. }
  2204. else
  2205. {
  2206. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  2207. result = FAIL;
  2208. }
  2209. }
  2210. else
  2211. {
  2212. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  2213. result = FAIL;
  2214. }
  2215. }
  2216. else
  2217. {
  2218. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  2219. result = FAIL;
  2220. }
  2221. }
  2222. else
  2223. {
  2224. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  2225. result = FAIL;
  2226. }
  2227. if(BufTmp != NULL)
  2228. free(BufTmp);
  2229. return result;
  2230. }
  2231. //===============================================
  2232. // Common Detect Chk - Stop Charging ?
  2233. //===============================================
  2234. bool isEvBoardStopChargeFlag(byte gunIndex)
  2235. {
  2236. return chargingInfo[gunIndex]->StopChargeFlag;
  2237. }
  2238. bool isEvBoardNormalStopChargeFlag(byte gunIndex)
  2239. {
  2240. return chargingInfo[gunIndex]->NormalStopChargeFlag;
  2241. }
  2242. //===============================================
  2243. // 插槍鎖槍狀況
  2244. //===============================================
  2245. void ClearDetectPluginFlag()
  2246. {
  2247. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2248. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2249. {
  2250. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  2251. chargingInfo[gun_index]->RemoteStartFlag = NO;
  2252. if (chargingInfo[gun_index]->ReservedStartFlag == YES)
  2253. chargingInfo[gun_index]->ReservedStartFlag = NO;
  2254. }
  2255. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  2256. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2257. }
  2258. void DetectPluginStart()
  2259. {
  2260. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  2261. }
  2262. bool isDetectPlugin()
  2263. {
  2264. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  2265. return YES;
  2266. return NO;
  2267. }
  2268. bool isEvGunLocked(byte gunIndex)
  2269. {
  2270. return chargingInfo[gunIndex]->GunLocked;
  2271. }
  2272. //===============================================
  2273. // Common Detect Chk - Chademo
  2274. //===============================================
  2275. bool isEvContactorWelding_chademo(byte gunIndex)
  2276. {
  2277. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  2278. }
  2279. bool isEvStopReq_chademo(byte gunIndex)
  2280. {
  2281. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  2282. }
  2283. byte isPrechargeStatus_chademo(byte gunIndex)
  2284. {
  2285. byte result = 0x00;
  2286. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2287. return result;
  2288. }
  2289. //===============================================
  2290. // Common Detect Chk - GB
  2291. //===============================================
  2292. byte isPrechargeStatus_gb(byte gunIndex)
  2293. {
  2294. byte result = 0x00;
  2295. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2296. return result;
  2297. }
  2298. //===============================================
  2299. // Common Detect Chk - CCS
  2300. //===============================================
  2301. byte isPrechargeStatus_ccs(byte gunIndex)
  2302. {
  2303. byte result = 0x00;
  2304. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2305. {
  2306. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2307. }
  2308. return result;
  2309. }
  2310. //===============================================
  2311. // Callback
  2312. //===============================================
  2313. void DisplayChargingInfo()
  2314. {
  2315. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  2316. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2317. {
  2318. if (chargingInfo[i]->SystemStatus != SYS_MODE_IDLE &&
  2319. chargingInfo[i]->SystemStatus != SYS_MODE_RESERVATION)
  2320. {
  2321. ChangeGunSelectByIndex(i);
  2322. usleep(50000);
  2323. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2324. return;
  2325. }
  2326. }
  2327. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2328. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  2329. ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_COMPLETE)
  2330. {
  2331. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2332. }
  2333. usleep(50000);
  2334. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2335. }
  2336. void _AutoReturnTimeout()
  2337. {
  2338. PRINTF_FUNC("*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  2339. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2340. {
  2341. ClearDetectPluginFlag();
  2342. }
  2343. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2344. {
  2345. bool needPluginDet = true;
  2346. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  2347. {
  2348. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2349. {
  2350. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  2351. {
  2352. needPluginDet = false;
  2353. break;
  2354. }
  2355. }
  2356. }
  2357. if (needPluginDet)
  2358. DetectPluginStart();
  2359. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHCOMP;
  2360. }
  2361. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  2362. {
  2363. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHFAIL;
  2364. }
  2365. usleep(50000);
  2366. stopChargingChkByCard = false;
  2367. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2368. }
  2369. void _SelfTestTimeout()
  2370. {
  2371. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  2372. {
  2373. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2374. {
  2375. ChargingAlarmProcess(gun_index);
  2376. }
  2377. }
  2378. ShmPsuData->Work_Step = _NO_WORKING;
  2379. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2380. PRINTF_FUNC("Self test timeout. \n");
  2381. }
  2382. void _AuthorizedTimeout()
  2383. {
  2384. //if(IsAuthorizingMode())
  2385. {
  2386. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  2387. isCardScan = false;
  2388. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2389. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHFAIL;
  2390. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  2391. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2392. ClearAuthorizedFlag();
  2393. }
  2394. }
  2395. void _DetectPlugInTimeout()
  2396. {
  2397. PRINTF_FUNC("*********** _DetectPlugInTimeout (%d)*********** \n", _connectionTimeout);
  2398. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2399. ClearDetectPluginFlag();
  2400. usleep(50000);
  2401. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2402. }
  2403. //LW_tpc plug detect
  2404. void _DetectTpcPlugInTimeout()
  2405. {
  2406. PRINTF_FUNC("// _Detect CHAdeMO_TPC PlugInTimeout(60 sec) // \n");
  2407. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE;
  2408. }
  2409. void _DetectEvChargingEnableTimeout(byte gunIndex)
  2410. {
  2411. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2412. {
  2413. if(!isEvGunLocked(gunIndex))
  2414. {
  2415. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  2416. }
  2417. }
  2418. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2419. {
  2420. if(!isEvGunLocked(gunIndex))
  2421. {
  2422. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  2423. }
  2424. }
  2425. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  2426. {
  2427. if(!isEvGunLocked(gunIndex))
  2428. {
  2429. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  2430. }
  2431. }
  2432. ChargingTerminalProcess(gunIndex);
  2433. _AutoReturnTimeout();
  2434. }
  2435. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  2436. {
  2437. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  2438. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  2439. {
  2440. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  2441. _AutoReturnTimeout();
  2442. }
  2443. }
  2444. void _PrepareTimeout(byte gunIndex)
  2445. {
  2446. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  2447. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  2448. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  2449. _AutoReturnTimeout();
  2450. }
  2451. void _CcsPrechargeTimeout(byte gunIndex)
  2452. {
  2453. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  2454. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  2455. }
  2456. //===============================================
  2457. // 卡號驗證
  2458. //===============================================
  2459. void AuthorizingStart()
  2460. {
  2461. ocpp_set_authorizeReq_cmd(YES);
  2462. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  2463. }
  2464. void ClearAuthorizedFlag()
  2465. {
  2466. ocpp_set_authorizeConf_cmd(NO);
  2467. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  2468. }
  2469. bool isAuthorizedComplete()
  2470. {
  2471. return ocpp_get_authorize_conf();
  2472. }
  2473. bool IsAuthorizingMode()
  2474. {
  2475. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  2476. return false;
  2477. return true;
  2478. }
  2479. //===============================================
  2480. // 紀錄 Alarm Code
  2481. //===============================================
  2482. void RecordAlarmCode(byte gunIndex, char *code)
  2483. {
  2484. if (strcmp(code, "011018") == EQUAL) { ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = YES; return; }
  2485. else if (strcmp(code, "011019") == EQUAL) { ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = YES; return; }
  2486. else if (strcmp(code, "011020") == EQUAL) { ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = YES; return; }
  2487. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) == EQUAL)
  2488. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  2489. if (strcmp(code, "012234") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = YES;
  2490. else if (strcmp(code, "012235") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  2491. else if (strcmp(code, "012236") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = YES;
  2492. else if (strcmp(code, "012288") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = YES;
  2493. else if (strcmp(code, "012289") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = YES;
  2494. else if (strcmp(code, "012290") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = YES;
  2495. else if (strcmp(code, "012229") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = YES;
  2496. else if (strcmp(code, "012230") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = YES;
  2497. else if (strcmp(code, "012231") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = YES;
  2498. else if (strcmp(code, "012296") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = YES;
  2499. else if (strcmp(code, "012297") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = YES;
  2500. else if (strcmp(code, "012298") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = YES;
  2501. else if (strcmp(code, "011011") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = YES;
  2502. else if (strcmp(code, "011013") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = YES;
  2503. else if (strcmp(code, "011015") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = YES;
  2504. else if (strcmp(code, "011012") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = YES;
  2505. else if (strcmp(code, "011014") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = YES;
  2506. else if (strcmp(code, "011016") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = YES;
  2507. else if (strcmp(code, "012320") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = YES;
  2508. else if (strcmp(code, "012321") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = YES;
  2509. else if (strcmp(code, "012322") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = YES;
  2510. else if (strcmp(code, "011039") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault = YES;
  2511. else if (strcmp(code, "011040") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault = YES;
  2512. else if (strcmp(code, "023984") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.StopByEvseCondition = YES;
  2513. }
  2514. void ResetChargerAlarmCode(byte gunIndex, char *code)
  2515. {
  2516. if (strcmp(code, "012229") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP)
  2517. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  2518. else if (strcmp(code, "012230") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP)
  2519. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  2520. else if (strcmp(code, "012231") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP)
  2521. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  2522. else if (strcmp(code, "011011") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault)
  2523. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  2524. else if (strcmp(code, "011013") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault)
  2525. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  2526. else if (strcmp(code, "011015") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault)
  2527. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  2528. else if (strcmp(code, "011012") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault)
  2529. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  2530. else if (strcmp(code, "011014") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault)
  2531. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  2532. else if (strcmp(code, "011016") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault)
  2533. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  2534. else if (strcmp(code, "011018") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail)
  2535. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  2536. else if (strcmp(code, "011019") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail)
  2537. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  2538. else if (strcmp(code, "011020") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail)
  2539. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  2540. else if (strcmp(code, "012320") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP)
  2541. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = NO;
  2542. else if (strcmp(code, "012321") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP)
  2543. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = NO;
  2544. else if (strcmp(code, "012322") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP)
  2545. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = NO;
  2546. else if (strcmp(code, "011039") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault)
  2547. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault = NO;
  2548. else if (strcmp(code, "011040") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault)
  2549. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault = NO;
  2550. else if (strcmp(code, "023984") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.StopByEvseCondition)
  2551. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.StopByEvseCondition = NO;
  2552. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6) == EQUAL)
  2553. {
  2554. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2555. }
  2556. }
  2557. void ReleaseSysAlarmCode(byte gunIndex)
  2558. {
  2559. // 回 idle 後主要清除 GFD Trip、UVP、OVP、GFD Warning,PSU fail
  2560. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2561. {
  2562. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012251", 6) == EQUAL ||
  2563. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012252", 6) == EQUAL ||
  2564. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012324", 6) == EQUAL ||
  2565. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012325", 6) == EQUAL ||
  2566. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012267", 6) == EQUAL ||
  2567. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012346", 6) == EQUAL)
  2568. {
  2569. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2570. }
  2571. }
  2572. if (gunIndex == 0)
  2573. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_1 = NO;
  2574. else if (gunIndex == 1)
  2575. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_2 = NO;
  2576. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2577. {
  2578. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  2579. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  2580. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP = NO;
  2581. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  2582. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = NO;
  2583. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2584. {
  2585. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL ||
  2586. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL ||
  2587. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012217", 6) == EQUAL ||
  2588. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012296", 6) == EQUAL)
  2589. {
  2590. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2591. }
  2592. }
  2593. }
  2594. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  2595. {
  2596. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  2597. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  2598. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP = NO;
  2599. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  2600. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = NO;
  2601. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2602. {
  2603. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL ||
  2604. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL ||
  2605. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012219", 6) == EQUAL ||
  2606. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012297", 6) == EQUAL)
  2607. {
  2608. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2609. }
  2610. }
  2611. }
  2612. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2613. {
  2614. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  2615. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  2616. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP = NO;
  2617. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  2618. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = NO;
  2619. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2620. {
  2621. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL ||
  2622. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL ||
  2623. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012221", 6) == EQUAL ||
  2624. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012298", 6) == EQUAL)
  2625. {
  2626. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2627. }
  2628. }
  2629. }
  2630. ocpp_clear_errorCode_cmd(gunIndex);
  2631. }
  2632. void NormalStop(byte gunIndex)
  2633. {
  2634. PRINTF_FUNC("NormalStop (%d) \n", gunIndex);
  2635. }
  2636. //===============================================
  2637. // EmergencyStop and Charging Stop
  2638. //===============================================
  2639. void ChargingTerminalProcess(byte gunIndex)
  2640. {
  2641. SetChargerMode(gunIndex, SYS_MODE_TERMINATING);
  2642. }
  2643. void ChargingAlarmProcess(byte gunIndex)
  2644. {
  2645. CheckSystemErrorFunction(gunIndex);
  2646. ocpp_set_errCode_cmd(gunIndex);
  2647. SetChargerMode(gunIndex, SYS_MODE_ALARM);
  2648. }
  2649. void AcChargingTerminalProcess()
  2650. {
  2651. ac_chargingInfo[0]->SystemStatus = SYS_MODE_TERMINATING;
  2652. }
  2653. //void StopChargingProcessByString(byte level)
  2654. //{
  2655. // if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2656. // {
  2657. // ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2658. // }
  2659. //}
  2660. //void ReleaseChargingProcessByString(byte level)
  2661. //{
  2662. // if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2663. // ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2664. //}
  2665. // 一般錯誤停止充電處理函式
  2666. void BoardErrOccurByString(byte index, char *code)
  2667. {
  2668. //byte level = 1;
  2669. if ((chargingInfo[index]->SystemStatus > SYS_MODE_IDLE && chargingInfo[index]->SystemStatus < SYS_MODE_TERMINATING) ||
  2670. (chargingInfo[index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2671. {
  2672. PRINTF_FUNC("BoardErrOccurByString (%d) - (%s) \n", index, code);
  2673. if (strncmp(code, "023730", 6) == EQUAL &&
  2674. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2675. {
  2676. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2677. }
  2678. ChargingAlarmProcess(index);
  2679. }
  2680. //60StopChargingProcessByString(level);
  2681. }
  2682. // 急停狀況的停止充電處理函式
  2683. void EmcOccurThenStopCharging()
  2684. {
  2685. //byte level = 2;
  2686. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen...
  2687. // 其錯誤等級為 2
  2688. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2689. {
  2690. if ((chargingInfo[gun]->SystemStatus > SYS_MODE_IDLE && chargingInfo[gun]->SystemStatus < SYS_MODE_TERMINATING) ||
  2691. (chargingInfo[gun]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[gun]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2692. {
  2693. ChargingAlarmProcess(gun);
  2694. }
  2695. }
  2696. //StopChargingProcessByString(level);
  2697. }
  2698. void ReleaseBoardErrOccurByString(byte index, char *code)
  2699. {
  2700. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  2701. {
  2702. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2703. }
  2704. }
  2705. void ReleaseEmsOccureByString(byte index, char *code)
  2706. {
  2707. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2708. {
  2709. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2710. }
  2711. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2712. {
  2713. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2714. }
  2715. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2716. {
  2717. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2718. }
  2719. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2720. {
  2721. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2722. }
  2723. }
  2724. //===============================================
  2725. // 確認硬體 (按鈕) 狀態
  2726. //===============================================
  2727. bool leftBtnPush = false;
  2728. bool rightBtnPush = false;
  2729. byte _delayDetectSingleBtnCnt = 0;
  2730. bool twoBtnPress = 0;
  2731. bool btnDelayFlag = 0;
  2732. void ButtonTriggerEvent()
  2733. {
  2734. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS &&
  2735. ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS)
  2736. {
  2737. ShmSysConfigAndInfo->SysConfig.ShowInformation = YES;
  2738. twoBtnPress = TRUE;
  2739. }
  2740. else
  2741. {
  2742. ShmSysConfigAndInfo->SysConfig.ShowInformation = NO;
  2743. //Delay for detecting the single btn state after pressing two btn.
  2744. if (twoBtnPress == TRUE && _delayDetectSingleBtnCnt < 100)
  2745. _delayDetectSingleBtnCnt++;
  2746. else
  2747. {
  2748. if (twoBtnPress == TRUE)
  2749. twoBtnPress = FALSE;
  2750. _delayDetectSingleBtnCnt = 0;
  2751. }
  2752. }
  2753. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  2754. {
  2755. if(!leftBtnPush)
  2756. {
  2757. leftBtnPush = BTN_PRESS;
  2758. PRINTF_FUNC("left btn down............................... \n");
  2759. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2760. {
  2761. switch(ac_chargingInfo[0]->SystemStatus)
  2762. {
  2763. case SYS_MODE_IDLE:
  2764. case SYS_MODE_MAINTAIN:
  2765. case SYS_MODE_RESERVATION:
  2766. {
  2767. if(isDetectPlugin())
  2768. {
  2769. _DetectPlugInTimeout();
  2770. StopSystemTimeoutDet();
  2771. }
  2772. }
  2773. break;
  2774. case SYS_MODE_MODE_REASSIGN_CHECK:
  2775. case SYS_MODE_REASSIGN:
  2776. case SYS_MODE_PREPARING:
  2777. case SYS_MODE_PREPARE_FOR_EV:
  2778. case SYS_MODE_PREPARE_FOR_EVSE:
  2779. case SYS_MODE_CCS_PRECHARGE_STEP0:
  2780. case SYS_MODE_CCS_PRECHARGE_STEP1:
  2781. {
  2782. // 取消充電
  2783. AcChargingTerminalProcess();
  2784. }
  2785. break;
  2786. case SYS_MODE_CHARGING:
  2787. {
  2788. if (ShmSysConfigAndInfo->SysConfig.StopChargingByButton == YES ||
  2789. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  2790. ac_chargingInfo[0]->isRemoteStart == NO) ||
  2791. ac_chargingInfo[0]->isRemoteStart)
  2792. {
  2793. // 停止充電
  2794. AcChargingTerminalProcess();
  2795. }
  2796. }
  2797. break;
  2798. case SYS_MODE_COMPLETE:
  2799. {}
  2800. break;
  2801. }
  2802. }
  2803. else
  2804. {
  2805. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2806. {
  2807. case SYS_MODE_IDLE:
  2808. case SYS_MODE_MAINTAIN:
  2809. case SYS_MODE_RESERVATION:
  2810. {
  2811. if(isDetectPlugin())
  2812. {
  2813. _DetectPlugInTimeout();
  2814. StopSystemTimeoutDet();
  2815. btnDelayFlag = TRUE;
  2816. }
  2817. else
  2818. {
  2819. //LW_tpc plug detect
  2820. if (ShmDcCommonData->TpcDetectFunction == YES
  2821. && ShmSysConfigAndInfo->SysConfig.ShowInformation == NO && twoBtnPress == FALSE
  2822. && btnDelayFlag == FALSE)
  2823. {
  2824. if (ShmDcCommonData->_tpcPlugDetectState != _TPC_PLUG_DETECT_STATUS_WAIT)
  2825. {
  2826. PRINTF_FUNC("// Start Detect Plug in // \n");
  2827. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_WAIT;
  2828. GetTimespecFunc(&_tcpDetectStatus_time);
  2829. }
  2830. else
  2831. {
  2832. PRINTF_FUNC("// Stop Detect PlugIn for TPC. // \n");
  2833. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE;
  2834. StopGunInfoTimeoutDet(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2835. }
  2836. }
  2837. }
  2838. }
  2839. break;
  2840. case SYS_MODE_MODE_REASSIGN_CHECK:
  2841. case SYS_MODE_REASSIGN:
  2842. case SYS_MODE_PREPARING:
  2843. case SYS_MODE_PREPARE_FOR_EV:
  2844. case SYS_MODE_PREPARE_FOR_EVSE:
  2845. case SYS_MODE_CCS_PRECHARGE_STEP0:
  2846. case SYS_MODE_CCS_PRECHARGE_STEP1:
  2847. {
  2848. // 按鈕 - 取消充電
  2849. // if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2850. // AcChargingTerminalProcess();
  2851. // else
  2852. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2853. }
  2854. break;
  2855. case SYS_MODE_CHARGING:
  2856. {
  2857. if (ShmSysConfigAndInfo->SysConfig.StopChargingByButton == YES ||
  2858. ShmDcCommonData->enObtainAuthorizbyCCID ||
  2859. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  2860. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->isRemoteStart == NO) ||
  2861. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->isRemoteStart)
  2862. {
  2863. // 按鈕 - 停止充電
  2864. NormalStop(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2865. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2866. }
  2867. }
  2868. break;
  2869. case SYS_MODE_COMPLETE:
  2870. {
  2871. // 回 IDLE
  2872. //PRINTF_FUNC("right btn down.................SYS_MODE_COMPLETE \n");
  2873. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = SYS_MODE_IDLE;
  2874. }
  2875. break;
  2876. }
  2877. }
  2878. }
  2879. }
  2880. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  2881. {
  2882. if(leftBtnPush)
  2883. {
  2884. leftBtnPush = BTN_RELEASE;
  2885. PRINTF_FUNC("left btn up............................... \n");
  2886. //LW_tpc plug detect
  2887. switch (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2888. {
  2889. case SYS_MODE_IDLE:
  2890. btnDelayFlag = FALSE;
  2891. break;
  2892. }
  2893. }
  2894. }
  2895. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  2896. {
  2897. if(!rightBtnPush)
  2898. {
  2899. rightBtnPush = BTN_PRESS;
  2900. PRINTF_FUNC("right btn down .................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2901. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG &&
  2902. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  2903. return;
  2904. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2905. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  2906. ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 == ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2907. {
  2908. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2909. }
  2910. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2911. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  2912. {
  2913. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2914. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2915. }
  2916. else if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2917. {
  2918. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  2919. {
  2920. if (chargingInfo[_index]->SystemStatus != SYS_MODE_BOOTING &&
  2921. chargingInfo[_index]->SystemStatus != SYS_MODE_IDLE &&
  2922. chargingInfo[_index]->SystemStatus != SYS_MODE_RESERVATION)
  2923. {
  2924. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  2925. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2926. return;
  2927. }
  2928. }
  2929. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2930. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2931. }
  2932. else
  2933. {
  2934. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2935. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2936. }
  2937. }
  2938. }
  2939. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  2940. {
  2941. if(rightBtnPush)
  2942. {
  2943. rightBtnPush = BTN_RELEASE;
  2944. PRINTF_FUNC("right btn up............................... \n");
  2945. }
  2946. }
  2947. }
  2948. void ChkPrimaryStatus()
  2949. {
  2950. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2951. {
  2952. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2953. EmcOccurThenStopCharging();
  2954. }
  2955. else
  2956. ReleaseEmsOccureByString(0, "012251");
  2957. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2958. {
  2959. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2960. EmcOccurThenStopCharging();
  2961. }
  2962. else
  2963. ReleaseEmsOccureByString(0, "012238");
  2964. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2965. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2966. else
  2967. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2968. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2969. {
  2970. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2971. EmcOccurThenStopCharging();
  2972. }
  2973. else
  2974. ReleaseEmsOccureByString(0, "012252");
  2975. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2976. {
  2977. EmcOccurThenStopCharging();
  2978. }
  2979. else
  2980. ReleaseEmsOccureByString(0, "012267");
  2981. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU)
  2982. {
  2983. EmcOccurThenStopCharging ();
  2984. }
  2985. else
  2986. ReleaseEmsOccureByString (0,"012346");
  2987. }
  2988. //===============================================
  2989. // 確認各小板偵測的錯誤狀況
  2990. //===============================================
  2991. void CheckSystemErrorFunction(byte index)
  2992. {
  2993. // System
  2994. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  2995. RecordAlarmCode(index, "012251");
  2996. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip)
  2997. RecordAlarmCode(index, "012238");
  2998. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2999. RecordAlarmCode(index, "012252");
  3000. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  3001. RecordAlarmCode(index, "012267");
  3002. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU)
  3003. RecordAlarmCode(index, "012346");
  3004. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  3005. {
  3006. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  3007. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  3008. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  3009. {
  3010. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  3011. {
  3012. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  3013. }
  3014. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  3015. RecordAlarmCode(index, "012203");
  3016. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  3017. RecordAlarmCode(index, "012204");
  3018. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  3019. RecordAlarmCode(index, "012205");
  3020. chargingInfo[index]->StopChargeFlag = YES;
  3021. }
  3022. else
  3023. {
  3024. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  3025. {
  3026. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3027. }
  3028. }
  3029. }
  3030. else
  3031. {
  3032. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  3033. {
  3034. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3035. }
  3036. }
  3037. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  3038. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  3039. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  3040. {
  3041. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  3042. {
  3043. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  3044. }
  3045. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  3046. RecordAlarmCode(index, "012200");
  3047. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  3048. RecordAlarmCode(index, "012201");
  3049. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  3050. RecordAlarmCode(index, "012202");
  3051. chargingInfo[index]->StopChargeFlag = YES;
  3052. }
  3053. else
  3054. {
  3055. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  3056. {
  3057. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3058. }
  3059. }
  3060. }
  3061. //===============================================
  3062. // 確認 GPIO 狀態
  3063. //===============================================
  3064. void gpio_set_value(unsigned int gpio, unsigned int value)
  3065. {
  3066. int fd;
  3067. char buf[MAX_BUF];
  3068. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  3069. fd = open(buf, O_WRONLY);
  3070. if (fd < 0)
  3071. {
  3072. perror("gpio/set-value");
  3073. return;
  3074. }
  3075. if (value)
  3076. write(fd, "1", 2);
  3077. else
  3078. write(fd, "0", 2);
  3079. close(fd);
  3080. }
  3081. int gpio_get_value(unsigned int gpio, unsigned int *value)
  3082. {
  3083. int fd;
  3084. char buf[MAX_BUF];
  3085. char ch;
  3086. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  3087. fd = open(buf, O_RDONLY);
  3088. if (fd < 0) {
  3089. perror("gpio/get-value");
  3090. return fd;
  3091. }
  3092. read(fd, &ch, 1);
  3093. if (ch != '0') {
  3094. *value = 1;
  3095. } else {
  3096. *value = 0;
  3097. }
  3098. close(fd);
  3099. return 0;
  3100. }
  3101. void CheckGunTypeFromHw()
  3102. {
  3103. int pinIn[4] = { 22, 23, 44, 45 };
  3104. unsigned int gpioValue = 0;
  3105. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  3106. gpio_get_value(pinIn[i], &gpioValue);
  3107. {
  3108. switch (pinIn[i])
  3109. {
  3110. case 22:
  3111. bd1_1_status = gpioValue;
  3112. break;
  3113. case 23:
  3114. bd1_2_status = gpioValue;
  3115. break;
  3116. case 44:
  3117. bd0_1_status = gpioValue;
  3118. break;
  3119. case 45:
  3120. bd0_2_status = gpioValue;
  3121. break;
  3122. }
  3123. }
  3124. }
  3125. }
  3126. // Bypass useless functions
  3127. void CheckGpioInStatus()
  3128. {
  3129. int pinIn[2] = { 27, 47 };
  3130. unsigned int gpioValue = 0;
  3131. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  3132. {
  3133. gpio_get_value(pinIn[i], &gpioValue);
  3134. if (gpioValue == 0x01)
  3135. {
  3136. switch(pinIn[i])
  3137. {
  3138. // 小板緊急停止
  3139. case 47:
  3140. {
  3141. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3142. {
  3143. if (chargingInfo[i]->slotsIndex == 1)
  3144. {
  3145. // if (chargingInfo[i]->Type == _Type_Chademo)
  3146. // BoardErrOccurByString(i, "023730");
  3147. // else if (chargingInfo[i]->Type == _Type_CCS)
  3148. // BoardErrOccurByString(i, "013627");
  3149. break;
  3150. }
  3151. }
  3152. }
  3153. break;
  3154. case 27:
  3155. {
  3156. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3157. {
  3158. if (chargingInfo[i]->slotsIndex == 3)
  3159. {
  3160. // if (chargingInfo[i]->Type == _Type_Chademo)
  3161. // BoardErrOccurByString(i, "023730");
  3162. // else if (chargingInfo[i]->Type == _Type_CCS)
  3163. // BoardErrOccurByString(i, "013627");
  3164. break;
  3165. }
  3166. }
  3167. }
  3168. break;
  3169. }
  3170. }
  3171. else
  3172. {
  3173. switch (pinIn[i])
  3174. {
  3175. // 小板解除緊急停止
  3176. case 47:
  3177. {
  3178. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3179. {
  3180. if (chargingInfo[i]->slotsIndex == 1)
  3181. {
  3182. if (chargingInfo[i]->Type == _Type_Chademo)
  3183. ReleaseBoardErrOccurByString(i, "023730");
  3184. // else if (chargingInfo[i]->Type == _Type_CCS)
  3185. // ReleaseBoardErrOccurByString(i, "013627");
  3186. break;
  3187. }
  3188. }
  3189. }
  3190. break;
  3191. case 27:
  3192. {
  3193. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3194. {
  3195. if (chargingInfo[i]->slotsIndex == 3)
  3196. {
  3197. if (chargingInfo[i]->Type == _Type_Chademo)
  3198. ReleaseBoardErrOccurByString(i, "023730");
  3199. // else if (chargingInfo[i]->Type == _Type_CCS)
  3200. // ReleaseBoardErrOccurByString(i, "013627");
  3201. break;
  3202. }
  3203. }
  3204. }
  3205. break;
  3206. }
  3207. }
  3208. }
  3209. }
  3210. //===============================================
  3211. // Main process
  3212. //===============================================
  3213. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  3214. {
  3215. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  3216. {
  3217. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  3218. {
  3219. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  3220. return true;
  3221. }
  3222. }
  3223. for (byte index = 0; index < CCS_QUANTITY; index++)
  3224. {
  3225. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  3226. {
  3227. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  3228. return true;
  3229. }
  3230. }
  3231. for (byte index = 0; index < GB_QUANTITY; index++)
  3232. {
  3233. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  3234. {
  3235. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  3236. return true;
  3237. }
  3238. }
  3239. return false;
  3240. }
  3241. // 檢查 Byte 中某個 Bit 的值
  3242. // _byte : 欲改變的 byte
  3243. // _bit : 該 byte 的第幾個 bit
  3244. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  3245. {
  3246. return ( _byte & mask_table[_bit] ) != 0x00;
  3247. }
  3248. // 設定 Byte 中某個 Bit的值
  3249. // _byte : 欲改變的 byte
  3250. // _bit : 該 byte 的第幾個 bit
  3251. // value : 修改的值為 0 or 1
  3252. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  3253. {
  3254. if(value == 1)
  3255. *_byte |= (1 << _bit);
  3256. else if (value == 0)
  3257. *_byte ^= (1 << _bit);
  3258. }
  3259. void GetCcidAuthPossible(bool *idleReq, byte *idleIndex)
  3260. {
  3261. for (byte gun_index = 0;
  3262. gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  3263. gun_index ++)
  3264. {
  3265. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_PREPARE_FOR_EV &&
  3266. ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  3267. {
  3268. *idleIndex = gun_index;
  3269. *idleReq = true;
  3270. break;
  3271. }
  3272. }
  3273. }
  3274. void UserScanFunction()
  3275. {
  3276. bool idleReq = false;
  3277. unsigned char idleIndex = 255;
  3278. //unsigned char stopReq = 255;
  3279. bool hasGunUsing = false;
  3280. bool isReservedCard = false;
  3281. // 當前非驗證的狀態
  3282. if(!IsAuthorizingMode())
  3283. {
  3284. // 先判斷現在是否可以提供刷卡
  3285. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  3286. // 2. 停止充電
  3287. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX ||
  3288. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_EMC)
  3289. {
  3290. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3291. return;
  3292. }
  3293. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  3294. {
  3295. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  3296. ac_chargingInfo[0]->SystemStatus != SYS_MODE_ALARM)
  3297. {
  3298. // 如果選 AC 槍
  3299. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  3300. ac_chargingInfo[0]->SystemStatus < SYS_MODE_ALARM)
  3301. {
  3302. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  3303. {
  3304. char value[32];
  3305. PRINTF_FUNC("ac stop charging \n");
  3306. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  3307. ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  3308. memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId, ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  3309. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  3310. {
  3311. AcChargingTerminalProcess();
  3312. }
  3313. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3314. }
  3315. }
  3316. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE ||
  3317. ac_chargingInfo[0]->SystemStatus == SYS_MODE_RESERVATION)
  3318. {
  3319. idleReq = true;
  3320. }
  3321. }
  3322. else
  3323. {
  3324. if (IsGunUsing(ShmSysConfigAndInfo->SysInfo.CurGunSelected))
  3325. {
  3326. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_CHARGING)
  3327. {
  3328. char value[32];
  3329. PRINTF_FUNC("stop charging \n");
  3330. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  3331. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  3332. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  3333. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  3334. // 同一張卡直接停掉
  3335. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  3336. {
  3337. NormalStop(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3338. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3339. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3340. }
  3341. else
  3342. {
  3343. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  3344. {
  3345. GetCcidAuthPossible(&idleReq, &idleIndex);
  3346. }
  3347. if (!idleReq)
  3348. {
  3349. PRINTF_FUNC ( "_LCM_AUTHORIZ_FAIL \n" );
  3350. stopChargingChkByCard = true;
  3351. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  3352. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3353. }
  3354. }
  3355. }
  3356. else
  3357. {
  3358. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  3359. {
  3360. GetCcidAuthPossible(&idleReq, &idleIndex);
  3361. }
  3362. }
  3363. }
  3364. // else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE ||
  3365. // chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  3366. // {
  3367. // idleIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  3368. // idleReq = true;
  3369. // }
  3370. else
  3371. {
  3372. if (ShmDcCommonData->enObtainAuthorizbyCCID &&
  3373. (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZING ||
  3374. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZ_COMP ||
  3375. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZ_FAIL))
  3376. return;
  3377. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3378. {
  3379. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  3380. {
  3381. idleIndex = gun_index;
  3382. idleReq = true;
  3383. }
  3384. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3385. {
  3386. idleReq = true;
  3387. }
  3388. else
  3389. {
  3390. hasGunUsing = IsGunUsing(gun_index);
  3391. }
  3392. }
  3393. // 壁掛不給充 - 已經有至少一把槍在充電
  3394. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES &&
  3395. hasGunUsing)
  3396. {
  3397. PRINTF_FUNC("DW Serial not support dual charging. \n");
  3398. idleReq = false;
  3399. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3400. }
  3401. }
  3402. }
  3403. if (idleReq)
  3404. {
  3405. // if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  3406. // stopReq != 255 &&
  3407. // ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  3408. // {
  3409. // idleReq = false;
  3410. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3411. // }
  3412. // else
  3413. {
  3414. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gun_index++)
  3415. {
  3416. if (ac_chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3417. {
  3418. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3419. (char *) ShmDcCommonData->_reserved_ac_UserId [gun_index] ) == EQUAL)
  3420. {
  3421. isReservedCard = true;
  3422. // 驗證通過
  3423. ac_chargingInfo[gun_index]->ReservedStartFlag = YES;
  3424. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3425. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3426. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3427. break;
  3428. }
  3429. }
  3430. }
  3431. if (!isReservedCard)
  3432. {
  3433. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3434. {
  3435. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3436. {
  3437. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId,
  3438. (char *)ShmDcCommonData->_reserved_UserId[gun_index]) == EQUAL)
  3439. {
  3440. isReservedCard = true;
  3441. // 驗證通過
  3442. chargingInfo[gun_index]->ReservedStartFlag = YES;
  3443. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3444. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3445. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3446. break;
  3447. }
  3448. }
  3449. }
  3450. }
  3451. if (!isReservedCard &&
  3452. ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  3453. (idleIndex != 255 && (chargingInfo[idleIndex]->SystemStatus == SYS_MODE_IDLE || chargingInfo[idleIndex]->SystemStatus == SYS_MODE_PREPARE_FOR_EV))))
  3454. {
  3455. if (ShmDcCommonData->_CcidAuthProcessStep != _CCID_AUTH)
  3456. {
  3457. PRINTF_FUNC("Start Authorizing...UserId = %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3458. // LCM => Authorizing
  3459. if (ShmDcCommonData->enObtainAuthorizbyCCID &&
  3460. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != DEFAULT_AC_INDEX)
  3461. {
  3462. ShmSysConfigAndInfo->SysInfo.CurGunSelected = idleIndex;
  3463. }
  3464. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  3465. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTH;
  3466. // 進入確認卡號狀態
  3467. AuthorizingStart();
  3468. }
  3469. }
  3470. else
  3471. {
  3472. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3473. }
  3474. }
  3475. }
  3476. else
  3477. {
  3478. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3479. }
  3480. }
  3481. }
  3482. }
  3483. unsigned char isModeChange(unsigned char gun_index)
  3484. {
  3485. unsigned char result = NO;
  3486. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  3487. {
  3488. result = YES;
  3489. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  3490. }
  3491. return result;
  3492. }
  3493. void ScannerCardProcess()
  3494. {
  3495. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL &&
  3496. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3497. {
  3498. isCardScan = true;
  3499. // 處理刷卡及驗證卡號的動作
  3500. UserScanFunction();
  3501. }
  3502. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3503. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3504. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  3505. {
  3506. StartSystemTimeoutDet(Timeout_Authorizing);
  3507. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3508. {
  3509. // 白名單驗證
  3510. for (int i = 0; i < 10; i++)
  3511. {
  3512. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  3513. {
  3514. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  3515. {
  3516. PRINTF_FUNC("White Card Pass (%s)... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3517. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3518. ClearAuthorizedFlag();
  3519. break;
  3520. }
  3521. }
  3522. }
  3523. }
  3524. // 確認驗證卡號完成沒
  3525. if (isAuthorizedComplete() || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3526. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3527. {
  3528. StopSystemTimeoutDet();
  3529. // 判斷後台回覆狀態
  3530. if(ocpp_chk_authrization_cmd() || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3531. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3532. {
  3533. // LCM => Authorize complete
  3534. PRINTF_FUNC("_LCM_AUTHORIZ_COMP \n");
  3535. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3536. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  3537. }
  3538. else
  3539. {
  3540. // LCM => Authorize fail
  3541. PRINTF_FUNC("_LCM_AUTHORIZ_FAIL \n");
  3542. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3543. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3544. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  3545. }
  3546. ClearAuthorizedFlag();
  3547. }
  3548. }
  3549. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  3550. {
  3551. StartSystemTimeoutDet(Timeout_VerifyFail);
  3552. isCardScan = false;
  3553. }
  3554. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  3555. {
  3556. StartSystemTimeoutDet(Timeout_VerifyComp);
  3557. }
  3558. else if(ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  3559. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  3560. {
  3561. StartSystemTimeoutDet(Timeout_WaitPlug);
  3562. }
  3563. else
  3564. {
  3565. isCardScan = false;
  3566. }
  3567. }
  3568. bool AddGunInfoByConnector(byte typeValue, byte slots)
  3569. {
  3570. bool result = true;
  3571. switch (typeValue)
  3572. {
  3573. case '0': // none
  3574. break;
  3575. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  3576. break;
  3577. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  3578. break;
  3579. case '3': // IEC 62196-2 Type 2 Plug
  3580. case '4': // IEC 62196-2 Type 2 Socket
  3581. if (AC_QUANTITY > _ac_Index)
  3582. {
  3583. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  3584. // AC 固定 index
  3585. ac_chargingInfo[_acgunIndex]->Index = DEFAULT_AC_INDEX;
  3586. ac_chargingInfo[_acgunIndex]->ReservationId = -1;
  3587. ac_chargingInfo[_acgunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3588. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  3589. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  3590. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3591. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3592. _ac_Index++;
  3593. _acgunIndex++;
  3594. }
  3595. else
  3596. result = false;
  3597. break;
  3598. case '5': // GB/T AC Plug
  3599. break;
  3600. case '6': // GB/T AC Socket
  3601. break;
  3602. case 'J': // CHAdeMO - 60A
  3603. case 'K': // CHAdeMO - 200A / 500V
  3604. case 'L': // CHAdeMO - 80A
  3605. case 'S': // CHAdeMO - 125A (boost : 200A)
  3606. {
  3607. if (CHAdeMO_QUANTITY > _chademoIndex)
  3608. {
  3609. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  3610. chargingInfo[_gunIndex]->Index = _gunIndex;
  3611. chargingInfo[_gunIndex]->ReservationId = -1;
  3612. chargingInfo[_gunIndex]->slotsIndex = slots;
  3613. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3614. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  3615. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  3616. chargingInfo[_gunIndex]->IsAvailable = YES;
  3617. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3618. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3619. chargingInfo[_gunIndex]->ModelType = typeValue;
  3620. ShmDcCommonData->TpcDetectFunction = YES; //LW_tpc plug detect
  3621. _chademoIndex++;
  3622. _gunIndex++;
  3623. }
  3624. else
  3625. result = false;
  3626. }
  3627. break;
  3628. case 'U': // Natural cooling CCS1
  3629. case 'E': // Natural cooling CCS2
  3630. case 'T': // Rema CCS1 - 300A
  3631. case 'D': // Rema CCS2 - 300A
  3632. case 'M': // CCS2 - 80A
  3633. case 'N': // CCS1 - 80A
  3634. case 'R': // CCS2 - 250A
  3635. case 'V' : // Liquid cooling CCS1 - 500A
  3636. case 'F' : // Liquid cooling CCS2 - 500A
  3637. case 'Y' : // CCS1 - 150A
  3638. case 'Z' : // CCS2 - 150A
  3639. {
  3640. if (CCS_QUANTITY > _ccsIndex)
  3641. {
  3642. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  3643. chargingInfo[_gunIndex]->Index = _gunIndex;
  3644. chargingInfo[_gunIndex]->ReservationId = -1;
  3645. chargingInfo[_gunIndex]->slotsIndex = slots;
  3646. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3647. chargingInfo[_gunIndex]->Type = _Type_CCS;
  3648. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  3649. chargingInfo[_gunIndex]->IsAvailable = YES;
  3650. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3651. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3652. chargingInfo[_gunIndex]->ModelType = typeValue;
  3653. // 現階段預設為走 DIN70121
  3654. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  3655. if (typeValue == 'U' || typeValue == 'T' || typeValue == 'N' || typeValue == 'V' || typeValue == 'Y')
  3656. ShmDcCommonData->CcsTypeSaved[_gunIndex] = _CCS_TYPE_CCS1;
  3657. else if (typeValue == 'E' || typeValue == 'D' || typeValue == 'M' || typeValue == 'R' || typeValue == 'F' || typeValue == 'Z')
  3658. ShmDcCommonData->CcsTypeSaved[_gunIndex] = _CCS_TYPE_CCS2;
  3659. _ccsIndex++;
  3660. _gunIndex++;
  3661. }
  3662. else
  3663. result = false;
  3664. }
  3665. break;
  3666. case 'G': // GBT DC
  3667. case 'B':
  3668. {
  3669. if (GB_QUANTITY > _gb_Index)
  3670. {
  3671. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  3672. chargingInfo[_gunIndex]->Index = _gunIndex;
  3673. chargingInfo[_gunIndex]->ReservationId = -1;
  3674. chargingInfo[_gunIndex]->slotsIndex = slots;
  3675. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3676. chargingInfo[_gunIndex]->Type = _Type_GB;
  3677. chargingInfo[_gunIndex]->type_index = _gb_Index;
  3678. chargingInfo[_gunIndex]->IsAvailable = YES;
  3679. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3680. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3681. chargingInfo[_gunIndex]->ModelType = typeValue;
  3682. _gb_Index++;
  3683. _gunIndex++;
  3684. }
  3685. else
  3686. result = false;
  3687. }
  3688. break;
  3689. }
  3690. return result;
  3691. }
  3692. bool CheckConnectorTypeStatus()
  3693. {
  3694. bool result = true;
  3695. // 硬體 0 1 => CCS,1 0 => Chademo,1 1 => GB
  3696. //CheckFwSlotSta.tusLog();
  3697. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  3698. {
  3699. byte slots = 1;
  3700. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  3701. {
  3702. if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  3703. {
  3704. return false;
  3705. }
  3706. slots++;
  3707. }
  3708. // AC index 接在 DC 後面
  3709. // if (_ac_Index > 0)
  3710. // ac_chargingInfo[0]->Index += _gunIndex;
  3711. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3712. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  3713. PRINTF_FUNC("Charger Count => DC-Count = %d, AC-Count = %d \n",
  3714. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  3715. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  3716. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  3717. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  3718. result = false;
  3719. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3720. {
  3721. bool isFind = false;
  3722. if (chargingInfo[0]->Type == _Type_Chademo)
  3723. {
  3724. if ((bd0_1_status == 0 && bd0_2_status == 1) ||
  3725. (bd1_1_status == 0 && bd1_2_status == 1))
  3726. {
  3727. isFind = true;
  3728. }
  3729. }
  3730. else if (chargingInfo[0]->Type == _Type_CCS)
  3731. {
  3732. if ((bd0_1_status == 1 && bd0_2_status == 0) ||
  3733. (bd1_1_status == 1 && bd1_2_status == 0))
  3734. {
  3735. isFind = true;
  3736. }
  3737. }
  3738. else if (chargingInfo[0]->Type == _Type_GB)
  3739. {
  3740. if ((bd0_1_status == 1 && bd0_2_status == 1) ||
  3741. (bd1_1_status == 1 && bd1_2_status == 1))
  3742. {
  3743. isFind = true;
  3744. }
  3745. }
  3746. if (isFind)
  3747. {
  3748. chargingInfo[0]->Evboard_id = 0x01;
  3749. CheckHwSlotStatusLog(0);
  3750. }
  3751. else
  3752. result = false;
  3753. }
  3754. else
  3755. {
  3756. char type = NO_DEFINE;
  3757. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  3758. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  3759. {
  3760. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  3761. {
  3762. PRINTF_FUNC("HW Conn %d (Chademo) \n", gunIndex);
  3763. // 與硬體相同 type : Chademo
  3764. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3765. {
  3766. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3767. }
  3768. }
  3769. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  3770. {
  3771. PRINTF_FUNC("HW Conn %d (CCS) \n", gunIndex);
  3772. // 與硬體相同 type : CCS
  3773. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  3774. {
  3775. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3776. }
  3777. }
  3778. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  3779. {
  3780. PRINTF_FUNC("HW Conn %d (GB) \n", gunIndex);
  3781. // 與硬體相同 type : GB
  3782. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3783. {
  3784. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3785. }
  3786. }
  3787. else if (gunIndex == 0)
  3788. {
  3789. PRINTF_FUNC("HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n",
  3790. gunIndex, bd0_1_status, bd0_2_status);
  3791. }
  3792. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  3793. {
  3794. PRINTF_FUNC("HW Conn %d (Chademo) \n", gunIndex);
  3795. // 與硬體相同 type : Chademo
  3796. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3797. {
  3798. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3799. }
  3800. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3801. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3802. }
  3803. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  3804. {
  3805. PRINTF_FUNC("HW Conn %d (CCS) \n", gunIndex);
  3806. // 與硬體相同 type : CCS
  3807. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  3808. {
  3809. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3810. }
  3811. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3812. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3813. }
  3814. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  3815. {
  3816. PRINTF_FUNC("HW Conn %d (GB) \n", gunIndex);
  3817. // 與硬體相同 type : GB
  3818. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3819. {
  3820. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3821. }
  3822. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3823. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3824. }
  3825. else if (gunIndex == 1)
  3826. {
  3827. PRINTF_FUNC("HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n",
  3828. gunIndex, bd1_1_status, bd1_2_status);
  3829. }
  3830. CheckHwSlotStatusLog(gunIndex);
  3831. if (type == NO_DEFINE)
  3832. type = chargingInfo[gunIndex]->Type;
  3833. else
  3834. {
  3835. // 雙槍都是同一個 type
  3836. if (type == chargingInfo[gunIndex]->Type)
  3837. ShmDcCommonData->SysGunAreSameType = YES;
  3838. }
  3839. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  3840. result = false;
  3841. }
  3842. }
  3843. }
  3844. else
  3845. {
  3846. // Module Name 不正確 - 告警
  3847. result = false;
  3848. }
  3849. return result;
  3850. }
  3851. void KillTask()
  3852. {
  3853. ChangeLcmByIndex(_LCM_FIX);
  3854. system("killall Module_EventLogging");
  3855. system("killall Module_PrimaryComm");
  3856. system("killall Module_EvComm");
  3857. system("killall Module_InternalComm");
  3858. system("killall Module_PsuComm");
  3859. system("killall Module_Module_SmartBox");
  3860. system("killall Module_4g &");
  3861. system("killall Module_Wifi &");
  3862. }
  3863. void KillTaskExceptDetectTask()
  3864. {
  3865. ChangeLcmByIndex(_LCM_FIX);
  3866. sleep(3);
  3867. system("killall Module_EvComm");
  3868. //system("killall Module_InternalComm");
  3869. system("killall Module_PsuComm");
  3870. system("killall Module_Module_SmartBox");
  3871. system("killall Module_4g &");
  3872. system("killall Module_Wifi &");
  3873. }
  3874. void KillAllTask()
  3875. {
  3876. ChangeLcmByIndex(_LCM_FIX);
  3877. system("killall Module_EventLogging");
  3878. system("killall Module_PrimaryComm");
  3879. system("killall Module_EvComm");
  3880. system("killall Module_LcmControl");
  3881. system("killall Module_InternalComm");
  3882. system("killall Module_PsuComm");
  3883. system("killall Module_Module_SmartBox");
  3884. system("killall OcppBackend &");
  3885. system("killall Module_4g &");
  3886. system("killall Module_Wifi &");
  3887. system("killall Module_DcMeter &");
  3888. }
  3889. void LogUpgradeType(unsigned int type)
  3890. {
  3891. switch (type)
  3892. {
  3893. case _UPDATE_TYPE_CSU_UBOOT: PRINTF_FUNC("CSU bootloader (uboot) \n"); break;
  3894. case _UPDATE_TYPE_CSU_DTB: PRINTF_FUNC("CSU kernel configuration (dtb) \n"); break;
  3895. case _UPDATE_TYPE_CSU_ZIMAGE: PRINTF_FUNC("CSU kernel image (zImage) \n"); break;
  3896. case _UPDATE_TYPE_CSU_RFILES: PRINTF_FUNC("CSU root file system (ramdisk.gz) \n"); break;
  3897. case _UPDATE_TYPE_CSU_CONFIG: PRINTF_FUNC("CSU user configuration (bin) \n"); break;
  3898. case _UPDATE_TYPE_CCS_UBOOT: PRINTF_FUNC("CCS board bootloader (uboot) \n"); break;
  3899. case _UPDATE_TYPE_CCS_DTB: PRINTF_FUNC("CCS board kernel configuration (dtb) \n"); break;
  3900. case _UPDATE_TYPE_CCS_ZIMAGE: PRINTF_FUNC("CCS board kernel image (zImage) \n"); break;
  3901. case _UPDATE_TYPE_CCS_RFILES: PRINTF_FUNC("CCS board file system (ramdisk.gz) \n"); break;
  3902. case _UPDATE_TYPE_DCM_ST407: PRINTF_FUNC("DCM 407 primary controller \n"); break;
  3903. case _UPDATE_TYPE_RELAY_CTR: PRINTF_FUNC("Relay control board \n"); break;
  3904. case _UPDATE_TYPE_FAN_CTR: PRINTF_FUNC("Fan control board \n"); break;
  3905. case _UPDATE_TYPE_AC_WLMOUNT_CTR: PRINTF_FUNC("AC wall-mount (low-end) controller \n"); break;
  3906. case _UPDATE_TYPE_LED_CTR: PRINTF_FUNC("LED control board \n"); break;
  3907. case _UPDATE_TYPE_CHADEMO: PRINTF_FUNC("CAHdeMO board \n"); break;
  3908. case _UPDATE_TYPE_GBT: PRINTF_FUNC("GBT board \n"); break;
  3909. }
  3910. }
  3911. int CheckUpdateProcess(void)
  3912. {
  3913. //bool isPass = true;
  3914. uint8_t retSucc = 0;
  3915. uint8_t retFail = 0;
  3916. byte index = 0;
  3917. byte target = 0;
  3918. char new_str[256];
  3919. unsigned char *ptr = NULL;
  3920. int fd = 0;
  3921. int CanFd = 0;
  3922. int uartFd = 0;
  3923. unsigned int Type = 0;
  3924. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  3925. DIR *d;
  3926. struct dirent *dir;
  3927. byte result = PASS;
  3928. char Buf[256];
  3929. d = opendir("/mnt/");
  3930. if (d)
  3931. {
  3932. while ((dir = readdir(d)) != NULL)
  3933. {
  3934. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
  3935. {
  3936. continue;
  3937. }
  3938. memset(new_str, 0x00, sizeof(new_str));
  3939. strcat(new_str, "/mnt/");
  3940. strcat(new_str, dir->d_name);
  3941. PRINTF_FUNC("%s \n", new_str);
  3942. fd = open(new_str, O_RDONLY);
  3943. if (fd < 0)
  3944. {
  3945. close(fd);
  3946. result = FAIL;
  3947. sleep(1);
  3948. continue;
  3949. }
  3950. ptr = malloc(MaxLen); //-48 is take out the header
  3951. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  3952. //get the image length
  3953. ImageLen = read(fd, ptr, MaxLen);
  3954. bool isModelNameFail = false;
  3955. // DS-----J0E-2PH
  3956. if((ShmSysConfigAndInfo->SysConfig.ModelName[0] != ptr[0]) ||
  3957. (ShmSysConfigAndInfo->SysConfig.ModelName[1] != ptr[1]) ||
  3958. (ShmSysConfigAndInfo->SysConfig.ModelName[7] != ptr[7]) ||
  3959. (ShmSysConfigAndInfo->SysConfig.ModelName[8] != ptr[8]) ||
  3960. (ShmSysConfigAndInfo->SysConfig.ModelName[9] != ptr[9]) ||
  3961. (ShmSysConfigAndInfo->SysConfig.ModelName[11] != ptr[11]) ||
  3962. (ShmSysConfigAndInfo->SysConfig.ModelName[12] != ptr[12]) ||
  3963. (ShmSysConfigAndInfo->SysConfig.ModelName[13] != ptr[13]))
  3964. {
  3965. result = MODELNAME_FAIL;
  3966. isModelNameFail = true;
  3967. }
  3968. if (isModelNameFail)
  3969. {
  3970. close(fd);
  3971. continue;
  3972. }
  3973. PRINTF_FUNC("model name check pass. \n");
  3974. if (ImageLen > 20) {
  3975. Type = (((unsigned int)ptr[16]) << 24 |
  3976. ((unsigned int)ptr[17]) << 16 |
  3977. ((unsigned int)ptr[18]) << 8 |
  3978. ((unsigned int)ptr[19]));
  3979. LogUpgradeType(Type);
  3980. PRINTF_FUNC("Upgrade Status : PASS : %d, FAIL : %d \n", retSucc, retFail);
  3981. free(ptr);
  3982. switch (Type)
  3983. {
  3984. case _UPDATE_TYPE_CSU_UBOOT:
  3985. case _UPDATE_TYPE_CSU_DTB:
  3986. case _UPDATE_TYPE_CSU_ZIMAGE:
  3987. case _UPDATE_TYPE_CSU_RFILES:
  3988. case _UPDATE_TYPE_CSU_CONFIG:
  3989. system("echo 3 > /proc/sys/vm/drop_caches");
  3990. sleep(2);
  3991. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3992. retSucc++;
  3993. else
  3994. {
  3995. PRINTF_FUNC("Upgrade CSU Failed (%d) \n", Type);
  3996. retFail++;
  3997. }
  3998. memset(Buf, 0, sizeof(Buf));
  3999. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4000. system(Buf);
  4001. break;
  4002. case _UPDATE_TYPE_CCS_UBOOT:
  4003. case _UPDATE_TYPE_CCS_DTB:
  4004. case _UPDATE_TYPE_CCS_ZIMAGE:
  4005. case _UPDATE_TYPE_CCS_RFILES:
  4006. CanFd = InitCanBus();
  4007. if (CanFd > 0)
  4008. {
  4009. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  4010. {
  4011. if (chargingInfo[index]->Type == _Type_CCS)
  4012. {
  4013. byte targetID = chargingInfo[index]->Evboard_id;
  4014. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1 &&
  4015. ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0)
  4016. {
  4017. targetID += 1;
  4018. }
  4019. system("echo 3 > /proc/sys/vm/drop_caches");
  4020. sleep(2);
  4021. PRINTF_FUNC("Upgrade CCS Processing..target id = %d \n", targetID);
  4022. if (Upgrade_CCS(CanFd, Type, targetID, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL)
  4023. {
  4024. PRINTF_FUNC("Upgrade CCS Failed \n");
  4025. retFail++;
  4026. }
  4027. else
  4028. retSucc++;
  4029. }
  4030. }
  4031. close(CanFd);
  4032. }
  4033. memset(Buf, 0, sizeof(Buf));
  4034. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4035. system(Buf);
  4036. break;
  4037. case _UPDATE_TYPE_DCM_ST407:
  4038. case _UPDATE_TYPE_RELAY_CTR:
  4039. case _UPDATE_TYPE_FAN_CTR:
  4040. case _UPDATE_TYPE_AC_WLMOUNT_CTR:
  4041. case _UPDATE_TYPE_LED_CTR:
  4042. target = 0x00;
  4043. if (Type == _UPDATE_TYPE_DCM_ST407) {
  4044. target = UPGRADE_PRI;
  4045. } else if (Type == _UPDATE_TYPE_RELAY_CTR) {
  4046. target = UPGRADE_RB;
  4047. } else if (Type == _UPDATE_TYPE_FAN_CTR) {
  4048. target = UPGRADE_FAN;
  4049. } else if (Type == _UPDATE_TYPE_AC_WLMOUNT_CTR) {
  4050. target = UPGRADE_AC;
  4051. } else if (Type == _UPDATE_TYPE_LED_CTR) {
  4052. target = UPGRADE_LED;
  4053. }
  4054. uartFd = InitComPort(target);
  4055. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  4056. {
  4057. retSucc++;
  4058. }
  4059. else
  4060. {
  4061. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  4062. retFail++;
  4063. }
  4064. if (uartFd > 0)
  4065. {
  4066. close(uartFd);
  4067. }
  4068. memset(Buf, 0, sizeof(Buf));
  4069. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4070. system(Buf);
  4071. break;
  4072. case _UPDATE_TYPE_CHADEMO:
  4073. case _UPDATE_TYPE_GBT:
  4074. CanFd = InitCanBus();
  4075. if (CanFd > 0)
  4076. {
  4077. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  4078. {
  4079. if ((Type == _UPDATE_TYPE_CHADEMO && chargingInfo[index]->Type == _Type_Chademo) ||
  4080. (Type == _UPDATE_TYPE_GBT && chargingInfo[index]->Type == _Type_GB))
  4081. {
  4082. PRINTF_FUNC("Upgrade GBT / Chademo Processing..target id = %d \n", chargingInfo[index]->Evboard_id);
  4083. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  4084. retSucc++;
  4085. else
  4086. {
  4087. if (chargingInfo[index]->Type == _Type_Chademo)
  4088. PRINTF_FUNC("Upgrade Chademo Failed \n");
  4089. else
  4090. PRINTF_FUNC("Upgrade GBT Failed \n");
  4091. retFail++;
  4092. }
  4093. }
  4094. }
  4095. close(CanFd);
  4096. }
  4097. memset(Buf, 0, sizeof(Buf));
  4098. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4099. system(Buf);
  4100. break;
  4101. }
  4102. }
  4103. close(fd);
  4104. }
  4105. }
  4106. free(dir);
  4107. closedir(d);
  4108. PRINTF_FUNC("Upgrade Result : PASS : %d, FAIL : %d \n", retSucc, retFail);
  4109. sleep(1);
  4110. system("rm -rvf /mnt/* ");
  4111. if (retFail != 0)
  4112. {
  4113. result = FAIL;
  4114. }
  4115. return result;
  4116. }
  4117. void CreateRfidFork()
  4118. {
  4119. pid_t rfidRecPid;
  4120. rfidRecPid = fork();
  4121. if (rfidRecPid == 0)
  4122. {
  4123. int module_type = MODULE_EWT;
  4124. while(true)
  4125. {
  4126. // 刷卡判斷
  4127. RFID rfid;
  4128. if (!ShmSysConfigAndInfo->SysConfig.isRFID)
  4129. {}
  4130. else if(getRequestCardSN(rfidFd, module_type, &rfid))
  4131. {
  4132. //PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4133. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  4134. {
  4135. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  4136. {
  4137. switch (rfid.snType)
  4138. {
  4139. case RFID_SN_TYPE_6BYTE:
  4140. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4141. "%02X%02X%02X%02X%02X%02X",
  4142. rfid.currentCard[0], rfid.currentCard[1],
  4143. rfid.currentCard[2], rfid.currentCard[3],
  4144. rfid.currentCard[4], rfid.currentCard[5]);
  4145. break;
  4146. case RFID_SN_TYPE_7BYTE:
  4147. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4148. "%02X%02X%02X%02X%02X%02X%02X",
  4149. rfid.currentCard[0], rfid.currentCard[1],
  4150. rfid.currentCard[2], rfid.currentCard[3],
  4151. rfid.currentCard[4], rfid.currentCard[5],
  4152. rfid.currentCard[6]);
  4153. break;
  4154. case RFID_SN_TYPE_10BYTE:
  4155. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4156. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  4157. rfid.currentCard[0], rfid.currentCard[1],
  4158. rfid.currentCard[2], rfid.currentCard[3],
  4159. rfid.currentCard[4], rfid.currentCard[5],
  4160. rfid.currentCard[6], rfid.currentCard[7],
  4161. rfid.currentCard[8], rfid.currentCard[9]);
  4162. break;
  4163. case RFID_SN_TYPE_4BYTE:
  4164. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4165. "%02X%02X%02X%02X",
  4166. rfid.currentCard[0], rfid.currentCard[1],
  4167. rfid.currentCard[2], rfid.currentCard[3]);
  4168. break;
  4169. }
  4170. }
  4171. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  4172. {
  4173. switch (rfid.snType)
  4174. {
  4175. case RFID_SN_TYPE_6BYTE:
  4176. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4177. "%02X%02X%02X%02X%02X%02X",
  4178. rfid.currentCard[5], rfid.currentCard[4],
  4179. rfid.currentCard[3], rfid.currentCard[2],
  4180. rfid.currentCard[1], rfid.currentCard[0]);
  4181. break;
  4182. case RFID_SN_TYPE_7BYTE:
  4183. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4184. "%02X%02X%02X%02X%02X%02X%02X",
  4185. rfid.currentCard[6], rfid.currentCard[5],
  4186. rfid.currentCard[4], rfid.currentCard[3],
  4187. rfid.currentCard[2], rfid.currentCard[1],
  4188. rfid.currentCard[0]);
  4189. break;
  4190. case RFID_SN_TYPE_10BYTE:
  4191. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4192. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  4193. rfid.currentCard[9], rfid.currentCard[8],
  4194. rfid.currentCard[7], rfid.currentCard[6],
  4195. rfid.currentCard[5], rfid.currentCard[4],
  4196. rfid.currentCard[3], rfid.currentCard[2],
  4197. rfid.currentCard[1], rfid.currentCard[0]);
  4198. break;
  4199. case RFID_SN_TYPE_4BYTE:
  4200. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4201. "%02X%02X%02X%02X",
  4202. rfid.currentCard[3], rfid.currentCard[2],
  4203. rfid.currentCard[1], rfid.currentCard[0]);
  4204. break;
  4205. }
  4206. }
  4207. PRINTF_FUNC("RFID : Get card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4208. }
  4209. if (rfid.cardType == ISO14443A)
  4210. sethaltCard ( rfidFd, module_type);
  4211. else if (rfid.cardType == IS014443B)
  4212. { }
  4213. else if (rfid.cardType == FELICA)
  4214. { }
  4215. }
  4216. sleep(1);
  4217. }
  4218. }
  4219. }
  4220. void CreateCheckSystemTaskFork()
  4221. {
  4222. pid_t taskPid;
  4223. taskPid = fork();
  4224. if (taskPid == 0)
  4225. {
  4226. bool stopToDet = false;
  4227. while(true)
  4228. {
  4229. if (!stopToDet)
  4230. {
  4231. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4232. {
  4233. if (chargingInfo[_index]->SystemStatus == SYS_MODE_UPDATE ||
  4234. chargingInfo[_index]->Type == 0x09)
  4235. {
  4236. stopToDet = true;
  4237. continue;
  4238. }
  4239. }
  4240. if (!stopToDet)
  4241. CheckSystemTaskAlive();
  4242. }
  4243. sleep(5);
  4244. }
  4245. }
  4246. }
  4247. void StartSystemTimeoutDet(unsigned char flag)
  4248. {
  4249. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  4250. {
  4251. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4252. }
  4253. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  4254. }
  4255. void StopSystemTimeoutDet()
  4256. {
  4257. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4258. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  4259. }
  4260. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  4261. {
  4262. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  4263. {
  4264. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  4265. {
  4266. GetTimespecFunc(&chargingInfo[gunIndex]->ConnectorTimeout);
  4267. }
  4268. chargingInfo[gunIndex]->TimeoutFlag = flag;
  4269. }
  4270. }
  4271. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  4272. {
  4273. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  4274. {
  4275. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  4276. }
  4277. }
  4278. void CheckConnectionTimeout()
  4279. {
  4280. if(system("pidof -s OcppBackend > /dev/null") != 0)
  4281. {
  4282. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4283. }
  4284. else
  4285. {
  4286. _connectionTimeout = _ocpp_get_connect_timeout();
  4287. if(_connectionTimeout <= 0)
  4288. {
  4289. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4290. }
  4291. }
  4292. }
  4293. void GetOfflinePowerValue()
  4294. {
  4295. int value = atoi((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[OffLineMaxChargingPower].ItemData);//Unit: W
  4296. if (ShmDcCommonData->_offlinePower != value)
  4297. {
  4298. ShmDcCommonData->_offlinePower = value;
  4299. PRINTF_FUNC("OCPP offline .. Read OfflinePower = %d \n", ShmDcCommonData->_offlinePower);
  4300. }
  4301. }
  4302. void CreateTimeoutFork()
  4303. {
  4304. pid_t timeoutPid;
  4305. timeoutPid = fork();
  4306. if (timeoutPid > 0)
  4307. {
  4308. GetTimespecFunc(&_cmdSubPriority_time);
  4309. CheckConnectionTimeout();
  4310. int _timebuf = 0;
  4311. while(true)
  4312. {
  4313. _timebuf = GetTimeoutValue(&_cmdSubPriority_time);
  4314. if (_timebuf < 0)
  4315. {
  4316. GetTimespecFunc(&_cmdSubPriority_time);
  4317. }
  4318. else
  4319. {
  4320. if (_timebuf >= 5)
  4321. {
  4322. CheckConnectionTimeout();
  4323. //LW_OfflinePower
  4324. GetOfflinePowerValue();
  4325. GetTimespecFunc(&_cmdSubPriority_time);
  4326. }
  4327. }
  4328. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  4329. // 系統
  4330. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_None)
  4331. {
  4332. _timebuf = GetTimeoutValue(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4333. if (_timebuf < 0)
  4334. {
  4335. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4336. }
  4337. else
  4338. {
  4339. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  4340. {
  4341. case Timeout_SelftestChk:
  4342. if (_timebuf >= SELFTEST_TIMEOUT)
  4343. {
  4344. _SelfTestTimeout();
  4345. StopSystemTimeoutDet();
  4346. }
  4347. break;
  4348. case Timeout_Authorizing:
  4349. if (_timebuf >= AUTHORIZE_TIMEOUT)
  4350. {
  4351. _AuthorizedTimeout();
  4352. StopSystemTimeoutDet();
  4353. }
  4354. break;
  4355. case Timeout_VerifyFail:
  4356. if (_timebuf >= AUTHORIZE_FAIL_TIMEOUT)
  4357. {
  4358. _AutoReturnTimeout();
  4359. StopSystemTimeoutDet();
  4360. }
  4361. break;
  4362. case Timeout_VerifyComp:
  4363. if (_timebuf >= AUTHORIZE_COMP_TIMEOUT)
  4364. {
  4365. _AutoReturnTimeout();
  4366. StopSystemTimeoutDet();
  4367. }
  4368. break;
  4369. case Timeout_WaitPlug:
  4370. if (_timebuf >= _connectionTimeout)
  4371. {
  4372. _DetectPlugInTimeout();
  4373. StopSystemTimeoutDet();
  4374. }
  4375. break;
  4376. case Timeout_ReturnToChargingGunDet:
  4377. {
  4378. if (_timebuf >= RETURN_TO_CHARGING_PAGE)
  4379. {
  4380. DisplayChargingInfo();
  4381. StopSystemTimeoutDet();
  4382. }
  4383. }
  4384. break;
  4385. // case Timeout_AuthorizingForStop:
  4386. // {
  4387. // if (_timebuf / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT)
  4388. // {
  4389. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  4390. // ClearAuthorizedFlag();
  4391. // StopSystemTimeoutDet();
  4392. // }
  4393. // }
  4394. // break;
  4395. }
  4396. }
  4397. }
  4398. // 各槍
  4399. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4400. {
  4401. if (chargingInfo[gun_index]->TimeoutFlag != Timeout_None)
  4402. {
  4403. _timebuf = GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout);
  4404. if (_timebuf < 0)
  4405. {
  4406. GetTimespecFunc(&chargingInfo[gun_index]->ConnectorTimeout);
  4407. }
  4408. else
  4409. {
  4410. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  4411. switch(chargingInfo[gun_index]->TimeoutFlag)
  4412. {
  4413. case Timeout_Preparing:
  4414. {
  4415. if (_timebuf >= GUN_PREPARE_TIMEOUT)
  4416. {
  4417. _PrepareTimeout(gun_index);
  4418. StopGunInfoTimeoutDet(gun_index);
  4419. }
  4420. }
  4421. break;
  4422. case Timeout_EvChargingDet:
  4423. {
  4424. if (_timebuf >= GUN_EV_WAIT_TIMEOUT)
  4425. {
  4426. _DetectEvChargingEnableTimeout(gun_index);
  4427. StopGunInfoTimeoutDet(gun_index);
  4428. }
  4429. }
  4430. break;
  4431. case Timeout_EvseChargingDet:
  4432. {
  4433. if (chargingInfo[gun_index]->Type == _Type_GB)
  4434. {
  4435. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT * 3)
  4436. {
  4437. _DetectEvseChargingEnableTimeout(gun_index);
  4438. StopGunInfoTimeoutDet(gun_index);
  4439. }
  4440. }
  4441. else
  4442. {
  4443. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT)
  4444. {
  4445. _DetectEvseChargingEnableTimeout(gun_index);
  4446. StopGunInfoTimeoutDet(gun_index);
  4447. }
  4448. }
  4449. }
  4450. break;
  4451. case Timeout_EvseCompleteDet:
  4452. {
  4453. if (_timebuf >= GUN_COMP_WAIT_TIMEOUT)
  4454. {
  4455. StopGunInfoTimeoutDet(gun_index);
  4456. }
  4457. }
  4458. break;
  4459. case Timeout_ForCcsPrechargeDet:
  4460. {
  4461. if (_timebuf >= GUN_PRECHARGING_TIMEOUT)
  4462. {
  4463. _CcsPrechargeTimeout(gun_index);
  4464. StopGunInfoTimeoutDet(gun_index);
  4465. }
  4466. }
  4467. break;
  4468. }
  4469. }
  4470. }
  4471. }
  4472. sleep(1);
  4473. }
  4474. }
  4475. }
  4476. void GetSystemTime()
  4477. {
  4478. struct timeb csuTime;
  4479. struct tm *tmCSU;
  4480. ftime(&csuTime);
  4481. tmCSU = localtime(&csuTime.time);
  4482. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  4483. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4484. tmCSU->tm_sec);
  4485. // byte date[14];
  4486. //
  4487. //
  4488. // //sprintf(&date, "%d", );
  4489. //
  4490. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  4491. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  4492. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  4493. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  4494. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  4495. //
  4496. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  4497. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  4498. //
  4499. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  4500. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  4501. //
  4502. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  4503. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  4504. //
  4505. // date[10] = (tmCSU->tm_min) / 10 % 10;
  4506. // date[11] = (tmCSU->tm_min) / 1 % 10;
  4507. //
  4508. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  4509. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  4510. // 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],
  4511. // date[4], date[5], date[6], date[7],
  4512. // date[8], date[9], date[10], date[11],
  4513. // date[12], date[13]);
  4514. }
  4515. void CheckFactoryConfigFunction()
  4516. {
  4517. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4518. {
  4519. char Buf[256];
  4520. sleep(5);
  4521. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  4522. ShmSysConfigAndInfo->SysConfig.ModelName,
  4523. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  4524. system(Buf);
  4525. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4526. system("sync");
  4527. sleep(5);
  4528. system("reboot -f");
  4529. sleep(5);
  4530. system("reboot -f");
  4531. }
  4532. }
  4533. void CheckFwUpdateFunction()
  4534. {
  4535. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  4536. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  4537. {
  4538. DEBUG_INFO_MSG("ftp : update start. \n");
  4539. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4540. {
  4541. SetChargerMode(gun_index, SYS_MODE_UPDATE);
  4542. }
  4543. CloseWatchDog();
  4544. sleep(2);
  4545. KillTask();
  4546. byte updateResult = CheckUpdateProcess();
  4547. if (updateResult == PASS)
  4548. DEBUG_INFO_MSG("ftp : update complete. \n");
  4549. else if (updateResult == MODELNAME_FAIL)
  4550. {
  4551. DEBUG_INFO_MSG("ftp : model name is none match. \n");
  4552. KillAllTask();
  4553. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4554. sleep(5);
  4555. system("/usr/bin/run_evse_restart.sh");
  4556. return;
  4557. }
  4558. else
  4559. DEBUG_INFO_MSG("ftp : update fail. \n");
  4560. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4561. sleep(5);
  4562. system("reboot -f");
  4563. }
  4564. else
  4565. {
  4566. ocpp_chk_update_cmd();
  4567. }
  4568. }
  4569. int TariffParsing(char *StringItem, char *TariffCode)
  4570. {
  4571. char *ptrSave, *ptrToken;
  4572. char strSource[128];
  4573. int fee = 0;
  4574. memcpy(strSource, StringItem, 128);
  4575. ptrToken = strtok_r(strSource, ";", &ptrSave);
  4576. while(ptrToken != NULL)
  4577. {
  4578. char *strTariff = strstr(ptrToken, TariffCode);
  4579. if(strTariff != NULL)
  4580. {
  4581. char *strFee = strstr(strTariff, "$");
  4582. if(strFee != NULL)
  4583. {
  4584. strFee++;
  4585. float fFee = atof(strFee);
  4586. fee = (int)(fFee * 100);
  4587. break;
  4588. }
  4589. }
  4590. ptrToken = strtok_r(NULL, ";", &ptrSave);
  4591. }
  4592. return fee;
  4593. }
  4594. void DefaultPriceHandlerOcpp16(char *StrDefaultPrice)
  4595. {
  4596. unsigned int connectionFee = 0, currentRate = 0, occupancyFee = 0;
  4597. PRINTF_FUNC("DefaultPriceHandler - StrDefaultPrice = %s \n", StrDefaultPrice);
  4598. connectionFee = TariffParsing(StrDefaultPrice, "Connection Fee");
  4599. currentRate = TariffParsing(StrDefaultPrice, "Current Rate");
  4600. occupancyFee = TariffParsing(StrDefaultPrice, "Occupancy Fee");
  4601. PRINTF_FUNC("*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100));
  4602. PRINTF_FUNC("*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100));
  4603. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100));
  4604. ShmDcCommonData->balanceInfo.defaultPrice = currentRate;
  4605. }
  4606. void UserPriceHandlerOcpp16(char *UserId, char *UserPrice)
  4607. {
  4608. unsigned int connectionFee = 0, currentRate = 0, occupancyFee = 0;
  4609. int accountBalance = 0;
  4610. byte acGunIndex = 0;
  4611. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  4612. {
  4613. if (gunIndex == 1)
  4614. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4615. else
  4616. acGunIndex = 0;
  4617. if((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4618. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) &&
  4619. strcmp((char *)chargingInfo[gunIndex]->StartUserId, UserId) == EQUAL)
  4620. {
  4621. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Connection Fee");
  4622. currentRate = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Current Rate");
  4623. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Occupancy Fee");
  4624. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Account Balance");
  4625. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].UserPrice != currentRate ||
  4626. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4627. {
  4628. PRINTF_FUNC("*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100));
  4629. PRINTF_FUNC("*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100));
  4630. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100));
  4631. PRINTF_FUNC("*** Account Balance: %d.%02d ***", (accountBalance / 100), (accountBalance % 100));
  4632. PRINTF_FUNC("*** Connector Id %d User Price %d.%02d, Account Balance %d.%02d ***",
  4633. (gunIndex), (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100));
  4634. }
  4635. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].UserPrice = currentRate;
  4636. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance = accountBalance;
  4637. }
  4638. }
  4639. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex++)
  4640. {
  4641. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4642. if(strcmp((char *)ac_chargingInfo[gunIndex]->StartUserId, UserId) == EQUAL)
  4643. {
  4644. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].UserPrice != currentRate ||
  4645. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance != accountBalance)
  4646. {
  4647. PRINTF_FUNC("*** AC Connector User Price %d.%02d, Account Balance %d.%02d ***",
  4648. (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100));
  4649. }
  4650. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].UserPrice = currentRate;
  4651. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance = accountBalance;
  4652. }
  4653. }
  4654. }
  4655. void RunningFinalCostHandlerOcpp16()
  4656. {
  4657. unsigned int connectionFee = 0, sessionFee = 0, occupancyFee = 0, totalCost = 0;
  4658. int accountBalance = 0;
  4659. byte acGunIndex = 0;
  4660. bool isFind = false;
  4661. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  4662. {
  4663. isFind = false;
  4664. if (gunIndex == 1)
  4665. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4666. else
  4667. acGunIndex = 0;
  4668. // PRINTF_FUNC("*** gunIndex = %d, description = %s *** \n", gunIndex + acGunIndex,
  4669. // ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description);
  4670. // PRINTF_FUNC("*** gunIndex = %d, f description = %s *** \n", gunIndex + acGunIndex,
  4671. // ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description);
  4672. if (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4673. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4674. {
  4675. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description) > 0)
  4676. {
  4677. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Connection Fee");
  4678. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Session Fee");
  4679. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Occupancy Fee");
  4680. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Total Cost");
  4681. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Account Balance");
  4682. PRINTF_FUNC("*** Connector Id: %d Running Cost *** \n", gunIndex);
  4683. isFind = true;
  4684. memset(&ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex], 0x00, sizeof(struct StrcutRunningCost));
  4685. }
  4686. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description) > 0)
  4687. {
  4688. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Connection Fee");
  4689. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Session Fee");
  4690. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Occupancy Fee");
  4691. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Total Cost");
  4692. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Account Balance");
  4693. PRINTF_FUNC("********** Connector Id: %d Final Cost ********** \n", gunIndex);
  4694. isFind = true;
  4695. memset(&ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex], 0x00, sizeof(struct StrcutFinalCost));
  4696. }
  4697. if (isFind)
  4698. {
  4699. PRINTF_FUNC("*** Connection Fee: %d.%02d *** \n", (connectionFee / 100), (connectionFee % 100));
  4700. PRINTF_FUNC("*** Session Fee: %d.%02d *** \n", (sessionFee / 100), (sessionFee % 100));
  4701. PRINTF_FUNC("*** Occupancy Fee: %d.%02d *** \n", (occupancyFee / 100), (occupancyFee % 100));
  4702. PRINTF_FUNC("*** Total Cost: %d.%02d *** \n", (totalCost / 100), (totalCost % 100));
  4703. PRINTF_FUNC("*** Account Balance: %d.%02d *** \n", (accountBalance / 100), (accountBalance % 100));
  4704. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost != totalCost ||
  4705. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4706. {
  4707. PRINTF_FUNC("*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d *** \n",
  4708. (gunIndex), (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100));
  4709. }
  4710. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost = totalCost;
  4711. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance = accountBalance;
  4712. }
  4713. }
  4714. }
  4715. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex++)
  4716. {
  4717. isFind = false;
  4718. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4719. if (ac_chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_PREPARING &&
  4720. ac_chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4721. {
  4722. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description) > 0)
  4723. {
  4724. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Connection Fee");
  4725. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Session Fee");
  4726. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Occupancy Fee");
  4727. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Total Cost");
  4728. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Account Balance");
  4729. PRINTF_FUNC("*** AC Connector : Running Cost ***\n");
  4730. isFind = true;
  4731. memset(&ShmOCPP16Data->Cost.RunningCost[acGunIndex], 0x00, sizeof(struct StrcutRunningCost));
  4732. }
  4733. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description) > 0)
  4734. {
  4735. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Connection Fee");
  4736. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Session Fee");
  4737. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Occupancy Fee");
  4738. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Total Cost");
  4739. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Account Balance");
  4740. PRINTF_FUNC("*** AC Connector : Final Cost ***\n");
  4741. isFind = true;
  4742. memset(&ShmOCPP16Data->Cost.FinalCost[acGunIndex], 0x00, sizeof(struct StrcutFinalCost));
  4743. }
  4744. if (isFind)
  4745. {
  4746. PRINTF_FUNC("*** Connection Fee: %d.%02d ***\n", (connectionFee / 100), (connectionFee % 100));
  4747. PRINTF_FUNC("*** Session Fee: %d.%02d ***\n", (sessionFee / 100), (sessionFee % 100));
  4748. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***\n", (occupancyFee / 100), (occupancyFee % 100));
  4749. PRINTF_FUNC("*** Total Cost: %d.%02d ***\n", (totalCost / 100), (totalCost % 100));
  4750. PRINTF_FUNC("*** Account Balance: %d.%02d ***\n", (accountBalance / 100), (accountBalance % 100));
  4751. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost != totalCost ||
  4752. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4753. {
  4754. PRINTF_FUNC("*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d ***\n",
  4755. (gunIndex), (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100));
  4756. }
  4757. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].TotalCost = totalCost;
  4758. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance = accountBalance;
  4759. }
  4760. }
  4761. }
  4762. }
  4763. //===============================================
  4764. // ocpp key
  4765. //===============================================
  4766. bool ocpp_authorizeRemoteTxReqChk_key()
  4767. {
  4768. bool result = false;
  4769. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4770. {
  4771. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
  4772. result = true;
  4773. }
  4774. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4775. {
  4776. }
  4777. return result;
  4778. }
  4779. //===============================================
  4780. // Check reservation date is expired
  4781. //===============================================
  4782. int opcc_chk_reserve_expired(byte gun_index)
  4783. {
  4784. int result = NO;
  4785. byte acDirIndex = 0;
  4786. if (gun_index == 1)
  4787. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4788. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4789. {
  4790. if(chargingInfo[gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  4791. PRINTF_FUNC("%s \n", ShmOCPP16Data->ReserveNow[gun_index + acDirIndex].ExpiryDate);
  4792. if (CheckTimeOut(ShmOCPP16Data->ReserveNow[gun_index + acDirIndex].ExpiryDate))
  4793. result = YES;
  4794. }
  4795. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4796. {
  4797. if(chargingInfo[gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  4798. PRINTF_FUNC("%s \n", ShmOCPP20Data->ReserveNow[gun_index + acDirIndex].expiryDateTime);
  4799. if (CheckTimeOut(ShmOCPP20Data->ReserveNow[gun_index + acDirIndex].expiryDateTime))
  4800. result = YES;
  4801. }
  4802. return result;
  4803. }
  4804. //===============================================
  4805. // AC OCPP routine
  4806. //===============================================
  4807. void ocpp_ac_chk_availability_cmd()
  4808. {
  4809. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4810. byte type = 0;
  4811. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  4812. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4813. {
  4814. if (ShmOCPP16Data->CsMsg.bits[hasDc].ChangeAvailabilityReq == YES)
  4815. {
  4816. PRINTF_FUNC("***************ChkOcppStatus : AC OcppChangeAvailability******************** \n");
  4817. DEBUG_ERROR_MSG("***************ChkOcppStatus : AC OcppChangeAvailability******************** \n");
  4818. ShmOCPP16Data->CsMsg.bits[hasDc].ChangeAvailabilityReq = NO;
  4819. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[hasDc].Type, "Operative") == EQUAL)
  4820. {
  4821. if (isDb_ready)
  4822. DB_Update_Operactive(localDb, hasDc, true);
  4823. type = 1;
  4824. }
  4825. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[hasDc].Type, "Inoperative") == EQUAL)
  4826. {
  4827. if (isDb_ready)
  4828. DB_Update_Operactive(localDb, hasDc, false);
  4829. type = 2;
  4830. }
  4831. }
  4832. }
  4833. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4834. {
  4835. if (ShmOCPP20Data->CsMsg.bits[hasDc].ChangeAvailabilityReq == YES)
  4836. {
  4837. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4838. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4839. ShmOCPP20Data->CsMsg.bits[hasDc].ChangeAvailabilityReq = NO;
  4840. if(strcmp((char *)ShmOCPP20Data->ChangeAvailability[hasDc].operationalStatus, "Operative") == EQUAL)
  4841. {
  4842. if (isDb_ready)
  4843. DB_Update_Operactive(localDb, hasDc, true);
  4844. type = 1;
  4845. }
  4846. else if (strcmp((char *)ShmOCPP20Data->ChangeAvailability[hasDc].operationalStatus, "Inoperative") == EQUAL)
  4847. {
  4848. if (isDb_ready)
  4849. DB_Update_Operactive(localDb, hasDc, false);
  4850. type = 2;
  4851. }
  4852. }
  4853. }
  4854. if (type == 1)
  4855. {
  4856. ac_chargingInfo[0]->IsAvailable = YES;
  4857. }
  4858. else if (type == 2)
  4859. {
  4860. ac_chargingInfo[0]->IsAvailable = NO;
  4861. }
  4862. }
  4863. void ocpp_ac_chk_unlock_cmd()
  4864. {
  4865. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4866. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  4867. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4868. {
  4869. if (ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorReq == YES)
  4870. {
  4871. ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorReq = NO;
  4872. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4873. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  4874. {
  4875. // Unlocked - 充電中,需停止充電
  4876. strcpy((char *)ShmOCPP16Data->StopTransaction[hasDc].StopReason, "UnlockCommand");
  4877. ac_chargingInfo[0]->StopChargeFlag = YES;
  4878. }
  4879. strcpy((char *)ShmOCPP16Data->UnlockConnector[hasDc].ResponseStatus, "Unlocked");
  4880. ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorConf = YES;
  4881. }
  4882. }
  4883. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4884. {
  4885. if (ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorReq == YES)
  4886. {
  4887. ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorReq = NO;
  4888. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4889. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  4890. {
  4891. // Unlocked - 充電中,需停止充電
  4892. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].transactionInfo.stoppedReason, "UnlockCommand");
  4893. ac_chargingInfo[0]->StopChargeFlag = YES;
  4894. }
  4895. strcpy((char*)ShmOCPP20Data->UnlockConnector[hasDc].Response_status, "Unlocked");
  4896. ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorConf = YES;
  4897. }
  4898. }
  4899. }
  4900. void ocpp_ac_chk_profileConf_cmd()
  4901. {
  4902. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4903. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4904. {
  4905. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf == YES)
  4906. {
  4907. ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf = NO;
  4908. }
  4909. }
  4910. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4911. {
  4912. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf == YES)
  4913. {
  4914. ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf = NO;
  4915. }
  4916. }
  4917. }
  4918. void ocpp_ac_chargingProfile_process()
  4919. {
  4920. int _time = 0;
  4921. int _startCount = NO_DEFINE;
  4922. int _maxCount = 0;
  4923. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4924. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4925. {
  4926. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingProfileKind, "Absolute") == EQUAL &&
  4927. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingProfileId == YES)
  4928. {
  4929. _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.StartSchedule);
  4930. if (_time > -1)
  4931. {
  4932. _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod);
  4933. _startCount = NO_DEFINE;
  4934. for (byte _count = 0; _count < _maxCount; _count++)
  4935. {
  4936. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4937. if (_time >= ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  4938. {
  4939. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  4940. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  4941. {
  4942. _startCount = _count;
  4943. }
  4944. }
  4945. }
  4946. if (_startCount < _maxCount)
  4947. {
  4948. //PRINTF_FUNC("AC - Gun_%d, Get Profile - Profile Index = %d \n", hasDc, _startCount);
  4949. //PRINTF_FUNC("Profile Limit = %.2f \n", ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  4950. if (ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit >= 1 &&
  4951. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit <= 5)
  4952. {
  4953. ac_chargingInfo[0]->ChargingProfileCurrent = AC_MINIMUM_DUTY;
  4954. }
  4955. else if (ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit == 0)
  4956. {
  4957. ac_chargingInfo[0]->ChargingProfileCurrent = AC_STOP_DUTY;
  4958. }
  4959. else
  4960. {
  4961. ac_chargingInfo[0]->ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit;
  4962. }
  4963. }
  4964. else
  4965. {
  4966. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4967. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4968. }
  4969. }
  4970. else
  4971. {
  4972. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4973. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4974. }
  4975. }
  4976. else
  4977. {
  4978. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4979. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4980. }
  4981. }
  4982. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4983. {
  4984. if (strcmp((char *)ShmOCPP20Data->SmartChargingProfile[hasDc].chargingProfileKind, "Absolute") == EQUAL &&
  4985. ShmOCPP20Data->SmartChargingProfile[hasDc].id == YES)
  4986. {
  4987. _time = GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].startSchedule);
  4988. _maxCount = ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod);
  4989. _startCount = NO_DEFINE;
  4990. for (byte _count = 0; _count < _maxCount; _count++)
  4991. {
  4992. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4993. if (_time >= ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].startPeriod)
  4994. {
  4995. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].limit >= MIN_OUTPUT_CUR) ||
  4996. ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].limit > MIN_OUTPUT_CUR)
  4997. {
  4998. _startCount = _count;
  4999. }
  5000. }
  5001. }
  5002. if (_startCount < _maxCount)
  5003. {
  5004. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", 1, _startCount);
  5005. //PRINTF_FUNC("Profile Limit = %.2f \n", ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit);
  5006. if (ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit >= 1 &&
  5007. ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit <= 5)
  5008. {
  5009. ac_chargingInfo [0]->ChargingProfileCurrent = AC_MINIMUM_DUTY;
  5010. }
  5011. else if (ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit == 0)
  5012. {
  5013. ac_chargingInfo [0]->ChargingProfileCurrent = AC_STOP_DUTY;
  5014. }
  5015. else
  5016. {
  5017. ac_chargingInfo [0]->ChargingProfileCurrent = ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit;
  5018. }
  5019. }
  5020. else
  5021. {
  5022. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5023. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5024. }
  5025. }
  5026. else
  5027. {
  5028. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5029. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5030. }
  5031. }
  5032. else
  5033. {
  5034. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5035. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5036. }
  5037. if (ac_chargingInfo[0]->ChargingProfileCurrent >= 0 &&
  5038. _AcChargingProfileCurBuffer != ac_chargingInfo[0]->ChargingProfileCurrent)
  5039. {
  5040. _AcChargingProfileCurBuffer = ac_chargingInfo[0]->ChargingProfileCurrent;
  5041. PRINTF_FUNC ( "AC Profile (duty) : ChargingProfileCurrent = %.2f \n", ac_chargingInfo[0]->ChargingProfileCurrent);
  5042. }
  5043. }
  5044. void ocpp_ac_lift_profileReq_cmd(byte gunIndex)
  5045. {
  5046. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5047. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5048. {
  5049. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf == NO)
  5050. {
  5051. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileReq == NO)
  5052. ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileReq = YES;
  5053. }
  5054. }
  5055. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5056. {
  5057. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf == NO)
  5058. {
  5059. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileReq == NO)
  5060. ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileReq = YES;
  5061. }
  5062. }
  5063. }
  5064. int opcc_ac_chk_reserve_expired()
  5065. {
  5066. int result = NO;
  5067. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5068. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5069. {
  5070. if(ac_chargingInfo [0]->SystemStatus != SYS_MODE_RESERVATION)
  5071. PRINTF_FUNC("%s \n", ShmOCPP16Data->ReserveNow[hasDc].ExpiryDate);
  5072. if (CheckTimeOut(ShmOCPP16Data->ReserveNow[hasDc].ExpiryDate))
  5073. result = YES;
  5074. }
  5075. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5076. {
  5077. if(ac_chargingInfo [0]->SystemStatus != SYS_MODE_RESERVATION)
  5078. PRINTF_FUNC("%s \n", ShmOCPP20Data->ReserveNow[hasDc].expiryDateTime);
  5079. if (CheckTimeOut(ShmOCPP20Data->ReserveNow[hasDc].expiryDateTime))
  5080. result = YES;
  5081. }
  5082. return result;
  5083. }
  5084. void ocpp_ac_chk_reserved_cmd()
  5085. {
  5086. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5087. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5088. {
  5089. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE &&
  5090. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowReq == YES)
  5091. {
  5092. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5093. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowReq = NO;
  5094. if ( ! opcc_ac_chk_reserve_expired ())
  5095. {
  5096. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP16Data->ReserveNow [hasDc].ReservationId );
  5097. PRINTF_FUNC ( "IdTag = %s \n", ShmOCPP16Data->ReserveNow [hasDc].IdTag );
  5098. PRINTF_FUNC ( "*************ChkOcppStatus : (AC) OcppReservedStatus End****************** \n" );
  5099. ac_chargingInfo [0]->ReservationId = ShmOCPP16Data->ReserveNow [hasDc].ReservationId;
  5100. ac_chargingInfo [0]->SystemStatus = SYS_MODE_RESERVATION;
  5101. strcpy ( (char *) ShmDcCommonData->_reserved_ac_UserId[0],
  5102. (char *) ShmOCPP16Data->ReserveNow [hasDc].IdTag );
  5103. }
  5104. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowConf = YES;
  5105. }
  5106. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_RESERVATION &&
  5107. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationReq == YES)
  5108. {
  5109. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationReq = NO;
  5110. if (ShmOCPP16Data->ReserveNow [hasDc].ReservationId == ac_chargingInfo [0]->ReservationId)
  5111. {
  5112. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5113. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP16Data->ReserveNow [hasDc].ReservationId );
  5114. PRINTF_FUNC ( "***********ChkOcppStatus : (AC) Cancel OcppReservedStatus End**************** \n" );
  5115. ac_chargingInfo [0]->ReservationId = -1;
  5116. ac_chargingInfo [0]->SystemStatus = SYS_MODE_IDLE;
  5117. strcpy ( (char *) ShmDcCommonData->_reserved_ac_UserId[0], "" );
  5118. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationConf = YES;
  5119. }
  5120. }
  5121. }
  5122. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5123. {
  5124. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE &&
  5125. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowReq == YES)
  5126. {
  5127. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5128. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowReq = NO;
  5129. if ( ! opcc_ac_chk_reserve_expired ())
  5130. {
  5131. PRINTF_FUNC ( "id = %d \n", ShmOCPP20Data->ReserveNow [hasDc].id );
  5132. PRINTF_FUNC ( "idToken = %s \n", ShmOCPP20Data->ReserveNow [hasDc].idToken.idToken );
  5133. PRINTF_FUNC ( "*************ChkOcppStatus : (AC) OcppReservedStatus End****************** \n" );
  5134. ac_chargingInfo [0]->ReservationId = ShmOCPP20Data->ReserveNow [hasDc].id;
  5135. ac_chargingInfo [0]->SystemStatus = SYS_MODE_RESERVATION;
  5136. strncpy ( (char *) ShmDcCommonData->_reserved_UserId,
  5137. (char *) ShmOCPP20Data->ReserveNow [hasDc].idToken.idToken, 30 );
  5138. }
  5139. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowConf = YES;
  5140. }
  5141. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_RESERVATION &&
  5142. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationReq == YES)
  5143. {
  5144. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationReq = NO;
  5145. if (ShmOCPP20Data->ReserveNow [hasDc].id == ac_chargingInfo [0]->ReservationId)
  5146. {
  5147. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5148. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP20Data->ReserveNow [hasDc].id );
  5149. PRINTF_FUNC ( "***********ChkOcppStatus : (AC) Cancel OcppReservedStatus End**************** \n" );
  5150. ac_chargingInfo [0]->ReservationId = -1;
  5151. ac_chargingInfo [0]->SystemStatus = SYS_MODE_IDLE;
  5152. strcpy ( (char *) ShmDcCommonData->_reserved_UserId, "" );
  5153. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationConf = YES;
  5154. }
  5155. }
  5156. }
  5157. }
  5158. //===============================================
  5159. // OCPP routine
  5160. //===============================================
  5161. void ocpp_process_start()
  5162. {
  5163. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  5164. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  5165. {
  5166. if(system("pidof -s OcppBackend > /dev/null") != 0)
  5167. {
  5168. DEBUG_ERROR_MSG("OcppBackend not running, restart it.\r\n");
  5169. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5170. system("/root/OcppBackend &");
  5171. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5172. system("/root/OcppBackend20 &");
  5173. }
  5174. else
  5175. {
  5176. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5177. {
  5178. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  5179. {
  5180. DEBUG_ERROR_MSG("OcppBackend watch dog timeout task restart.\n");
  5181. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  5182. system("pkill OcppBackend");
  5183. }
  5184. }
  5185. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5186. {
  5187. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  5188. {
  5189. DEBUG_ERROR_MSG("OcppBackend20 watch dog timeout task restart.\n");
  5190. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  5191. system("pkill OcppBackend20");
  5192. }
  5193. }
  5194. }
  5195. }
  5196. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  5197. {
  5198. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  5199. {
  5200. DEBUG_ERROR_MSG("OcppBackendPH not running, restart it.\r\n");
  5201. system("/root/OcppBackendPH &");
  5202. }
  5203. }
  5204. }
  5205. void ocpp_auto_response_BootNotification()
  5206. {
  5207. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5208. {
  5209. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  5210. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  5211. }
  5212. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5213. {
  5214. if (ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES)
  5215. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = NO;
  5216. }
  5217. }
  5218. bool ocpp_is_resPass_StartTransactionConf(byte gun_index)
  5219. {
  5220. bool result = false;
  5221. byte acDirIndex = 0;
  5222. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5223. if (gun_index == 1)
  5224. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5225. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5226. {
  5227. result = ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf;
  5228. ShmDcCommonData->startTransactionID[gun_index] = ShmOCPP16Data->StartTransaction[gun_index + acDirIndex].ResponseTransactionId;
  5229. }
  5230. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5231. {
  5232. result = ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf;
  5233. }
  5234. return result;
  5235. }
  5236. void ocpp_auto_response_StartTransationConf(byte gun_index)
  5237. {
  5238. byte acDirIndex = 0;
  5239. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5240. if (gun_index == 1)
  5241. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5242. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5243. {
  5244. if (ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf)
  5245. ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf = NO;
  5246. }
  5247. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5248. {
  5249. if (ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf)
  5250. ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf = NO;
  5251. }
  5252. }
  5253. void ocpp_auto_response_StopTransationConf(byte gun_index)
  5254. {
  5255. byte acDirIndex = 0;
  5256. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5257. if (gun_index == 1)
  5258. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5259. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5260. {
  5261. if(ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf)
  5262. ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf = NO;
  5263. }
  5264. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5265. {
  5266. }
  5267. }
  5268. void ocpp_auto_response_StopTransationConfPH(byte gun_index)
  5269. {
  5270. byte acDirIndex = 0;
  5271. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5272. if (gun_index == 1)
  5273. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5274. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  5275. {
  5276. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5277. {
  5278. if(ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf)
  5279. ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf = NO;
  5280. }
  5281. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5282. {
  5283. }
  5284. }
  5285. }
  5286. void ocpp_copy_stopReason2maintainServer(byte gun_index)
  5287. {
  5288. byte acDirIndex = 0;
  5289. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5290. if (gun_index == 1)
  5291. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5292. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  5293. {
  5294. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index + acDirIndex].StopReason, ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason));
  5295. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index + acDirIndex].IdTag, ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].IdTag));
  5296. }
  5297. }
  5298. void ocpp_auto_response_StartTransationConfPH(byte gun_index)
  5299. {
  5300. byte acDirIndex = 0;
  5301. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5302. if (gun_index == 1)
  5303. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5304. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  5305. {
  5306. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5307. {
  5308. if (ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf)
  5309. ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf = NO;
  5310. }
  5311. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5312. {
  5313. }
  5314. }
  5315. }
  5316. void ocpp_auto_response_ReserveConf(byte gun_index)
  5317. {
  5318. byte acDirIndex = 0;
  5319. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5320. if (gun_index == 1)
  5321. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5322. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5323. ShmOCPP16Data->CsMsg.bits[gun_index + acDirIndex].ReserveNowConf = YES;
  5324. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5325. ShmOCPP20Data->CsMsg.bits[gun_index + acDirIndex].ReserveNowConf = YES;
  5326. }
  5327. bool ocpp_chk_authrization_cmd()
  5328. {
  5329. char buf2[16] = "";
  5330. memset(buf2, 0, ARRAY_SIZE(buf2));
  5331. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5332. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  5333. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5334. sprintf(buf2, "%s", ShmOCPP20Data->Authorize.Response_idTokenInfo.status);
  5335. // 因為無法得知實際的長度,所以只能用搜尋的方式
  5336. if(strcmp(buf2, "Accepted") == EQUAL)
  5337. return true;
  5338. return false;
  5339. }
  5340. bool opcc_sub_get_reset_req()
  5341. {
  5342. bool result = false;
  5343. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5344. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  5345. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5346. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  5347. return result;
  5348. }
  5349. void ocpp_sub_run_reset(bool canReset)
  5350. {
  5351. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5352. {
  5353. if(canReset && strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  5354. {
  5355. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  5356. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  5357. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  5358. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  5359. sleep(3);
  5360. system("reboot -f");
  5361. }
  5362. else if (canReset && strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  5363. {
  5364. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  5365. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  5366. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  5367. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  5368. sleep(3);
  5369. KillAllTask();
  5370. CloseWatchDog();
  5371. system("/usr/bin/run_evse_restart.sh");
  5372. }
  5373. }
  5374. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5375. {
  5376. if(canReset && strcmp((char *)ShmOCPP20Data->Reset.type, "Immediate") == EQUAL)
  5377. {
  5378. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  5379. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  5380. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  5381. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  5382. sleep(3);
  5383. system("reboot -f");
  5384. }
  5385. else if (canReset && strcmp((char *)ShmOCPP20Data->Reset.type, "OnIdle") == EQUAL)
  5386. {
  5387. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  5388. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  5389. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  5390. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  5391. sleep(3);
  5392. KillAllTask();
  5393. CloseWatchDog();
  5394. system("/usr/bin/run_evse_restart.sh");
  5395. }
  5396. }
  5397. }
  5398. void ocpp_chk_reset_cmd(byte isforce)
  5399. {
  5400. if (opcc_sub_get_reset_req())
  5401. {
  5402. bool canReset = true;
  5403. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  5404. {
  5405. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5406. {
  5407. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5408. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  5409. {
  5410. // Reset - AC 先停止充電
  5411. ac_chargingInfo[0]->StopChargeFlag = YES;
  5412. GetTimespecFunc(&_resetChkTime);
  5413. }
  5414. }
  5415. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5416. {
  5417. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_REASSIGN &&
  5418. chargingInfo[_index]->SystemStatus <= SYS_MODE_TERMINATING)
  5419. {
  5420. canReset = false;
  5421. // Reset 系統先停止充電
  5422. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5423. {
  5424. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  5425. ocpp_set_stopReason_by_cmd(_index, "HardReset");
  5426. else
  5427. ocpp_set_stopReason_by_cmd(_index, "SoftReset");
  5428. }
  5429. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5430. {
  5431. if(strcmp((char *)ShmOCPP20Data->Reset.type, "Immediate") == EQUAL)
  5432. ocpp_set_stopReason_by_cmd(_index, "HardReset");
  5433. else
  5434. ocpp_set_stopReason_by_cmd(_index, "SoftReset");
  5435. }
  5436. ChargingTerminalProcess(_index);
  5437. GetTimespecFunc(&_resetChkTime);
  5438. }
  5439. }
  5440. }
  5441. if (canReset)
  5442. {
  5443. if (GetTimeoutValue(&_resetChkTime) < 0)
  5444. GetTimespecFunc(&_resetChkTime);
  5445. if (GetTimeoutValue(&_resetChkTime) >= 5)
  5446. {
  5447. ShmDcCommonData->evBoardResetFlag = YES;
  5448. // Reset -> change status to maintain.
  5449. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5450. {
  5451. SetChargerMode(_index, SYS_MODE_MAINTAIN);
  5452. }
  5453. ChangeLcmByIndex(_LCM_FIX);
  5454. }
  5455. else
  5456. canReset = false;
  5457. }
  5458. else
  5459. GetTimespecFunc(&_resetChkTime);
  5460. if (isforce == YES)
  5461. canReset = true;
  5462. ocpp_sub_run_reset(canReset);
  5463. }
  5464. }
  5465. void ocpp_chk_reserved_cmd(byte gunIndex)
  5466. {
  5467. byte acDirIndex = 0;
  5468. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5469. if (gunIndex == 1)
  5470. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5471. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5472. {
  5473. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE &&
  5474. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq == YES)
  5475. {
  5476. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5477. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq = NO;
  5478. if (!opcc_chk_reserve_expired(gunIndex))
  5479. {
  5480. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId);
  5481. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].IdTag);
  5482. PRINTF_FUNC("*************ChkOcppStatus : OcppReservedStatus End****************** \n");
  5483. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  5484. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  5485. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex],
  5486. (char *)ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].IdTag);
  5487. }
  5488. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowConf = YES;
  5489. }
  5490. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION &&
  5491. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq == YES)
  5492. {
  5493. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq = NO;
  5494. if (ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId == chargingInfo[gunIndex]->ReservationId)
  5495. {
  5496. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5497. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId);
  5498. PRINTF_FUNC("***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n");
  5499. chargingInfo[gunIndex]->ReservationId = -1;
  5500. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_IDLE;
  5501. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex], "");
  5502. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationConf = YES;
  5503. }
  5504. }
  5505. }
  5506. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5507. {
  5508. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE &&
  5509. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq == YES)
  5510. {
  5511. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5512. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq = NO;
  5513. if (!opcc_chk_reserve_expired(gunIndex))
  5514. {
  5515. PRINTF_FUNC("id = %d \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id);
  5516. PRINTF_FUNC("idToken = %s \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].idToken.idToken);
  5517. PRINTF_FUNC("*************ChkOcppStatus : OcppReservedStatus End****************** \n");
  5518. chargingInfo[gunIndex]->ReservationId = ShmOCPP20Data->ReserveNow[gunIndex].id;
  5519. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  5520. strncpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex],
  5521. (char *)ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].idToken.idToken, 30);
  5522. }
  5523. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowConf = YES;
  5524. }
  5525. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION &&
  5526. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq == YES)
  5527. {
  5528. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq = NO;
  5529. if (ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id == chargingInfo[gunIndex]->ReservationId)
  5530. {
  5531. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5532. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id);
  5533. PRINTF_FUNC("***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n");
  5534. chargingInfo[gunIndex]->ReservationId = -1;
  5535. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_IDLE;
  5536. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex], "");
  5537. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationConf = YES;
  5538. }
  5539. }
  5540. }
  5541. }
  5542. void ocpp_chk_availability_cmd(byte gunIndex)
  5543. {
  5544. byte type = 0; // 0 : none, 1 : operative, 2 inoperative
  5545. byte acDirIndex = 0;
  5546. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5547. if (gunIndex == 1)
  5548. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5549. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5550. {
  5551. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5552. {
  5553. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5554. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5555. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5556. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex + acDirIndex].Type, "Operative") == EQUAL)
  5557. {
  5558. if (isDb_ready)
  5559. DB_Update_Operactive(localDb, gunIndex + acDirIndex, true);
  5560. type = 1;
  5561. }
  5562. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex + acDirIndex].Type, "Inoperative") == EQUAL)
  5563. {
  5564. if (isDb_ready)
  5565. DB_Update_Operactive(localDb, gunIndex + acDirIndex, false);
  5566. type = 2;
  5567. }
  5568. }
  5569. }
  5570. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5571. {
  5572. if (ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5573. {
  5574. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5575. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5576. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5577. if(strcmp((char *)ShmOCPP20Data->ChangeAvailability[gunIndex + acDirIndex].operationalStatus, "Operative") == EQUAL)
  5578. {
  5579. if (isDb_ready)
  5580. DB_Update_Operactive(localDb, gunIndex + acDirIndex, true);
  5581. type = 1;
  5582. }
  5583. else if (strcmp((char *)ShmOCPP20Data->ChangeAvailability[gunIndex + acDirIndex].operationalStatus, "Inoperative") == EQUAL)
  5584. {
  5585. if (isDb_ready)
  5586. DB_Update_Operactive(localDb, gunIndex + acDirIndex, false);
  5587. type = 2;
  5588. }
  5589. }
  5590. }
  5591. if (type == 1)
  5592. {
  5593. chargingInfo[gunIndex]->IsAvailable = YES;
  5594. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5595. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION ||
  5596. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5597. {
  5598. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  5599. }
  5600. }
  5601. else if (type == 2)
  5602. {
  5603. chargingInfo[gunIndex]->IsAvailable = NO;
  5604. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5605. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION ||
  5606. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5607. {
  5608. SetChargerMode(gunIndex, SYS_MODE_MAINTAIN);
  5609. }
  5610. }
  5611. }
  5612. void ocpp_chk_unlock_cmd(byte gunIndex)
  5613. {
  5614. byte acDirIndex = 0;
  5615. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5616. if (gunIndex == 1)
  5617. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5618. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5619. {
  5620. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5621. {
  5622. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5623. if ((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5624. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) ||
  5625. (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  5626. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  5627. {
  5628. // Unlocked - 充電中,需停止充電
  5629. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "UnlockCommand");
  5630. ChargingTerminalProcess(gunIndex);
  5631. }
  5632. if (chargingInfo[gunIndex]->Type == _Type_Chademo ||
  5633. chargingInfo[gunIndex]->Type == _Type_GB)
  5634. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  5635. else
  5636. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  5637. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorConf = YES;
  5638. }
  5639. }
  5640. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5641. {
  5642. if (ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5643. {
  5644. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5645. if ((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5646. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) ||
  5647. (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  5648. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  5649. {
  5650. // Unlocked - 充電中,需停止充電
  5651. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "UnlockCommand");
  5652. ChargingTerminalProcess(gunIndex);
  5653. }
  5654. if (chargingInfo[gunIndex]->Type == _Type_Chademo ||
  5655. chargingInfo[gunIndex]->Type == _Type_GB)
  5656. strcpy((char*)ShmOCPP20Data->UnlockConnector[gunIndex + acDirIndex].Response_status, "Unlocked");
  5657. else
  5658. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  5659. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorConf = YES;
  5660. }
  5661. }
  5662. }
  5663. bool ocpp_chk_remoteStop_cmd(byte gunIndex)
  5664. {
  5665. bool result = false;
  5666. byte acDirIndex = 0;
  5667. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5668. if (gunIndex == 1)
  5669. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5670. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5671. result = ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStopTransactionReq;
  5672. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5673. result = ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStopTransactionReq;
  5674. if (result)
  5675. {
  5676. DEBUG_INFO_MSG("Remote Stop by OCPP (%d) \n", gunIndex + acDirIndex);
  5677. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5678. {
  5679. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "Remote");
  5680. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStopTransactionReq = NO;
  5681. }
  5682. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5683. {
  5684. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "Remote");
  5685. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStopTransactionReq = NO;
  5686. }
  5687. }
  5688. return result;
  5689. }
  5690. bool ocpp_sub_get_remote_start_transaction_req(byte gun_index)
  5691. {
  5692. bool result = false;
  5693. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5694. {
  5695. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  5696. }
  5697. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5698. {
  5699. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
  5700. }
  5701. if (result && ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5702. {
  5703. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag,
  5704. (char *)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag);
  5705. }
  5706. return result;
  5707. }
  5708. void ocpp_sub_set_remote_start_transaction_req(byte gunIndex, bool result)
  5709. {
  5710. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5711. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStartTransactionReq = result;
  5712. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5713. ShmOCPP20Data->CsMsg.bits[gunIndex].RequestStartTransactionReq = result;
  5714. }
  5715. void ocpp_chk_remoteStart_cmd()
  5716. {
  5717. if(!isDetectPlugin())
  5718. {
  5719. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  5720. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5721. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++)
  5722. {
  5723. if (ocpp_sub_get_remote_start_transaction_req(acDirIndex))
  5724. {
  5725. if (ac_chargingInfo[ac_index]->SystemStatus == SYS_MODE_IDLE ||
  5726. ac_chargingInfo[ac_index]->SystemStatus == SYS_MODE_RESERVATION)
  5727. {
  5728. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  5729. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5730. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  5731. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  5732. //ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  5733. DetectPluginStart();
  5734. return;
  5735. }
  5736. ocpp_sub_set_remote_start_transaction_req(acDirIndex, NO);
  5737. }
  5738. }
  5739. byte threeGunIndex = 0;
  5740. byte dcIndex = 0;
  5741. bool isGunUsingStatus = false;
  5742. byte scheduleIndex = 0;
  5743. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5744. {
  5745. // 如果有 AC 槍,且 DC 槍也有兩把
  5746. if (acDirIndex == 1 && _index == 1)
  5747. threeGunIndex = 1;
  5748. if (ocpp_sub_get_remote_start_transaction_req(_index + threeGunIndex))
  5749. dcIndex = _index;
  5750. if (chargingInfo[_index]->SystemStatus != SYS_MODE_IDLE)
  5751. {
  5752. isGunUsingStatus = true;
  5753. }
  5754. if (chargingInfo[_index]->schedule.isTriggerStart)
  5755. scheduleIndex = _index;
  5756. }
  5757. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  5758. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  5759. {
  5760. if (dcIndex == 0)
  5761. threeGunIndex = 0;
  5762. //ocpp_sub_set_remote_start_transaction_req(dcIndex + threeGunIndex, NO);
  5763. return;
  5764. }
  5765. if (dcIndex == 0)
  5766. threeGunIndex = 0;
  5767. if (ocpp_sub_get_remote_start_transaction_req(dcIndex + threeGunIndex))
  5768. {
  5769. if (chargingInfo[dcIndex]->SystemStatus == SYS_MODE_IDLE ||
  5770. chargingInfo[dcIndex]->SystemStatus == SYS_MODE_RESERVATION)
  5771. {
  5772. if (ocpp_authorizeRemoteTxReqChk_key())
  5773. {
  5774. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId,
  5775. (char *)ShmOCPP16Data->RemoteStartTransaction[dcIndex + threeGunIndex].IdTag);
  5776. }
  5777. else
  5778. {
  5779. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  5780. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5781. ChangeGunSelectByIndex(dcIndex);
  5782. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  5783. DetectPluginStart();
  5784. }
  5785. }
  5786. ocpp_sub_set_remote_start_transaction_req(dcIndex + threeGunIndex, NO);
  5787. }
  5788. else if (chargingInfo[scheduleIndex]->schedule.isTriggerStart)
  5789. {
  5790. if (chargingInfo[scheduleIndex]->SystemStatus == SYS_MODE_IDLE ||
  5791. chargingInfo[scheduleIndex]->SystemStatus == SYS_MODE_RESERVATION)
  5792. {
  5793. chargingInfo[scheduleIndex]->RemoteStartFlag = YES;
  5794. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5795. DetectPluginStart();
  5796. }
  5797. chargingInfo[scheduleIndex]->schedule.isTriggerStart = NO;
  5798. }
  5799. }
  5800. }
  5801. bool ocpp_remote_start_wait_cmd()
  5802. {
  5803. bool result = false;
  5804. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5805. {
  5806. result = ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  5807. }
  5808. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5809. {
  5810. result = ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  5811. }
  5812. return result;
  5813. }
  5814. void ocpp_chk_update_cmd()
  5815. {
  5816. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5817. {
  5818. if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  5819. {
  5820. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  5821. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  5822. {
  5823. DEBUG_INFO_MSG("Backend : update start. \n");
  5824. sleep(2);
  5825. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  5826. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  5827. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5828. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5829. {
  5830. SetChargerMode(_index, SYS_MODE_UPDATE);
  5831. }
  5832. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5833. {
  5834. ac_chargingInfo[_index]->SystemStatus = SYS_MODE_UPDATE;
  5835. }
  5836. CloseWatchDog();
  5837. sleep(2);
  5838. KillTask();
  5839. byte updateResult = CheckUpdateProcess();
  5840. if (updateResult == PASS)
  5841. {
  5842. DEBUG_INFO_MSG("Backend : update complete. \n");
  5843. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5844. }
  5845. else if (updateResult == MODELNAME_FAIL)
  5846. {
  5847. DEBUG_INFO_MSG("Backend : model name is none match. \n");
  5848. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5849. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5850. sleep(5);
  5851. KillAllTask();
  5852. system("/usr/bin/run_evse_restart.sh");
  5853. return;
  5854. }
  5855. else
  5856. {
  5857. DEBUG_INFO_MSG("Backend : update fail. \n");
  5858. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5859. }
  5860. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5861. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5862. sleep(5);
  5863. system("reboot -f");
  5864. }
  5865. }
  5866. }
  5867. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5868. {
  5869. if(ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES)
  5870. {
  5871. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = NO;
  5872. if (strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded") == EQUAL)
  5873. {
  5874. DEBUG_INFO_MSG("Backend : update start. \n");
  5875. sleep(2);
  5876. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "");
  5877. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installing");
  5878. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5879. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5880. {
  5881. SetChargerMode(_index, SYS_MODE_UPDATE);
  5882. }
  5883. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5884. {
  5885. ac_chargingInfo[_index]->SystemStatus = SYS_MODE_UPDATE;
  5886. }
  5887. CloseWatchDog();
  5888. sleep(2);
  5889. KillTask();
  5890. byte updateResult = CheckUpdateProcess();
  5891. if (updateResult == PASS)
  5892. {
  5893. DEBUG_INFO_MSG("Backend : update complete. \n");
  5894. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5895. }
  5896. else if (updateResult == MODELNAME_FAIL)
  5897. {
  5898. DEBUG_INFO_MSG("Backend : model name is none match. \n");
  5899. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5900. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5901. sleep(5);
  5902. KillAllTask();
  5903. system("/usr/bin/run_evse_restart.sh");
  5904. return;
  5905. }
  5906. else
  5907. {
  5908. DEBUG_INFO_MSG("Backend : update fail. \n");
  5909. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5910. }
  5911. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5912. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5913. sleep(5);
  5914. system("reboot -f");
  5915. }
  5916. }
  5917. }
  5918. }
  5919. bool ocpp_chk_invalid_id_cmd(byte gunIndex)
  5920. {
  5921. byte acDirIndex = 0;
  5922. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5923. if (gunIndex == 1)
  5924. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5925. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5926. {
  5927. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  5928. {
  5929. if (strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Blocked") == EQUAL ||
  5930. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Expired") == EQUAL ||
  5931. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Invalid") == EQUAL)
  5932. return true;
  5933. }
  5934. }
  5935. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5936. {
  5937. if (strstr((char *) ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE"))
  5938. {
  5939. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Blocked") == EQUAL ||
  5940. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Expired") == EQUAL ||
  5941. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Invalid") == EQUAL ||
  5942. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NoCredit") == EQUAL ||
  5943. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAllowedTypeEVSE") == EQUAL ||
  5944. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAtThisLocation") == EQUAL ||
  5945. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAtThisTime") == EQUAL ||
  5946. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Unknown") == EQUAL)
  5947. return true;
  5948. }
  5949. }
  5950. return false;
  5951. }
  5952. bool ocpp_chk_profileConf_cmd(byte gunIndex)
  5953. {
  5954. byte acDirIndex = 0;
  5955. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5956. if (gunIndex == 1)
  5957. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5958. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5959. {
  5960. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == YES)
  5961. {
  5962. ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf = NO;
  5963. return true;
  5964. }
  5965. }
  5966. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5967. {
  5968. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == YES)
  5969. {
  5970. ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf = NO;
  5971. return true;
  5972. }
  5973. }
  5974. return false;
  5975. }
  5976. void ocpp_chk_notificationMessage_cmd(byte gunIndex)
  5977. {
  5978. byte acDirIndex = 0;
  5979. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5980. if (gunIndex == 1)
  5981. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5982. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5983. {
  5984. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].TriggerMessageReq == YES)
  5985. {
  5986. if (chargingInfo[gunIndex]->Type == _Type_Chademo &&
  5987. (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5988. chargingInfo[gunIndex]->SystemStatus ==SYS_MODE_RESERVATION ||
  5989. chargingInfo[gunIndex]->SystemStatus ==SYS_MODE_MAINTAIN))
  5990. {
  5991. if (ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag == NO)
  5992. {
  5993. PRINTF_FUNC("Trigger msg (StatusNotification) = %d (chademo) \n", gunIndex);
  5994. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = 0;
  5995. GetTimespecFunc(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  5996. ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag = YES;
  5997. }
  5998. }
  5999. else if (chargingInfo[gunIndex]->Type == _Type_CCS ||
  6000. chargingInfo[gunIndex]->Type == _Type_GB ||
  6001. chargingInfo[gunIndex]->Type == _Type_AC)
  6002. {
  6003. PRINTF_FUNC("Trigger msg (StatusNotification) = %d (ccs) \n", gunIndex);
  6004. }
  6005. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].TriggerMessageReq = NO;
  6006. }
  6007. }
  6008. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6009. {
  6010. }
  6011. if (ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag)
  6012. {
  6013. int _timeBuf = GetTimeoutValue(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  6014. if (_timeBuf < 0)
  6015. {
  6016. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = 0;
  6017. GetTimespecFunc(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  6018. }
  6019. if (_timeBuf > 30 ||
  6020. (_timeBuf > 2 &&
  6021. chargingInfo[gunIndex]->ConnectorPlugIn))
  6022. {
  6023. PRINTF_FUNC("Trigger msg - timeout or Plugin. \n");
  6024. ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag = NO;
  6025. }
  6026. else
  6027. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = (_timeBuf / uSEC_VAL);
  6028. }
  6029. }
  6030. void ocpp_lift_profileReq_cmd(byte gunIndex)
  6031. {
  6032. byte acDirIndex = 0;
  6033. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6034. if (gunIndex == 1)
  6035. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6036. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6037. {
  6038. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == NO)
  6039. {
  6040. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq == NO)
  6041. ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq = YES;
  6042. }
  6043. }
  6044. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6045. {
  6046. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == NO)
  6047. {
  6048. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq == NO)
  6049. ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq = YES;
  6050. }
  6051. }
  6052. }
  6053. void ocpp_set_startTransation_cmd(byte gunIndex)
  6054. {
  6055. byte _OcppGunIndex = gunIndex;
  6056. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6057. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6058. _OcppGunIndex = 2;
  6059. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6060. {
  6061. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6062. strcpy((char *)chargingInfo[gunIndex]->StartUserId, (char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  6063. else
  6064. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6065. if (strcmp((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, "") == EQUAL)
  6066. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6067. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  6068. PRINTF_FUNC("Gun = %d, IdTag = %s \n", _OcppGunIndex, chargingInfo[gunIndex]->StartUserId);
  6069. }
  6070. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6071. {
  6072. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6073. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6074. else
  6075. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)chargingInfo[gunIndex]->StartUserId);
  6076. ShmOCPP20Data->CpMsg.bits[_OcppGunIndex].TransactionEventReq = YES;
  6077. PRINTF_FUNC("Gun = %d, IdTag = %s \n", _OcppGunIndex, ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6078. }
  6079. }
  6080. void ocpp_set_startTransationPH_cmd(byte gunIndex)
  6081. {
  6082. byte _OcppGunIndex = gunIndex;
  6083. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6084. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6085. _OcppGunIndex = 2;
  6086. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  6087. {
  6088. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6089. {
  6090. if (strcmp((char *)ShmOCPP16DataPH->StartTransaction[_OcppGunIndex].IdTag, "") == EQUAL)
  6091. strcpy((char *)ShmOCPP16DataPH->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6092. ShmOCPP16DataPH->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  6093. }
  6094. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6095. {
  6096. }
  6097. }
  6098. }
  6099. void ocpp_set_stopTransation_cmd(byte gunIndex)
  6100. {
  6101. byte _OcppGunIndex = gunIndex;
  6102. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6103. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6104. _OcppGunIndex = 2;
  6105. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6106. {
  6107. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6108. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  6109. else
  6110. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6111. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  6112. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  6113. }
  6114. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6115. {
  6116. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6117. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6118. else
  6119. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)chargingInfo[gunIndex]->StartUserId);
  6120. PRINTF_FUNC("IdTag = %s \n", ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6121. ShmOCPP20Data->CpMsg.bits[_OcppGunIndex].TransactionEventReq = YES;
  6122. }
  6123. }
  6124. void ocpp_set_stopTransationPH_cmd(byte gunIndex)
  6125. {
  6126. byte _OcppGunIndex = gunIndex;
  6127. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6128. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6129. _OcppGunIndex = 2;
  6130. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  6131. {
  6132. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6133. {
  6134. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6135. strcpy((char *)ShmOCPP16DataPH->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16DataPH->StopTransaction[_OcppGunIndex].IdTag);
  6136. else
  6137. strcpy((char *)ShmOCPP16DataPH->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6138. ShmOCPP16DataPH->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  6139. }
  6140. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6141. {
  6142. }
  6143. }
  6144. }
  6145. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString)
  6146. {
  6147. // 參數 gunIndex 已包含 AC 槍
  6148. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6149. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, errString);
  6150. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6151. {
  6152. }
  6153. }
  6154. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason)
  6155. {
  6156. byte acDirIndex = 0;
  6157. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6158. if (gunIndex == 1)
  6159. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6160. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6161. {
  6162. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "") == EQUAL)
  6163. {
  6164. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, reason);
  6165. }
  6166. }
  6167. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6168. {
  6169. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "") == EQUAL)
  6170. {
  6171. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, reason);
  6172. }
  6173. }
  6174. }
  6175. void ocpp_set_authorizeReq_cmd(byte value)
  6176. {
  6177. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6178. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = value;
  6179. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6180. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = value;
  6181. }
  6182. void ocpp_set_authorizeConf_cmd(byte value)
  6183. {
  6184. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6185. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = value;
  6186. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6187. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = value;
  6188. }
  6189. void ocpp_set_errCode_cmd(byte gunIndex)
  6190. {
  6191. byte acDirIndex = 0;
  6192. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6193. if (gunIndex == 1)
  6194. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6195. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6196. {
  6197. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  6198. {
  6199. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "InternalError");
  6200. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, (char *)chargingInfo[gunIndex]->ConnectorAlarmCode);
  6201. }
  6202. else if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  6203. {
  6204. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "OtherError");
  6205. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, (char *)chargingInfo[gunIndex]->EvConnAlarmCode);
  6206. }
  6207. else
  6208. {
  6209. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError");
  6210. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "");
  6211. }
  6212. }
  6213. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6214. {
  6215. }
  6216. }
  6217. void ocpp_clear_errorCode_cmd(byte gunIndex)
  6218. {
  6219. byte acDirIndex = 0;
  6220. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6221. if (gunIndex == 1)
  6222. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6223. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6224. {
  6225. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError") != EQUAL)
  6226. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError");
  6227. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "") != EQUAL)
  6228. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "");
  6229. }
  6230. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6231. {
  6232. }
  6233. }
  6234. void ocpp_default_errorCode_cmd(byte gunIndex)
  6235. {
  6236. byte acDirIndex = 0;
  6237. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6238. if (gunIndex == 1)
  6239. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6240. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6241. {
  6242. if (strcmp ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "" ) == EQUAL)
  6243. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "NoError" );
  6244. }
  6245. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6246. {
  6247. }
  6248. }
  6249. void ocpp_clear_idTag_cmd(byte gun_index)
  6250. {
  6251. byte acDirIndex = 0;
  6252. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6253. if (gun_index == 1)
  6254. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6255. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6256. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index + acDirIndex].ResponseIdTagInfo.Status, "");
  6257. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6258. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].Response_idTokenInfo.status, "");
  6259. }
  6260. void ocpp_clear_stopReason(byte gun_index)
  6261. {
  6262. byte acDirIndex = 0;
  6263. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6264. if (gun_index == 1)
  6265. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6266. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6267. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason, "");
  6268. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6269. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].transactionInfo.stoppedReason, "");
  6270. }
  6271. void ocpp_clear_stopTansaction_Idtag(byte gun_index)
  6272. {
  6273. byte acDirIndex = 0;
  6274. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6275. if (gun_index == 1)
  6276. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6277. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6278. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].IdTag, "");
  6279. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6280. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].idToken.idToken, "");
  6281. }
  6282. unsigned short _ocpp_get_connect_timeout()
  6283. {
  6284. uint16_t result = (TIMEOUT_SPEC_HANDSHAKING / 1000);
  6285. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6286. {
  6287. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  6288. result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  6289. }
  6290. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6291. {
  6292. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
  6293. result = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  6294. }
  6295. return result;
  6296. }
  6297. bool ocpp_get_authorize_conf()
  6298. {
  6299. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6300. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  6301. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6302. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  6303. return false;
  6304. }
  6305. bool ocpp_get_startTransation_req(byte gunIndex)
  6306. {
  6307. bool result = false;
  6308. byte acDirIndex = 0;
  6309. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6310. if (gunIndex == 1)
  6311. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6312. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6313. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6314. {
  6315. result = ShmOCPP16Data->CpMsg.bits[gunIndex + acDirIndex].StartTransactionReq;
  6316. }
  6317. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6318. {
  6319. result = ShmOCPP20Data->CpMsg.bits[gunIndex + acDirIndex].TransactionEventReq;
  6320. }
  6321. return result;
  6322. }
  6323. void ocpp_get_freeVendor(byte gunIndex)
  6324. {
  6325. byte _OcppGunIndex = gunIndex;
  6326. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6327. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6328. _OcppGunIndex = 2;
  6329. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6330. {
  6331. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, "");
  6332. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag,
  6333. (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
  6334. }
  6335. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6336. {
  6337. if (strlen((char *)ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value) > 0)
  6338. {
  6339. }
  6340. else
  6341. {
  6342. }
  6343. }
  6344. }
  6345. void ocpp_parsing_DT_defaultPrice_cmd()
  6346. {
  6347. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6348. {
  6349. if(strcmp(MyDefaultPriceString, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData) != EQUAL)
  6350. {
  6351. DefaultPriceHandlerOcpp16((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  6352. memcpy(MyDefaultPriceString, ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, 128);
  6353. }
  6354. }
  6355. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6356. {
  6357. //ShmOCPP20Data->CostUpdated
  6358. }
  6359. }
  6360. void ocpp_parsing_DT_userPrice_cmd()
  6361. {
  6362. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6363. {
  6364. if (strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken) > 0 &&
  6365. strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.price) > 0)
  6366. {
  6367. UserPriceHandlerOcpp16((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken,
  6368. (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  6369. //memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  6370. }
  6371. }
  6372. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6373. {
  6374. //ShmOCPP20Data->CostUpdated
  6375. }
  6376. }
  6377. void ocpp_parsing_DT_running_final_cmd()
  6378. {
  6379. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6380. {
  6381. RunningFinalCostHandlerOcpp16();
  6382. }
  6383. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6384. {
  6385. //ShmOCPP20Data->CostUpdated
  6386. }
  6387. }
  6388. void ocpp_chargingProfile_process(byte _index)
  6389. {
  6390. int _time = 0;
  6391. int _startCount = NO_DEFINE;
  6392. int _maxCount = 0;
  6393. byte acDirIndex = 0;
  6394. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6395. if (_index == 1)
  6396. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6397. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6398. {
  6399. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingProfileKind, "Absolute") == EQUAL &&
  6400. ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingProfileId == YES)
  6401. {
  6402. _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.StartSchedule);
  6403. if (_time > -1)
  6404. {
  6405. _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod);
  6406. _startCount = NO_DEFINE;
  6407. for (byte _count = 0; _count < _maxCount; _count++)
  6408. {
  6409. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  6410. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  6411. {
  6412. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  6413. ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  6414. {
  6415. _startCount = _count;
  6416. }
  6417. }
  6418. }
  6419. if (_startCount < _maxCount)
  6420. {
  6421. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount);
  6422. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  6423. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  6424. chargingInfo[_index]->PresentChargingVoltage > 0)
  6425. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  6426. else
  6427. chargingInfo[_index]->ChargingProfileCurrent = 0;
  6428. }
  6429. else
  6430. {
  6431. chargingInfo[_index]->ChargingProfilePower = -1;
  6432. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6433. }
  6434. }
  6435. else
  6436. {
  6437. chargingInfo[_index]->ChargingProfilePower = -1;
  6438. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6439. }
  6440. }
  6441. else
  6442. {
  6443. chargingInfo[_index]->ChargingProfilePower = -1;
  6444. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6445. }
  6446. if (strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == EQUAL &&
  6447. ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES)
  6448. {
  6449. _time = GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  6450. if (_time > -1)
  6451. {
  6452. _maxCount = ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  6453. _startCount = NO_DEFINE;
  6454. for (byte _count = 0; _count < _maxCount; _count++)
  6455. {
  6456. if (_time >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  6457. {
  6458. if ((_count == 0 && ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  6459. ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  6460. {
  6461. _startCount = _count;
  6462. }
  6463. }
  6464. }
  6465. //PRINTF_FUNC("_startCount = %d, Max Profile Limit = %f \n", _startCount, ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  6466. if (_startCount < _maxCount)
  6467. {
  6468. float maxChargingProfilePow = ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  6469. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_BOTH)
  6470. maxChargingProfilePow /= 2;
  6471. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = maxChargingProfilePow;
  6472. }
  6473. else
  6474. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6475. }
  6476. }
  6477. else
  6478. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6479. }
  6480. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6481. {
  6482. if (strcmp((char *)ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingProfileKind, "Absolute") == EQUAL &&
  6483. ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].id == YES)
  6484. {
  6485. _time = GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].startSchedule);
  6486. _maxCount = ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod);
  6487. _startCount = NO_DEFINE;
  6488. for (byte _count = 0; _count < _maxCount; _count++)
  6489. {
  6490. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  6491. if (_time >= ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].startPeriod)
  6492. {
  6493. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].limit >= MIN_OUTPUT_CUR) ||
  6494. ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].limit > MIN_OUTPUT_CUR)
  6495. {
  6496. _startCount = _count;
  6497. }
  6498. }
  6499. }
  6500. if (_startCount < _maxCount)
  6501. {
  6502. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount);
  6503. //PRINTF_FUNC("Profile Limit = %f \n", ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit);
  6504. chargingInfo[_index]->ChargingProfilePower = ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit * AC_OUTPUT_VOL;
  6505. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 && chargingInfo[_index]->PresentChargingVoltage > 0)
  6506. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  6507. else
  6508. chargingInfo[_index]->ChargingProfileCurrent = 0;
  6509. }
  6510. else
  6511. {
  6512. chargingInfo[_index]->ChargingProfilePower = -1;
  6513. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6514. }
  6515. }
  6516. else
  6517. {
  6518. chargingInfo[_index]->ChargingProfilePower = -1;
  6519. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6520. }
  6521. }
  6522. else
  6523. {
  6524. chargingInfo[_index]->ChargingProfilePower = -1;
  6525. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6526. }
  6527. if (chargingInfo[_index]->ChargingProfilePower >= 0 &&
  6528. _ChargingProfilePwBuffer[_index] != chargingInfo[_index]->ChargingProfilePower)
  6529. {
  6530. _ChargingProfilePwBuffer[_index] = chargingInfo[_index]->ChargingProfilePower;
  6531. PRINTF_FUNC("Profile : ChargingProfilePower = %.2f \n", chargingInfo[_index]->ChargingProfilePower);
  6532. PRINTF_FUNC("Profile : ChargingProfileCurrent = %.2f \n", chargingInfo[_index]->ChargingProfileCurrent);
  6533. }
  6534. }
  6535. void CheckOcppProfileConfirm(byte gun_index)
  6536. {
  6537. if ((chargingInfo [gun_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo [gun_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  6538. (chargingInfo [gun_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo [gun_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  6539. {
  6540. ocpp_chk_profileConf_cmd ( gun_index );
  6541. }
  6542. }
  6543. void CheckAcOcppProfileConfirm()
  6544. {
  6545. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING &&
  6546. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  6547. {
  6548. ocpp_ac_chk_profileConf_cmd();
  6549. }
  6550. }
  6551. //===============================================
  6552. // wifi schedule
  6553. //===============================================
  6554. bool WifiScheduleStop(byte gunIndex)
  6555. {
  6556. bool result = false;
  6557. result = chargingInfo[gunIndex]->schedule.isTriggerStop;
  6558. chargingInfo[gunIndex]->schedule.isTriggerStop = NO;
  6559. return result;
  6560. }
  6561. //===============================================
  6562. // SQLite3 related routine
  6563. //===============================================
  6564. int NetworkDB_Open(sqlite3 *db)
  6565. {
  6566. int result = PASS;
  6567. char* errMsg = NULL;
  6568. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  6569. "idx integer primary key AUTOINCREMENT, "
  6570. "occurDatetime text NOT NULL, "
  6571. "isInternet text NOT NULL, "
  6572. "isOcppConnected text NOT NULL, "
  6573. "isEth0Internet text NOT NULL, "
  6574. "isMlan0Internet text NOT NULL, "
  6575. "isPpp0Internet text NOT NULL, "
  6576. "rssiMlan0 text NOT NULL, "
  6577. "rssiPpp0 text NOT NULL"
  6578. ");";
  6579. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  6580. if(sqlite3_open(NETWORK_DB_FILE, &db))
  6581. {
  6582. result = FAIL;
  6583. PRINTF_FUNC( "Can't open database (Net) : %s\n", sqlite3_errmsg(db));
  6584. sqlite3_close(db);
  6585. }
  6586. else
  6587. {
  6588. PRINTF_FUNC( "Local network status record database open successfully.\n");
  6589. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6590. {
  6591. result = FAIL;
  6592. PRINTF_FUNC( "Create local network status record table error message: %s\n", errMsg);
  6593. }
  6594. else
  6595. {
  6596. PRINTF_FUNC( "Opened local network status record table successfully\n");
  6597. }
  6598. sqlite3_close(db);
  6599. }
  6600. return result;
  6601. }
  6602. int DB_Open(sqlite3 *db)
  6603. {
  6604. int result = PASS;
  6605. char* errMsg = NULL;
  6606. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  6607. "idx integer primary key AUTOINCREMENT, "
  6608. "reservationId text, "
  6609. "transactionId text, "
  6610. "startMethod text, "
  6611. "userId text, "
  6612. "dateTimeStart text, "
  6613. "dateTimeStop text,"
  6614. "socStart text, "
  6615. "socStop text, "
  6616. "chargeEnergy text, "
  6617. "stopReason text,"
  6618. "vincode text"
  6619. ");";
  6620. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  6621. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6622. "`item` TEXT NOT NULL, "
  6623. "`connector` INTEGER NOT NULL, "
  6624. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  6625. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  6626. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6627. "`occurDatetime` TEXT NOT NULL, "
  6628. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  6629. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  6630. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6631. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  6632. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  6633. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6634. "`connector` INTEGER NOT NULL, "
  6635. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  6636. // char * createTestSql = "CREATE TABLE IF NOT EXISTS `for_test` ( "
  6637. // "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6638. // "`connector` INTEGER NOT NULL, "
  6639. // "`testID` TEXT, "
  6640. // "`checkStatus` INTEGER NOT NULL "
  6641. // ");";
  6642. if(sqlite3_open(DB_FILE, &db))
  6643. {
  6644. result = FAIL;
  6645. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6646. sqlite3_close(db);
  6647. }
  6648. else
  6649. {
  6650. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  6651. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6652. {
  6653. result = FAIL;
  6654. PRINTF_FUNC( "Create local charging record table error message: %s\n", errMsg);
  6655. }
  6656. else
  6657. {
  6658. PRINTF_FUNC( "Opened local charging record table successfully\n");
  6659. }
  6660. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  6661. {
  6662. result = FAIL;
  6663. PRINTF_FUNC( "Create local config table error message: %s\n", errMsg);
  6664. }
  6665. else
  6666. {
  6667. PRINTF_FUNC( "Opened local config table successfully\n");
  6668. }
  6669. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6670. {
  6671. result = FAIL;
  6672. PRINTF_FUNC( "Create local record table error message: %s\n", errMsg);
  6673. }
  6674. else
  6675. {
  6676. PRINTF_FUNC( "Opened local record table successfully\n");
  6677. }
  6678. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  6679. {
  6680. result = FAIL;
  6681. PRINTF_FUNC( "Create reboot record table error message: %s\n", errMsg);
  6682. }
  6683. else
  6684. {
  6685. PRINTF_FUNC( "Opened reboot record table successfully\n");
  6686. }
  6687. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  6688. {
  6689. result = FAIL;
  6690. PRINTF_FUNC( "Create power consumption record table error message: %s\n", errMsg);
  6691. }
  6692. else
  6693. {
  6694. PRINTF_FUNC( "Opened power consumption record table successfully\n");
  6695. }
  6696. // if (sqlite3_exec(db, createTestSql, 0, 0, &errMsg) != SQLITE_OK)
  6697. // {
  6698. // result = FAIL;
  6699. // printf( "Create test table error message: %s\n", errMsg);
  6700. // }
  6701. // else
  6702. // {
  6703. // printf( "Opened test table successfully\n");
  6704. // }
  6705. sqlite3_close(db);
  6706. }
  6707. return result;
  6708. }
  6709. int DB_Insert_Record(sqlite3 *db, int gun_index)
  6710. {
  6711. int result = PASS;
  6712. char* errMsg = NULL;
  6713. char insertSql[1024];
  6714. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6715. {
  6716. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, vincode) "
  6717. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  6718. chargingInfo[gun_index]->ReservationId,
  6719. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  6720. chargingInfo[gun_index]->StartMethod,
  6721. chargingInfo[gun_index]->StartUserId,
  6722. chargingInfo[gun_index]->StartDateTime,
  6723. chargingInfo[gun_index]->StopDateTime,
  6724. chargingInfo[gun_index]->EvBatteryStartSoc,
  6725. chargingInfo[gun_index]->EvBatterySoc,
  6726. chargingInfo[gun_index]->PresentChargedEnergy,
  6727. ShmOCPP16Data->StopTransaction[gun_index].StopReason,
  6728. chargingInfo[gun_index]->EVCCID);
  6729. }
  6730. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6731. {
  6732. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  6733. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  6734. chargingInfo[gun_index]->ReservationId,
  6735. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  6736. chargingInfo[gun_index]->StartMethod,
  6737. chargingInfo[gun_index]->StartUserId,
  6738. chargingInfo[gun_index]->StartDateTime,
  6739. chargingInfo[gun_index]->StopDateTime,
  6740. chargingInfo[gun_index]->EvBatteryStartSoc,
  6741. chargingInfo[gun_index]->EvBatterySoc,
  6742. chargingInfo[gun_index]->PresentChargedEnergy,
  6743. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  6744. }
  6745. if(sqlite3_open(DB_FILE, &db))
  6746. {
  6747. result = FAIL;
  6748. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6749. sqlite3_close(db);
  6750. }
  6751. else
  6752. {
  6753. PRINTF_FUNC( "Local charging record database open successfully.\n");
  6754. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6755. {
  6756. result = FAIL;
  6757. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  6758. }
  6759. else
  6760. {
  6761. PRINTF_FUNC( "Insert local charging record successfully\n");
  6762. }
  6763. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  6764. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6765. {
  6766. result = FAIL;
  6767. PRINTF_FUNC( "delete local charging error message: %s\n", errMsg);
  6768. }
  6769. else
  6770. {
  6771. PRINTF_FUNC( "delete local charging record successfully\n");
  6772. }
  6773. sqlite3_close(db);
  6774. }
  6775. return result;
  6776. }
  6777. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  6778. {
  6779. uint8_t result = false;
  6780. char* errMsg = NULL;
  6781. char sqlStr[1024];
  6782. srand(time(NULL));
  6783. if(sqlite3_open(DB_FILE, &db))
  6784. {
  6785. result = FAIL;
  6786. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6787. sqlite3_close(db);
  6788. }
  6789. else
  6790. {
  6791. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  6792. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  6793. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  6794. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6795. {
  6796. result = FAIL;
  6797. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  6798. }
  6799. else
  6800. {
  6801. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  6802. }
  6803. sqlite3_close(db);
  6804. }
  6805. return result;
  6806. }
  6807. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  6808. {
  6809. uint8_t result = true;
  6810. char* errMsg = NULL;
  6811. char sqlStr[1024];
  6812. char **rs;
  6813. int rows, cols;
  6814. sprintf(sqlStr, "select * from config where item='IsAvailable' and connector=%d;", gun_index);
  6815. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  6816. if(sqlite3_open(DB_FILE, &db))
  6817. {
  6818. result = FAIL;
  6819. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6820. sqlite3_close(db);
  6821. }
  6822. else
  6823. {
  6824. PRINTF_FUNC( "Local config query database open successfully.\r\n");
  6825. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  6826. if(rows>0)
  6827. {
  6828. for(int idxRow=1;idxRow<=rows;idxRow++)
  6829. {
  6830. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  6831. {
  6832. result = false;
  6833. }
  6834. PRINTF_FUNC("Query connector-%d isOperactive: %s\r\n", gun_index, rs[(idxRow*cols)+3]);
  6835. }
  6836. }
  6837. else
  6838. {
  6839. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.\r\n", gun_index);
  6840. }
  6841. sqlite3_free_table(rs);
  6842. sqlite3_close(db);
  6843. }
  6844. return result;
  6845. }
  6846. void DB_Update_PowerConsumption(sqlite3 *db, uint8_t gun_index, float value)
  6847. {
  6848. char* errMsg = NULL;
  6849. char sqlStr[1024];
  6850. srand(time(NULL));
  6851. if(sqlite3_open(DB_FILE, &db))
  6852. {
  6853. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6854. sqlite3_close(db);
  6855. }
  6856. else
  6857. {
  6858. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  6859. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6860. {
  6861. PRINTF_FUNC( "update power_consumption error message: %s\n", errMsg);
  6862. }
  6863. else
  6864. {
  6865. //PRINTF_FUNC("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  6866. }
  6867. sqlite3_close(db);
  6868. }
  6869. }
  6870. float DB_Get_PowerConsumption(sqlite3 *db, uint8_t gun_index)
  6871. {
  6872. float result = 0;
  6873. char* errMsg = NULL;
  6874. char sqlStr[1024];
  6875. char **rs;
  6876. int rows, cols;
  6877. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  6878. if (sqlite3_open(DB_FILE, &db))
  6879. {
  6880. result = FAIL;
  6881. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  6882. sqlite3_close(db);
  6883. }
  6884. else
  6885. {
  6886. PRINTF_FUNC("Local config query database open successfully.\r\n");
  6887. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  6888. if (rows > 0)
  6889. {
  6890. for (int idxRow = 1; idxRow <= rows; idxRow++)
  6891. {
  6892. result = atof(rs[(idxRow * cols) + 2]);
  6893. }
  6894. }
  6895. else
  6896. {
  6897. PRINTF_FUNC( "Query connector-%d fail, set default value to consumption.\r\n", gun_index);
  6898. }
  6899. sqlite3_free_table(rs);
  6900. sqlite3_close(db);
  6901. }
  6902. return result;
  6903. }
  6904. int DB_Reboot_Record(sqlite3 *db)
  6905. {
  6906. int result = PASS;
  6907. char* errMsg = NULL;
  6908. char insertSql[256];
  6909. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  6910. if(sqlite3_open(DB_FILE, &db))
  6911. {
  6912. result = FAIL;
  6913. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6914. sqlite3_close(db);
  6915. }
  6916. else
  6917. {
  6918. PRINTF_FUNC( "Local charging record database open successfully.\n");
  6919. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6920. {
  6921. result = FAIL;
  6922. PRINTF_FUNC( "Insert reboot record error message: %s\n", errMsg);
  6923. }
  6924. else
  6925. {
  6926. PRINTF_FUNC( "Insert reboot record successfully\n");
  6927. }
  6928. sprintf(insertSql, "delete from reboot_record where idx < (select idx from reboot_record order by idx desc limit 1)-2000;");
  6929. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6930. {
  6931. result = FAIL;
  6932. PRINTF_FUNC( "delete reboot record error message: %s\n", errMsg);
  6933. }
  6934. else
  6935. {
  6936. PRINTF_FUNC( "delete reboot record successfully\n");
  6937. }
  6938. sqlite3_close(db);
  6939. }
  6940. return result;
  6941. }
  6942. int DB_Insert_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  6943. {
  6944. int result = PASS;
  6945. char* errMsg = NULL;
  6946. char insertSql[1024];
  6947. sprintf(insertSql, "insert into for_test (connector, testID, checkStatus) values(%d, %s, 0);",
  6948. gun_index, textID);
  6949. if(sqlite3_open(DB_FILE, &db))
  6950. {
  6951. result = FAIL;
  6952. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6953. sqlite3_close(db);
  6954. }
  6955. else
  6956. {
  6957. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6958. {
  6959. result = FAIL;
  6960. PRINTF_FUNC( "Insert sdlu test error message : %s\n", errMsg);
  6961. }
  6962. else
  6963. {
  6964. PRINTF_FUNC( "Insert sdlu test successfully\n");
  6965. }
  6966. sqlite3_close(db);
  6967. }
  6968. return result;
  6969. }
  6970. int DB_delete_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  6971. {
  6972. int result = PASS;
  6973. char* errMsg = NULL;
  6974. char insertSql[1024];
  6975. sprintf(insertSql, "delete from for_test where testID = %s", textID);
  6976. if (sqlite3_open(DB_FILE, &db))
  6977. {
  6978. result = FAIL;
  6979. PRINTF_FUNC("Can't open database: %s\n", sqlite3_errmsg(db));
  6980. sqlite3_close(db);
  6981. }
  6982. else
  6983. {
  6984. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6985. {
  6986. result = FAIL;
  6987. PRINTF_FUNC( "Test message 2: %s\n", errMsg);
  6988. }
  6989. else
  6990. {
  6991. PRINTF_FUNC( "Test message 2 successfully\n");
  6992. }
  6993. sqlite3_close(db);
  6994. }
  6995. return result;
  6996. }
  6997. int DB_sql_test(sqlite3 *db, uint8_t gun_index)
  6998. {
  6999. int result = PASS;
  7000. char* errMsg = NULL;
  7001. char insertSql[1024];
  7002. char valueStr[128];
  7003. char **rs;
  7004. int rows, cols;
  7005. sprintf(insertSql, "select testID from for_test where checkStatus=0;");
  7006. if (sqlite3_open(DB_FILE, &db))
  7007. {
  7008. result = FAIL;
  7009. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  7010. sqlite3_close(db);
  7011. }
  7012. else
  7013. {
  7014. PRINTF_FUNC("Local for_test query database open successfully.\r\n");
  7015. sqlite3_get_table(db, insertSql, &rs, &rows, &cols, &errMsg);
  7016. if (rows > 0)
  7017. {
  7018. for (int idxRow = 1; idxRow <= rows; idxRow++)
  7019. {
  7020. strcpy(valueStr, rs[(idxRow * cols)]);
  7021. printf("valueStr = %s \n", valueStr);
  7022. }
  7023. }
  7024. else
  7025. {
  7026. PRINTF_FUNC("select testID from for_test fail \r\n");
  7027. }
  7028. sqlite3_free_table(rs);
  7029. sqlite3_close(db);
  7030. }
  7031. return result;
  7032. }
  7033. //===============================================
  7034. // Config process
  7035. //===============================================
  7036. void AddPlugInTimes(byte gunIndex)
  7037. {
  7038. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  7039. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  7040. else if(chargingInfo[gunIndex]->Type == _Type_CCS)
  7041. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  7042. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  7043. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  7044. }
  7045. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  7046. {
  7047. char cmdBuf[32];
  7048. struct timeb csuTime;
  7049. struct tm *tmCSU;
  7050. ftime(&csuTime);
  7051. tmCSU = localtime(&csuTime.time);
  7052. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  7053. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  7054. tmCSU->tm_sec);
  7055. if (isStart)
  7056. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  7057. else
  7058. {
  7059. if (strcmp((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00") != EQUAL)
  7060. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  7061. }
  7062. }
  7063. void zipLogFiles()
  7064. {
  7065. const char* logPath = "/Storage/SystemLog";
  7066. // 獲取目錄
  7067. DIR* pDir = opendir(logPath);
  7068. if (pDir != NULL)
  7069. {
  7070. struct timeb csuTime;
  7071. struct tm *tmCSU;
  7072. ftime(&csuTime);
  7073. tmCSU = localtime(&csuTime.time);
  7074. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  7075. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  7076. // tmCSU->tm_sec);
  7077. // Read items inside the folder
  7078. struct dirent* pEntry = NULL;
  7079. while ((pEntry = readdir(pDir)) != NULL)
  7080. {
  7081. if (strcmp(pEntry->d_name, ".") != 0 &&
  7082. strcmp(pEntry->d_name, "..") != 0 &&
  7083. strncmp(pEntry->d_name, "[", 1) == 0 &&
  7084. strstr(pEntry->d_name, "tar") < 0)
  7085. {
  7086. char yearC[5];
  7087. unsigned short year = 0;
  7088. char monthC[3];
  7089. unsigned short month = 0;
  7090. yearC[4] = '\0';
  7091. strncpy(yearC, pEntry->d_name + 1, 4);
  7092. monthC[2] = '\0';
  7093. strncpy(monthC, pEntry->d_name + 6, 2);
  7094. year = atoi(yearC);
  7095. month = atoi(monthC);
  7096. if (year != 0)
  7097. {
  7098. if (year < tmCSU->tm_year + 1900 ||
  7099. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  7100. {
  7101. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  7102. char file[256];
  7103. memset(file, 0x00, sizeof(file));
  7104. strcat(file, "tar zcvf ");
  7105. strcat(file, logPath);
  7106. strncat(file, "/", 1);
  7107. strcat(file, pEntry->d_name);
  7108. strcat(file, ".tar");
  7109. strncat(file, " ", 1);
  7110. strcat(file, logPath);
  7111. strncat(file, "/", 1);
  7112. strcat(file, pEntry->d_name);
  7113. PRINTF_FUNC("zip = %s \n", file);
  7114. system(file);
  7115. }
  7116. }
  7117. }
  7118. }
  7119. }
  7120. // Close folder
  7121. closedir(pDir);
  7122. }
  7123. void ChangeGunSelectByIndex(byte sel)
  7124. {
  7125. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  7126. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  7127. }
  7128. void CheckIsAlternatvieByModelName()
  7129. {
  7130. if(ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'W')
  7131. {
  7132. // 壁掛
  7133. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  7134. }
  7135. else
  7136. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  7137. }
  7138. void StopProcessingLoop()
  7139. {
  7140. ocpp_process_start();
  7141. for (;;)
  7142. {
  7143. CheckTask();
  7144. CheckFactoryConfigFunction();
  7145. CheckFwUpdateFunction();
  7146. ocpp_chk_reset_cmd(YES);
  7147. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  7148. {
  7149. // 經過底下的 function 後,ShmSysConfigAndInfo->SysWarningInfo.Level 會再被更新一次
  7150. ChkPrimaryStatus();
  7151. ButtonTriggerEvent();
  7152. ReviewCriticalAlarm();
  7153. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_NORMAL &&
  7154. ShmDcCommonData->rebootCount < 1)
  7155. {
  7156. PRINTF_FUNC("Soft reboot for retry self-test (Primary). \n");
  7157. SetupRebootCount(1);
  7158. KillAllTask();
  7159. sleep(3);
  7160. system("/usr/bin/run_evse_restart.sh");
  7161. return;
  7162. }
  7163. }
  7164. sleep(1);
  7165. }
  7166. }
  7167. void CreateWatchdog()
  7168. {
  7169. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  7170. {
  7171. wtdFd = InitWatchDog();
  7172. if (wtdFd < 0)
  7173. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  7174. }
  7175. }
  7176. bool IsConnectorWholeIdle()
  7177. {
  7178. bool result = true;
  7179. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  7180. {
  7181. if (chargingInfo[count]->SystemStatus != SYS_MODE_IDLE &&
  7182. chargingInfo[count]->SystemStatus != SYS_MODE_RESERVATION)
  7183. {
  7184. result = false;
  7185. break;
  7186. }
  7187. }
  7188. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  7189. {
  7190. if (ac_chargingInfo[count]->SystemStatus != SYS_MODE_IDLE &&
  7191. ac_chargingInfo[count]->IsErrorOccur == NO)
  7192. {
  7193. result = false;
  7194. break;
  7195. }
  7196. }
  7197. return result;
  7198. }
  7199. void ClearAlarmCodeWhenAcOff()
  7200. {
  7201. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  7202. {
  7203. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  7204. }
  7205. }
  7206. //==========================================
  7207. // Check task processing
  7208. //==========================================
  7209. void CheckTask()
  7210. {
  7211. if(reset4gStep == RESET_4G_STEP_NONE &&
  7212. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' ||
  7213. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  7214. {
  7215. if(system("pidof -s Module_4g > /dev/null") != 0)
  7216. {
  7217. DEBUG_ERROR_MSG("Module_4g not running, restart it.\r\n");
  7218. system("/root/Module_4g &");
  7219. }
  7220. }
  7221. if(reset4gStep == RESET_4G_STEP_NONE &&
  7222. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' ||
  7223. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  7224. {
  7225. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  7226. {
  7227. DEBUG_ERROR_MSG("Module_Wifi not running, restart it.\r\n");
  7228. system("/root/Module_Wifi &");
  7229. }
  7230. }
  7231. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'P')
  7232. {
  7233. if(system("pidof -s Module_DcMeter > /dev/null") != 0)
  7234. {
  7235. DEBUG_ERROR_MSG("Module_DcMeter not running, restart it.\r\n");
  7236. system("/root/Module_DcMeter &");
  7237. }
  7238. }
  7239. ocpp_process_start();
  7240. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  7241. {
  7242. DEBUG_ERROR_MSG("Module_ProduceUtils not running, restart it.\r\n");
  7243. system ("/root/Module_ProduceUtils &");
  7244. }
  7245. }
  7246. void Check267Alarm()
  7247. {
  7248. bool canRun = true;
  7249. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7250. {
  7251. if((chargingInfo[_index]->SystemStatus > SYS_MODE_IDLE && chargingInfo[_index]->SystemStatus < SYS_MODE_TERMINATING) ||
  7252. (chargingInfo[_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7253. {
  7254. canRun = false;
  7255. break;
  7256. }
  7257. }
  7258. if (canRun)
  7259. {
  7260. if ( ! ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  7261. {
  7262. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  7263. }
  7264. }
  7265. }
  7266. void CheckSystemTaskAlive()
  7267. {
  7268. unsigned char lostId = CheckSystemTask(ShmSysConfigAndInfo->SysInfo.SystemPage);
  7269. if (lostId != 0)
  7270. {
  7271. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost == NO)
  7272. {
  7273. EmcOccurThenStopCharging();
  7274. sleep(2);
  7275. if (lostId == _SYSTEM_TASK_LOST_ITEM_MAIN) PRINTF_FUNC ( "System task lost (CSU). \n" );
  7276. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVCOMM)
  7277. {
  7278. PRINTF_FUNC ( "System task lost (EVComm). \n" );
  7279. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun ++)
  7280. {
  7281. chargingInfo [gun]->ConnectorPlugIn = NO;
  7282. }
  7283. }
  7284. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PSUCOMM) PRINTF_FUNC ( "System task lost (PSU Task). \n" );
  7285. else if (lostId == _SYSTEM_TASK_LOST_ITEM_SMARTBOX) PRINTF_FUNC ( "System task lost (SmartBox Task). \n" );
  7286. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVENTLOG) PRINTF_FUNC ( "System task lost (Event log). \n" );
  7287. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PRIMARYCOMM) PRINTF_FUNC ( "System task lost (Primary). \n" );
  7288. else if (lostId == _SYSTEM_TASK_LOST_ITEM_LCMCONTROL) PRINTF_FUNC ( "System task lost (LCM Comm). \n" );
  7289. else if (lostId == _SYSTEM_TASK_LOST_ITEM_INTERCOMM) PRINTF_FUNC("System task lost (Internal Comm). \n");
  7290. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = YES;
  7291. }
  7292. }
  7293. else
  7294. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = NO;
  7295. }
  7296. void InitialDHCP()
  7297. {
  7298. char tmpbuf[256];
  7299. memset(tmpbuf,0,256);
  7300. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  7301. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  7302. system(tmpbuf);
  7303. }
  7304. void Run4gResetProc()
  7305. {
  7306. if (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == _SYS_4G_MODE_ENABLE ||
  7307. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_STATION)
  7308. {
  7309. if (!ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  7310. !ShmSysConfigAndInfo->SysInfo.InternetConn)
  7311. {
  7312. if (reset4gStep == RESET_4G_STEP_NONE)
  7313. {
  7314. if ((ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES && _4gResetChkFlag >= 12) ||
  7315. _4gResetChkFlag >= 60)
  7316. {
  7317. PRINTF_FUNC("Reset 4G/Wifi module. \n");
  7318. reset4gStep = RESET_4G_STEP_DELETE_TASK;
  7319. }
  7320. else
  7321. _4gResetChkFlag++;
  7322. }
  7323. }
  7324. else
  7325. _4gResetChkFlag = 0;
  7326. switch(reset4gStep)
  7327. {
  7328. case RESET_4G_STEP_DELETE_TASK:
  7329. {
  7330. PRINTF_FUNC("Killall 4G / Wifi task. \n");
  7331. system("killall Module_4g &");
  7332. system("killall Module_Wifi &");
  7333. reset4gStep = RESET_4G_STEP_GPIO_HIGH;
  7334. }
  7335. break;
  7336. case RESET_4G_STEP_GPIO_HIGH:
  7337. {
  7338. PRINTF_FUNC("Disable 4G/Wifi. GPIO-104 -> High. \n");
  7339. system("echo 1 > /sys/class/gpio/gpio104/value");
  7340. reset4gStep = RESET_4G_STEP_GPIO_LOW_COMP;
  7341. }
  7342. break;
  7343. case RESET_4G_STEP_GPIO_LOW_COMP:
  7344. {
  7345. PRINTF_FUNC("Enable 4G/Wifi. GPIO-104 -> Low. \n");
  7346. system("echo 0 > /sys/class/gpio/gpio104/value");
  7347. reset4gStep = RESET_4G_STEP_NONE;
  7348. _4gResetChkFlag = 0;
  7349. }
  7350. break;
  7351. }
  7352. }
  7353. else
  7354. {
  7355. if (reset4gStep != RESET_4G_STEP_NONE)
  7356. system("echo 0 > /sys/class/gpio/gpio104/value");
  7357. reset4gStep = RESET_4G_STEP_NONE;
  7358. _4gResetChkFlag = 0;
  7359. }
  7360. }
  7361. //==========================================
  7362. // Check Smart Charging Profile
  7363. //==========================================
  7364. int GetStartScheduleTime(unsigned char *time)
  7365. {
  7366. int result = -1;
  7367. struct tm tmScheduleStart;
  7368. struct timeb tbScheduleStart;
  7369. 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))
  7370. {
  7371. tmScheduleStart.tm_year -= 1900;
  7372. tmScheduleStart.tm_mon -= 1;
  7373. tbScheduleStart.time = mktime(&tmScheduleStart);
  7374. tbScheduleStart.millitm = 0;
  7375. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  7376. }
  7377. return result;
  7378. }
  7379. //==========================================
  7380. // sub function
  7381. //==========================================
  7382. void CheckReturnToChargingConn()
  7383. {
  7384. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  7385. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  7386. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  7387. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  7388. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  7389. {
  7390. bool isReturnTimeout = false;
  7391. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  7392. {
  7393. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  7394. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7395. {
  7396. if ((chargingInfo[count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= SYS_MODE_COMPLETE) ||
  7397. (chargingInfo[count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7398. {
  7399. isReturnTimeout = false;
  7400. break;
  7401. }
  7402. }
  7403. else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7404. {
  7405. if ((chargingInfo[count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= SYS_MODE_COMPLETE) ||
  7406. (chargingInfo[count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7407. {
  7408. isReturnTimeout = true;
  7409. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  7410. }
  7411. }
  7412. }
  7413. // AC 槍
  7414. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  7415. {
  7416. // 沒有選中 AC,且 AC 在充電中
  7417. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  7418. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_COMPLETE))
  7419. {
  7420. // 當前 DC 充電槍在 idle 狀態
  7421. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE ||
  7422. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  7423. {
  7424. isReturnTimeout = true;
  7425. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  7426. }
  7427. }
  7428. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  7429. ((chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_COMPLETE) ||
  7430. (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)))
  7431. {
  7432. // 當前 DC 充電槍在 idle 狀態
  7433. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE ||
  7434. ac_chargingInfo[0]->SystemStatus == SYS_MODE_RESERVATION)
  7435. {
  7436. isReturnTimeout = true;
  7437. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  7438. }
  7439. }
  7440. }
  7441. if (!isReturnTimeout)
  7442. StopSystemTimeoutDet();
  7443. }
  7444. }
  7445. bool GetStartChargingByAlterMode(byte _gun)
  7446. {
  7447. bool result = true;
  7448. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2 &&
  7449. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  7450. {
  7451. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select++)
  7452. {
  7453. if (_select != _gun)
  7454. {
  7455. if (chargingInfo[_select]->SystemStatus != SYS_MODE_IDLE &&
  7456. chargingInfo[_select]->SystemStatus != SYS_MODE_RESERVATION)
  7457. {
  7458. result = false;
  7459. break;
  7460. }
  7461. }
  7462. }
  7463. }
  7464. return result;
  7465. }
  7466. void TheEndCharging(byte gun_index)
  7467. {
  7468. chargingInfo[gun_index]->isRemoteStart = NO;
  7469. StopGunInfoTimeoutDet(gun_index);
  7470. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  7471. ChangeStartOrStopDateTime(NO, gun_index);
  7472. DB_Insert_Record(localDb, gun_index);
  7473. }
  7474. void AdjustChargerCurrent()
  7475. {
  7476. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  7477. // 設定的電流~ 如超過可輸出的電流,則 bypass
  7478. if (ShmSysConfigAndInfo->SysConfig.RatingCurrent < ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent ||
  7479. ShmSysConfigAndInfo->SysConfig.RatingCurrent == 0)
  7480. {
  7481. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent = 0;
  7482. }
  7483. PRINTF_FUNC ("System : MaxChargingPower = %d (KW), MaxChargingCurrent = %d (A)",
  7484. ShmPsuData->SystemAvailablePower / 10,
  7485. ShmPsuData->SystemAvailableCurrent / 10
  7486. );
  7487. PRINTF_FUNC ("Web Config : ChargingPower = %d (KW), ChargingCurrent = %d (A)",
  7488. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  7489. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent
  7490. );
  7491. }
  7492. bool CheckTheSystemType()
  7493. {
  7494. bool result = false;
  7495. char EvsePower[2];
  7496. EvsePower[2] = '\0';
  7497. strncpy ( EvsePower, (char *) (ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2 );
  7498. ShmDcCommonData->halfGroupCount = 1;
  7499. if (strcmp ( EvsePower, "30" ) == EQUAL)
  7500. ShmDcCommonData->systemType = _SYSTEM_TYPE_SIMPLE;
  7501. else
  7502. {
  7503. // 60KW 以上的系統
  7504. if (ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity == 0 &&
  7505. ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity == 0)
  7506. ShmDcCommonData->systemType = _SYSTEM_TYPE_STANDARD;
  7507. else
  7508. {
  7509. ShmDcCommonData->halfGroupCount = 2;
  7510. ShmDcCommonData->systemType = _SYSTEM_TYPE_SMART;
  7511. }
  7512. if ((ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity != 0 && ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity == 0) ||
  7513. (ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity == 0 && ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity != 0))
  7514. {
  7515. result = true;
  7516. }
  7517. }
  7518. return result;
  7519. }
  7520. void CheckStatusOfMeter()
  7521. {
  7522. // 橋接如果搭上~ 則兩個電表都是使用狀態
  7523. // 如橋接沒有搭上~ 則視該火線 relay 狀態為電表使用狀態
  7524. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  7525. {
  7526. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++)
  7527. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  7528. }
  7529. else
  7530. {
  7531. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++)
  7532. {
  7533. if (chargingInfo[mIndex]->RelayKPK2Status == YES || chargingInfo[mIndex]->RelayK1K2Status)
  7534. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  7535. else
  7536. ShmCsuMeterData->_meter[mIndex].isCalculation = NO;
  7537. }
  7538. }
  7539. }
  7540. void ReviewCriticalAlarm()
  7541. {
  7542. if (ShmDcCommonData->GunRelayDrivingOccur[0] == YES ||
  7543. ShmDcCommonData->GunRelayDrivingOccur[1] == YES ||
  7544. ShmDcCommonData->GunRelayWeldingOccur[0] == YES ||
  7545. ShmDcCommonData->GunRelayWeldingOccur[1] == YES ||
  7546. ShmDcCommonData->ParaRelayWeldingOccur == YES ||
  7547. ShmDcCommonData->ParaRelayDrivingOccur == YES ||
  7548. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip ||
  7549. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip ||
  7550. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen ||
  7551. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  7552. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm ||
  7553. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU ||
  7554. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost)
  7555. {
  7556. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_CRITICAL;
  7557. }
  7558. else
  7559. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_NORMAL;
  7560. }
  7561. void CheckRelayWeldingOrDrivingFault(byte gun_index)
  7562. {
  7563. // relay welding fault then stop the charging process.
  7564. byte faultCode = RELAY_STATUS_ERROR_NONE;
  7565. if (gun_index == 0)
  7566. {
  7567. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7568. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7569. {
  7570. faultCode = RELAY_STATUS_ERROR_WELDING;
  7571. }
  7572. else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7573. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7574. {
  7575. faultCode = RELAY_STATUS_ERROR_DRIVING;
  7576. }
  7577. }
  7578. else if (gun_index == 1)
  7579. {
  7580. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7581. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7582. {
  7583. faultCode = RELAY_STATUS_ERROR_WELDING;
  7584. }
  7585. else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7586. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7587. {
  7588. faultCode = RELAY_STATUS_ERROR_DRIVING;
  7589. }
  7590. }
  7591. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2 &&
  7592. !ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  7593. {
  7594. // 橋接
  7595. if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7596. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7597. ShmDcCommonData->RelayCheckStatus[RELAY_PRE_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7598. {
  7599. faultCode = RELAY_STATUS_ERROR_PARA_WELDING;
  7600. }
  7601. else if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7602. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7603. ShmDcCommonData->RelayCheckStatus[RELAY_PRE_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7604. {
  7605. faultCode = RELAY_STATUS_ERROR_PARA_DRIVING;
  7606. }
  7607. }
  7608. if (faultCode == RELAY_STATUS_ERROR_WELDING)
  7609. {
  7610. // welding
  7611. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  7612. RecordAlarmCode(gun_index, "011011");
  7613. else if (chargingInfo[gun_index]->Type == _Type_GB)
  7614. RecordAlarmCode(gun_index, "011015");
  7615. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  7616. RecordAlarmCode(gun_index, "011013");
  7617. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = YES;
  7618. EmcOccurThenStopCharging();
  7619. }
  7620. else if (faultCode == RELAY_STATUS_ERROR_DRIVING)
  7621. {
  7622. // driving
  7623. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  7624. RecordAlarmCode(gun_index, "011012");
  7625. else if (chargingInfo[gun_index]->Type == _Type_GB)
  7626. RecordAlarmCode(gun_index, "011016");
  7627. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  7628. RecordAlarmCode(gun_index, "011014");
  7629. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = YES;
  7630. EmcOccurThenStopCharging();
  7631. }
  7632. else
  7633. {
  7634. if (chargingInfo[gun_index]->SystemStatus <= SYS_MODE_TERMINATING ||
  7635. chargingInfo[gun_index]->SystemStatus >= SYS_MODE_ALARM)
  7636. {
  7637. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = NO;
  7638. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = NO;
  7639. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  7640. {
  7641. ResetChargerAlarmCode(gun_index, "011012");
  7642. ResetChargerAlarmCode(gun_index, "011011");
  7643. }
  7644. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  7645. {
  7646. ResetChargerAlarmCode(gun_index, "011014");
  7647. ResetChargerAlarmCode(gun_index, "011013");
  7648. }
  7649. else if (chargingInfo[gun_index]->Type == _Type_GB)
  7650. {
  7651. ResetChargerAlarmCode(gun_index, "011016");
  7652. ResetChargerAlarmCode(gun_index, "011015");
  7653. }
  7654. }
  7655. }
  7656. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2)
  7657. {
  7658. if ((chargingInfo[0]->SystemStatus <= SYS_MODE_TERMINATING ||
  7659. chargingInfo[0]->SystemStatus >= SYS_MODE_ALARM) &&
  7660. (chargingInfo[1]->SystemStatus <= SYS_MODE_TERMINATING ||
  7661. chargingInfo[1]->SystemStatus >= SYS_MODE_ALARM))
  7662. {
  7663. if (faultCode == RELAY_STATUS_ERROR_PARA_WELDING)
  7664. {
  7665. // parallel welding
  7666. RecordAlarmCode ( gun_index, "011039" );
  7667. ShmDcCommonData->ParaRelayWeldingOccur = YES;
  7668. EmcOccurThenStopCharging ();
  7669. }
  7670. else if (faultCode == RELAY_STATUS_ERROR_PARA_DRIVING)
  7671. {
  7672. // parallel driving
  7673. RecordAlarmCode ( gun_index, "011040" );
  7674. ShmDcCommonData->ParaRelayDrivingOccur = YES;
  7675. EmcOccurThenStopCharging ();
  7676. }
  7677. else
  7678. {
  7679. ShmDcCommonData->ParaRelayWeldingOccur = NO;
  7680. ShmDcCommonData->ParaRelayDrivingOccur = NO;
  7681. ResetChargerAlarmCode ( gun_index, "011039" );
  7682. ResetChargerAlarmCode ( gun_index, "011040" );
  7683. }
  7684. }
  7685. }
  7686. }
  7687. bool PrecheckIsPass(byte gun_index)
  7688. {
  7689. bool result = true;
  7690. // relay welding or driving 是反向
  7691. if (result)
  7692. result = !ShmDcCommonData->GunRelayWeldingOccur[gun_index];
  7693. if (result)
  7694. result = !ShmDcCommonData->ParaRelayWeldingOccur;
  7695. return result;
  7696. }
  7697. void CollectError(byte gun_index)
  7698. {
  7699. errCollect.GunErrMessage |= ShmDcCommonData->ConnectErrList[gun_index].GunErrMessage;
  7700. }
  7701. void AlarmCheck(byte type)
  7702. {
  7703. // type : Cha = 0、CCS = 1、GBT = 2
  7704. if (type == _Type_Chademo)
  7705. {
  7706. // GFD Trip
  7707. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  7708. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  7709. else
  7710. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  7711. // UVP
  7712. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  7713. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  7714. else
  7715. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  7716. // OTP
  7717. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  7718. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = YES;
  7719. else
  7720. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = NO;
  7721. // OVP
  7722. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  7723. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = YES;
  7724. else
  7725. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = NO;
  7726. // GFD Warning
  7727. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  7728. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  7729. else
  7730. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  7731. // Relay Welding
  7732. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7733. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  7734. else
  7735. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = NO;
  7736. // Relay Driving
  7737. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7738. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  7739. else
  7740. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  7741. // Connect temp Sensor broken
  7742. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7743. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = YES;
  7744. else
  7745. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = NO;
  7746. // Output UCP
  7747. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 0))
  7748. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = YES;
  7749. else
  7750. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = NO;
  7751. // Parallel Relay Welding
  7752. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 3))
  7753. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  7754. else
  7755. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  7756. // Parallel Relay Driving
  7757. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 4))
  7758. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  7759. else
  7760. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  7761. // Stop By Evse Condition
  7762. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 5))
  7763. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = YES;
  7764. else
  7765. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = NO;
  7766. }
  7767. else if (type == _Type_CCS)
  7768. {
  7769. // GFD Trip
  7770. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  7771. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  7772. else
  7773. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  7774. // UVP
  7775. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  7776. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  7777. else
  7778. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  7779. // OTP
  7780. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  7781. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = YES;
  7782. else
  7783. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = NO;
  7784. // OVP
  7785. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  7786. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = YES;
  7787. else
  7788. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = NO;
  7789. // GFD Warning
  7790. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  7791. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  7792. else
  7793. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  7794. // Relay Welding
  7795. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7796. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  7797. else
  7798. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = NO;
  7799. // Relay Driving
  7800. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7801. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  7802. else
  7803. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  7804. // Connect temp Sensor broken
  7805. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7806. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = YES;
  7807. else
  7808. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = NO;
  7809. // Output UCP
  7810. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 1))
  7811. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = YES;
  7812. else
  7813. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = NO;
  7814. // Parallel Relay Welding
  7815. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 3))
  7816. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  7817. else
  7818. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  7819. // Parallel Relay Driving
  7820. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 4))
  7821. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  7822. else
  7823. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  7824. // Stop By Evse Condition
  7825. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 5))
  7826. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = YES;
  7827. else
  7828. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = NO;
  7829. }
  7830. else if (type == _Type_GB)
  7831. {
  7832. // GFD Trip
  7833. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  7834. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  7835. else
  7836. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  7837. // UVP
  7838. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  7839. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  7840. else
  7841. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  7842. // OTP
  7843. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  7844. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = YES;
  7845. else
  7846. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = NO;
  7847. // OVP
  7848. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  7849. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = YES;
  7850. else
  7851. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = NO;
  7852. // GFD Warning
  7853. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  7854. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  7855. else
  7856. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  7857. // Relay Welding
  7858. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7859. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  7860. else
  7861. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = NO;
  7862. // Relay Driving
  7863. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7864. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  7865. else
  7866. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  7867. // Connect temp Sensor broken
  7868. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7869. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = YES;
  7870. else
  7871. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = NO;
  7872. // Output UCP
  7873. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 2))
  7874. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = YES;
  7875. else
  7876. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = NO;
  7877. // Parallel Relay Welding
  7878. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 3))
  7879. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  7880. else
  7881. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  7882. // Parallel Relay Driving
  7883. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 4))
  7884. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  7885. else
  7886. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  7887. // Stop By Evse Condition
  7888. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 5))
  7889. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = YES;
  7890. else
  7891. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = NO;
  7892. }
  7893. }
  7894. void SystemAlarmCheck()
  7895. {
  7896. byte system = 3;
  7897. // Parallel Welding
  7898. if (DetectBitValue(errCollect.GunErrMessage >> (8 * system), 3))
  7899. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  7900. else
  7901. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  7902. // Parallel Driving
  7903. if (DetectBitValue(errCollect.GunErrMessage >> (8 * system), 4))
  7904. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  7905. else
  7906. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  7907. }
  7908. void SavePresentChargedDuration(byte gun_index)
  7909. {
  7910. int _diffTime = 0;
  7911. ftime(&endChargingTime[gun_index]);
  7912. _diffTime = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  7913. // below 0 or over 5 sec is abnormal
  7914. if (_diffTime < 0 ||
  7915. _diffTime > chargingInfo[gun_index]->PresentChargedDuration + 5)
  7916. {
  7917. _presentChargingTimeBuf[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  7918. ftime(&startChargingTime[gun_index]);
  7919. PRINTF_FUNC("TimeZone changed (%d) \n", gun_index);
  7920. }
  7921. else
  7922. chargingInfo[gun_index]->PresentChargedDuration = _presentChargingTimeBuf[gun_index] + _diffTime;
  7923. }
  7924. void ClearEnergyTimePeriod(byte gun_index)
  7925. {
  7926. for (int i = 0; i < 24; i++)
  7927. {
  7928. ShmDcCommonData->energy_time_period[gun_index][i] = 0;
  7929. }
  7930. }
  7931. void CheckOcppCostAndPrice()
  7932. {
  7933. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling &&
  7934. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  7935. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  7936. {
  7937. ocpp_parsing_DT_defaultPrice_cmd();
  7938. ocpp_parsing_DT_userPrice_cmd();
  7939. ocpp_parsing_DT_running_final_cmd();
  7940. }
  7941. }
  7942. void CheckAuthrizeByEvccid()
  7943. {
  7944. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  7945. ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID)
  7946. ShmDcCommonData->enObtainAuthorizbyCCID = YES;
  7947. else
  7948. ShmDcCommonData->enObtainAuthorizbyCCID = NO;
  7949. }
  7950. void ConnectorOTP(byte _index)
  7951. {
  7952. bool isOTP = false;
  7953. if (chargingInfo[_index]->Type == _Type_Chademo)
  7954. {
  7955. if (ShmDcCommonData->ConnectErrList[_index].GunBits.ChaConnectOTP)
  7956. isOTP = true;
  7957. }
  7958. else if (chargingInfo[_index]->Type == _Type_CCS)
  7959. {
  7960. if (ShmDcCommonData->ConnectErrList[_index].GunBits.CCSConnectOTP)
  7961. isOTP = true;
  7962. }
  7963. else if (chargingInfo[_index]->Type == _Type_GB)
  7964. {
  7965. if (ShmDcCommonData->ConnectErrList[_index].GunBits.GBTConnectOTP)
  7966. isOTP = true;
  7967. }
  7968. if (isOTP)
  7969. {
  7970. if (gunTempAllowCount[_index] >= 1)
  7971. chargingInfo[_index]->StopChargeFlag = YES;
  7972. else
  7973. gunTempAllowCount[_index] += 1;
  7974. }
  7975. }
  7976. void CheckConnectorOTP(byte _index)
  7977. {
  7978. if (chargingInfo[_index]->Type == _Type_Chademo &&
  7979. chargingInfo[_index]->ModelType == 'K')
  7980. {
  7981. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  7982. {
  7983. ResetChargerAlarmCode(_index, "011018");
  7984. if (chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7985. RecordAlarmCode(_index, "012229");
  7986. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  7987. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7988. ResetChargerAlarmCode(_index, "012229");
  7989. }
  7990. else
  7991. {
  7992. // 沒接上 Sensor or 異常
  7993. RecordAlarmCode(_index, "011018");
  7994. ResetChargerAlarmCode(_index, "012229");
  7995. }
  7996. }
  7997. else if (chargingInfo[_index]->Type == _Type_CCS)
  7998. {
  7999. // CCS 不管甚麼輸出都會有槍溫偵測!!~
  8000. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  8001. {
  8002. ResetChargerAlarmCode(_index, "011019");
  8003. if(chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  8004. RecordAlarmCode(_index, "012230");
  8005. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  8006. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  8007. ResetChargerAlarmCode(_index, "012230");
  8008. }
  8009. else
  8010. {
  8011. // 沒接上 Sensor or 異常
  8012. RecordAlarmCode(_index, "011019");
  8013. ResetChargerAlarmCode(_index, "012230");
  8014. }
  8015. }
  8016. else if (chargingInfo[_index]->Type == _Type_GB &&
  8017. chargingInfo[_index]->ModelType == 'B')
  8018. {
  8019. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  8020. {
  8021. ResetChargerAlarmCode(_index, "011020");
  8022. if(chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  8023. RecordAlarmCode(_index, "012231");
  8024. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  8025. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  8026. ResetChargerAlarmCode(_index, "012231");
  8027. }
  8028. else
  8029. {
  8030. // 沒接上 Sensor or 異常
  8031. RecordAlarmCode(_index, "011020");
  8032. ResetChargerAlarmCode(_index, "012231");
  8033. }
  8034. }
  8035. ConnectorOTP(_index);
  8036. }
  8037. void ChkConnectorBoardStop(byte index)
  8038. {
  8039. if (isEvBoardStopChargeFlag(index) == YES)
  8040. {
  8041. // 板端要求停止 (錯誤)
  8042. ocpp_set_stopReason_by_cmd(index, "EVDisconnected");
  8043. ChargingAlarmProcess(index);
  8044. }
  8045. else if (isEvBoardNormalStopChargeFlag(index) == YES)
  8046. {
  8047. // 板端要求停止 (正常)
  8048. ocpp_set_stopReason_by_cmd(index, "EVDisconnected");
  8049. ChargingTerminalProcess(index);
  8050. }
  8051. }
  8052. void ChkNoCharging(byte index)
  8053. {
  8054. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  8055. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  8056. {
  8057. PRINTF_FUNC("Offline Policy : No Charing... \n");
  8058. ocpp_set_stopReason_by_cmd(index, "Other");
  8059. ChargingTerminalProcess(index);
  8060. }
  8061. }
  8062. void ChkPsuErrorStop(byte index)
  8063. {
  8064. if (ShmPsuData->PsuGroup[index].GroupErrorFlag.bits.PsuFailure == YES ||
  8065. (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf && ShmPsuData->PsuGroup[0].GroupErrorFlag.bits.PsuFailure))
  8066. {
  8067. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8068. {
  8069. if (chargingInfo[gun_index]->PresentChargingCurrent <= 5 &&
  8070. ((chargingInfo[gun_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  8071. (chargingInfo[gun_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[gun_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)))
  8072. {
  8073. ocpp_set_stopReason_by_cmd(gun_index, "Other");
  8074. ChargingAlarmProcess(gun_index);
  8075. }
  8076. }
  8077. }
  8078. }
  8079. void ChkBackendStop(byte index)
  8080. {
  8081. if (ocpp_chk_invalid_id_cmd(index))
  8082. {
  8083. // Start Transaction Stop
  8084. ocpp_set_stopReason_by_cmd(index, "DeAuthorized");
  8085. ChargingTerminalProcess(index);
  8086. }
  8087. else if (ocpp_chk_remoteStop_cmd(index) == YES ||
  8088. WifiScheduleStop(index))
  8089. {
  8090. // 後臺要求停止
  8091. ChargingTerminalProcess(index);
  8092. }
  8093. }
  8094. void ChkConfigurationStop(byte index)
  8095. {
  8096. if (CheckWebConfigChargingTimeout(index) ||
  8097. CheckWebConfigChargingEnergy(index) ||
  8098. CheckWebConfigChargingSOC(index))
  8099. {
  8100. // 網頁設定
  8101. PRINTF_FUNC("CheckBackendCharging stop charging (%d) \n", index);
  8102. RecordAlarmCode(index, "023984");
  8103. ChargingTerminalProcess(index);
  8104. return;
  8105. }
  8106. if (CheckOcppChargingTimeout(index) ||
  8107. CheckOcppChargingEnergy(index) ||
  8108. CheckOcppChargingSOC(index))
  8109. {
  8110. // OCPP 設定
  8111. PRINTF_FUNC("CheckOcppCharging stop charging (%d) \n", index);
  8112. RecordAlarmCode(index, "023984");
  8113. ChargingTerminalProcess(index);
  8114. }
  8115. }
  8116. void RecordGfdResult(byte index)
  8117. {
  8118. if (chargingInfo[index]->Type == _Type_Chademo)
  8119. {
  8120. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  8121. {
  8122. // GFD 錯誤停止
  8123. RecordAlarmCode(index, "012234");
  8124. ocpp_set_stopReason_by_cmd(index, "Other");
  8125. ChargingAlarmProcess(index);
  8126. }
  8127. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  8128. {
  8129. // GFD 警告
  8130. RecordAlarmCode(index, "012296");
  8131. }
  8132. }
  8133. else if (chargingInfo[index]->Type == _Type_GB)
  8134. {
  8135. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  8136. {
  8137. // GFD 錯誤停止
  8138. RecordAlarmCode(index, "012236");
  8139. ocpp_set_stopReason_by_cmd ( index, "Other" );
  8140. ChargingAlarmProcess ( index );
  8141. }
  8142. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  8143. {
  8144. // GFD 警告
  8145. RecordAlarmCode(index, "012298");
  8146. }
  8147. }
  8148. else if (chargingInfo[index]->Type == _Type_CCS)
  8149. {
  8150. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  8151. {
  8152. // GFD 錯誤停止
  8153. RecordAlarmCode(index, "012235");
  8154. ocpp_set_stopReason_by_cmd(index, "Other");
  8155. ChargingAlarmProcess ( index );
  8156. }
  8157. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  8158. {
  8159. // GFD 警告
  8160. RecordAlarmCode(index, "012297");
  8161. }
  8162. }
  8163. }
  8164. void CcidAuthorizationProcessing(byte gun_index)
  8165. {
  8166. if (chargingInfo [gun_index]->Type == _Type_CCS &&
  8167. chargingInfo [gun_index]->IsAvailable &&
  8168. ShmDcCommonData->enObtainAuthorizbyCCID &&
  8169. chargingInfo[gun_index]->ConnectorPlugIn &&
  8170. strcmp ((char *)ShmSysConfigAndInfo->SysConfig.UserId, "") == EQUAL)
  8171. {
  8172. AddPlugInTimes(gun_index);
  8173. PRINTF_FUNC("CcidAuthorizationProcessing. \n");
  8174. ChangeGunSelectByIndex(gun_index);
  8175. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8176. }
  8177. }
  8178. void CheckCcidAuthorizationFunction(byte gun_index)
  8179. {
  8180. if (chargingInfo [gun_index]->Type == _Type_CCS &&
  8181. ShmDcCommonData->enObtainAuthorizbyCCID)
  8182. {
  8183. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_GET &&
  8184. ShmDcCommonData->_CcidAuthProcessStep == _CCID_NONE &&
  8185. strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" ) == EQUAL)
  8186. {
  8187. PRINTF_FUNC ( "index = %d, Get for CCID (main). \n", gun_index );
  8188. ShmDcCommonData->_authWithCcidFlag [gun_index] = _CCID_CHECK;
  8189. ShmDcCommonData->_CcidAuthProcessStep = _CCID_CHECK;
  8190. //LWADD_EVCCID
  8191. if(strcmp((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[EVCCID_PREFIX].ItemData, "") != EQUAL)
  8192. {
  8193. strcpy((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[EVCCID_PREFIX].ItemData);
  8194. strcat((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)chargingInfo [gun_index]->EVCCID);
  8195. PRINTF_FUNC ( "Prfix CCID:%s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  8196. }
  8197. else
  8198. strcpy((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)chargingInfo[gun_index]->EVCCID);
  8199. }
  8200. else if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  8201. {
  8202. if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHFAIL)
  8203. {
  8204. PRINTF_FUNC ( "index = %d, _CCID_AUTHFAIL. \n", gun_index );
  8205. ShmDcCommonData->_authWithCcidFlag [gun_index] = ShmDcCommonData->_CcidAuthProcessStep;
  8206. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  8207. }
  8208. else if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHCOMP)
  8209. {
  8210. PRINTF_FUNC ( "index = %d, _CCID_AUTHCOMP. \n", gun_index );
  8211. ShmDcCommonData->_authWithCcidFlag [gun_index] = ShmDcCommonData->_CcidAuthProcessStep;
  8212. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  8213. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8214. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  8215. }
  8216. }
  8217. }
  8218. }
  8219. bool isCcidEnterToCharging(byte gun_index)
  8220. {
  8221. bool result = false;
  8222. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  8223. {
  8224. if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHCOMP)
  8225. {
  8226. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8227. {
  8228. if (chargingInfo [_index]->Type == _Type_CCS &&
  8229. chargingInfo [_index]->IsAvailable &&
  8230. ShmDcCommonData->_authWithCcidFlag [_index] == _CCID_CHECK)
  8231. {
  8232. if (_index != gun_index)
  8233. result = true;
  8234. break;
  8235. }
  8236. }
  8237. if (!result)
  8238. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  8239. }
  8240. }
  8241. return result;
  8242. }
  8243. bool IsNoneSelectAcModule()
  8244. {
  8245. bool result = false;
  8246. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0 ||
  8247. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  8248. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE))
  8249. {
  8250. result = true;
  8251. }
  8252. return result;
  8253. }
  8254. bool IsSelectAcModule()
  8255. {
  8256. bool result = false;
  8257. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  8258. return result;
  8259. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  8260. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  8261. {
  8262. result = true;
  8263. }
  8264. return result;
  8265. }
  8266. void DisplayPageChkByAcModule()
  8267. {
  8268. if (IsSelectAcModule())
  8269. {
  8270. if (ac_chargingInfo [0]->ConnectorPlugIn == AC_SYS_A)
  8271. {
  8272. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8273. }
  8274. else
  8275. {
  8276. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8277. }
  8278. }
  8279. }
  8280. void UpdateDcMeterPowerConsumption(byte _index)
  8281. {
  8282. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'P')
  8283. {
  8284. if (chargingInfo[_index]->PowerConsumption != ShmSysConfigAndInfo->SysInfo.DcMeterInfo[_index].totlizeImportEnergy)
  8285. chargingInfo[_index]->PowerConsumption = ShmSysConfigAndInfo->SysInfo.DcMeterInfo[_index].totlizeImportEnergy;
  8286. }
  8287. else if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
  8288. ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L')
  8289. {
  8290. if(chargingInfo[_index]->PowerConsumption != ShmCsuMeterData->_meter[_index]._curTotalCharging / 100)
  8291. chargingInfo[_index]->PowerConsumption = ShmCsuMeterData->_meter[_index]._curTotalCharging / 100;
  8292. }
  8293. }
  8294. int PresentChargedAcEnergyClear(unsigned char gun_index)
  8295. {
  8296. int result = FAIL;
  8297. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  8298. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod)*sizeof(float));
  8299. result = PASS;
  8300. return result;
  8301. }
  8302. void CheckTcpTimeout()
  8303. {
  8304. int _tpcTimebuf = 0;
  8305. _tpcTimebuf = GetTimeoutValue(&_tcpDetectStatus_time);
  8306. if (_tpcTimebuf < 0)
  8307. GetTimespecFunc(&_tcpDetectStatus_time);
  8308. else
  8309. {
  8310. if (_tpcTimebuf >= TPC_PLUG_DETECT_TIMEOUT)
  8311. {
  8312. _DetectTpcPlugInTimeout();
  8313. }
  8314. }
  8315. }
  8316. int main(void)
  8317. {
  8318. if(CreateShareMemory() == 0)
  8319. {
  8320. #ifdef SystemLogMessage
  8321. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  8322. #endif
  8323. if(ShmStatusCodeData!=NULL)
  8324. {
  8325. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  8326. }
  8327. sleep(5);
  8328. system("reboot -f");
  8329. sleep(5);
  8330. system("reboot -f");
  8331. }
  8332. PRINTF_FUNC("===== Boot and Initialization start ===== \n");
  8333. if (!InitialSystemDefaultConfig())
  8334. {
  8335. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  8336. StopProcessingLoop();
  8337. }
  8338. PRINTF_FUNC("CheckConnectorTypeStatus and CheckIsAlternatvie \n");
  8339. CheckGunTypeFromHw();
  8340. CheckIsAlternatvieByModelName();
  8341. InitialShareMemoryInfo();
  8342. PRINTF_FUNC("InitialShareMemoryInfo OK. \n");
  8343. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  8344. PRINTF_FUNC("ChargerType (IEC or UL) = IEC \n");
  8345. else
  8346. PRINTF_FUNC("ChargerType (IEC or UL) = UL \n");
  8347. ChangeLcmByIndex(_LCM_INIT);
  8348. if (!CheckConnectorTypeStatus())
  8349. isModelNameMatch = false;
  8350. Initialization();
  8351. PRINTF_FUNC("===== Initial Complete ===== \n");
  8352. PRINTF_FUNC("===== Spawn all Task and self test start ===== \n");
  8353. SpawnTask();
  8354. if (!isModelNameMatch)
  8355. {
  8356. // Module Name 與硬體對應不正確
  8357. PRINTF_FUNC("*** Module Name & HW info none match : Please check if the Model name and HW are matched. ***\n");
  8358. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  8359. ChangeLcmByIndex(_LCM_FIX);
  8360. sleep(3);
  8361. KillTask();
  8362. StopProcessingLoop();
  8363. }
  8364. PRINTF_FUNC("Module Name & HW info correct. \n");
  8365. CreateTimeoutFork();
  8366. if (!DEBUG_ALSTON)
  8367. {
  8368. PRINTF_FUNC("Self test run..\n");
  8369. SelfTestRun();
  8370. }
  8371. else
  8372. {
  8373. sleep(3);
  8374. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  8375. }
  8376. StopSystemTimeoutDet();
  8377. if (!DEBUG_ALSTON &&
  8378. (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  8379. ShmPsuData->Work_Step == _NO_WORKING ||
  8380. ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES ||
  8381. strcmp((char *)ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ") == EQUAL))
  8382. {
  8383. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  8384. {
  8385. if (!DisplaySelfTestFailReason() &&
  8386. ShmDcCommonData->rebootCount < 1)
  8387. {
  8388. PRINTF_FUNC("=======Soft reboot for retry self-test======= \n");
  8389. SetupRebootCount(1);
  8390. sleep(3);
  8391. KillAllTask();
  8392. system("/usr/bin/run_evse_restart.sh");
  8393. }
  8394. else if (ShmDcCommonData->rebootCount < 1)
  8395. {
  8396. PRINTF_FUNC("=======Soft reboot for retry self-test (Normal)======= \n");
  8397. SetupRebootCount(1);
  8398. sleep(3);
  8399. KillAllTask();
  8400. system("/usr/bin/run_evse_restart.sh");
  8401. }
  8402. else
  8403. SetupRebootCount(0);
  8404. }
  8405. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8406. {
  8407. ChargingAlarmProcess(gun_index);
  8408. }
  8409. //ChangeLcmByIndex(_LCM_FIX);
  8410. sleep(3);
  8411. //if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  8412. //{
  8413. KillTaskExceptDetectTask();
  8414. //}
  8415. //else
  8416. // KillTask();
  8417. StopProcessingLoop();
  8418. }
  8419. else
  8420. {
  8421. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8422. {
  8423. SetChargerMode(gun_index, SYS_MODE_IDLE);
  8424. }
  8425. SetupRebootCount(0);
  8426. }
  8427. PRINTF_FUNC("===== Self Test Complete ===== \n");
  8428. PRINTF_FUNC("===== Finally Create DB ===== \n");
  8429. // Local DB
  8430. if(DB_Open(localDb) != PASS)
  8431. {
  8432. PRINTF_FUNC("DB_Open fail. \n");
  8433. isDb_ready = false;
  8434. }
  8435. else
  8436. {
  8437. isDb_ready = true;
  8438. for (int _acIndex = 0; _acIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _acIndex++)
  8439. {
  8440. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  8441. ac_chargingInfo[0]->IsAvailable = DB_Get_Operactive(localDb, _acIndex + hasDc);
  8442. }
  8443. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  8444. {
  8445. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  8446. chargingInfo[_index]->PowerConsumption = DB_Get_PowerConsumption(localDb, _index);
  8447. }
  8448. DB_Reboot_Record(localDb);
  8449. }
  8450. if (NetworkDB_Open(networkDb) != PASS)
  8451. {
  8452. PRINTF_FUNC("NetworkDB_Open fail. \n");
  8453. }
  8454. ChangeLcmByIndex(_LCM_IDLE);
  8455. sleep(1);
  8456. CreateRfidFork();
  8457. sleep(1);
  8458. if (!DEBUG_ALSTON)
  8459. {
  8460. PRINTF_FUNC("===== Create chk system Task ===== \n");
  8461. CreateCheckSystemTaskFork();
  8462. }
  8463. PRINTF_FUNC("===== Create DB End ===== \n");
  8464. if (CheckTheSystemType())
  8465. {
  8466. PRINTF_FUNC("Can't confirm the system type. (reboot) \n");
  8467. sleep(3);
  8468. KillAllTask();
  8469. system("/usr/bin/run_evse_restart.sh");
  8470. }
  8471. PRINTF_FUNC("===== Charger info ===== ");
  8472. PRINTF_FUNC("SW Version = %s", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  8473. PRINTF_FUNC("Bootloader = %s", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  8474. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  8475. InfoSystemType();
  8476. InfoLcmVersion();
  8477. InfoFwSlotStatusLog();
  8478. AdjustChargerCurrent();
  8479. PRINTF_FUNC("===== Charger info End ===== ");
  8480. GetTimespecFunc(&_cmdMainPriority_time);
  8481. _ocppProfileChkFlag_1 = 0;
  8482. _ocppProfileChkFlag_2 = 0;
  8483. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") == EQUAL)
  8484. {
  8485. DEBUG_ERROR_MSG("URL is empty kill Module_OcppBackend...\n");
  8486. system ("pkill OcppBackend");
  8487. }
  8488. int _mainTimebuf = 0;
  8489. for (;;)
  8490. {
  8491. if (ShmDcCommonData->_isAutoRunTest != _isTestRun)
  8492. {
  8493. if (ShmDcCommonData->_isAutoRunTest == YES)
  8494. {
  8495. GetTimespecFunc(&_autoRuntestTime);
  8496. }
  8497. _isTestRun = ShmDcCommonData->_isAutoRunTest;
  8498. }
  8499. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8500. {
  8501. ocpp_ac_chk_availability_cmd();
  8502. ocpp_ac_chk_unlock_cmd();
  8503. }
  8504. ocpp_auto_response_BootNotification();
  8505. ocpp_chk_reset_cmd(NO);
  8506. ButtonTriggerEvent();
  8507. if (!DEBUG_ALSTON)
  8508. ChkPrimaryStatus();
  8509. if ((IsConnectorWholeIdle() ||
  8510. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX))
  8511. {
  8512. CheckFactoryConfigFunction();
  8513. CheckFwUpdateFunction();
  8514. }
  8515. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  8516. {
  8517. sleep(1);
  8518. continue;
  8519. }
  8520. // OCPP 邏輯
  8521. ocpp_chk_remoteStart_cmd();
  8522. // 讀卡邏輯
  8523. ScannerCardProcess();
  8524. // 當 AC 沒有搭上時,清除一些錯誤碼
  8525. ClearAlarmCodeWhenAcOff();
  8526. // 確認是否要回到充電中的槍畫面邏輯判斷
  8527. CheckReturnToChargingConn();
  8528. if ((_acgunIndex > 0 && isDetectPlugin() && !isCardScan))
  8529. {
  8530. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8531. }
  8532. _mainTimebuf = GetTimeoutValue(&_cmdMainPriority_time);
  8533. if (ShmDcCommonData->_tpcPlugDetectState == _TPC_PLUG_DETECT_STATUS_WAIT)
  8534. CheckTcpTimeout();
  8535. if (_mainTimebuf < 0)
  8536. GetTimespecFunc(&_cmdMainPriority_time);
  8537. if (_mainTimebuf >= 5)
  8538. {
  8539. if (ShmPsuData->PsuStopChargeFlag)
  8540. Check267Alarm();
  8541. CheckTask();
  8542. Run4gResetProc();
  8543. bool isBothCharging = true;
  8544. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8545. {
  8546. UpdateDcMeterPowerConsumption(_index);
  8547. if ((chargingInfo[_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  8548. (chargingInfo[_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  8549. {
  8550. ocpp_chargingProfile_process(_index);
  8551. if (chargingInfo[_index]->SystemStatus == SYS_MODE_CHARGING &&
  8552. ((_index == 0 && _ocppProfileChkFlag_1 >= 12) ||
  8553. (_index == 1 && _ocppProfileChkFlag_2 >= 12)))
  8554. {
  8555. ocpp_lift_profileReq_cmd(_index);
  8556. if (_index == 0)
  8557. _ocppProfileChkFlag_1 = 0;
  8558. else if (_index == 1)
  8559. _ocppProfileChkFlag_2 = 0;
  8560. }
  8561. else if (chargingInfo[_index]->SystemStatus != SYS_MODE_CHARGING)
  8562. {
  8563. ocpp_lift_profileReq_cmd(_index);
  8564. if (_index == 0)
  8565. _ocppProfileChkFlag_1 = 0;
  8566. else if (_index == 1)
  8567. _ocppProfileChkFlag_2 = 0;
  8568. }
  8569. else
  8570. {
  8571. if (_index == 0)
  8572. _ocppProfileChkFlag_1++;
  8573. else if (_index == 1)
  8574. _ocppProfileChkFlag_2++;
  8575. }
  8576. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  8577. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  8578. {
  8579. DB_Update_PowerConsumption(localDb, _index, chargingInfo[_index]->PowerConsumption);
  8580. }
  8581. }
  8582. if (chargingInfo[_index]->SystemStatus == SYS_MODE_RESERVATION)
  8583. {
  8584. if (opcc_chk_reserve_expired(_index))
  8585. {
  8586. chargingInfo[_index]->SystemStatus = SYS_MODE_IDLE;
  8587. }
  8588. }
  8589. // OTP
  8590. CheckConnectorOTP(_index);
  8591. if (ShmSmartBoxData->ConnectorStatus[_index].ConnectorStaus != _CONNECTOR_STATUS_USING)
  8592. isBothCharging = false;
  8593. }
  8594. if (isBothCharging)
  8595. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_BOTH;
  8596. else
  8597. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_NORMAL;
  8598. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  8599. {
  8600. if ((ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING) ||
  8601. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  8602. {
  8603. ocpp_ac_chargingProfile_process();
  8604. }
  8605. if ((ac_chargingInfo[_index]->SystemStatus >= SYS_MODE_PREPARING &&
  8606. ac_chargingInfo[_index]->SystemStatus <= SYS_MODE_CHARGING))
  8607. {
  8608. if (ac_chargingInfo[_index]->SystemStatus == SYS_MODE_CHARGING &&
  8609. _ac_ocppProfileChkFlag == 12)
  8610. {
  8611. ocpp_ac_lift_profileReq_cmd(_index);
  8612. _ac_ocppProfileChkFlag = 0;
  8613. }
  8614. else if (ac_chargingInfo[_index]->SystemStatus != SYS_MODE_CHARGING)
  8615. {
  8616. ocpp_ac_lift_profileReq_cmd(_index);
  8617. _ac_ocppProfileChkFlag = 0;
  8618. }
  8619. else
  8620. _ac_ocppProfileChkFlag++;
  8621. }
  8622. }
  8623. CheckOcppCostAndPrice();
  8624. CheckAuthrizeByEvccid();
  8625. GetTimespecFunc(&_cmdMainPriority_time);
  8626. if (!DEBUG_ALSTON && wtdFd == -1)
  8627. {
  8628. PRINTF_FUNC("===== Create crazy dog ===== \n");
  8629. CreateWatchdog();
  8630. }
  8631. }
  8632. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
  8633. ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L')
  8634. CheckStatusOfMeter();
  8635. // 確認當前錯誤 Level = 2 ?
  8636. ReviewCriticalAlarm();
  8637. errCollect.GunErrMessage = 0;
  8638. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8639. {
  8640. CheckAcOcppProfileConfirm();
  8641. ocpp_ac_chk_reserved_cmd();
  8642. }
  8643. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8644. {
  8645. //CheckGpioInStatus();
  8646. // 確認系統的錯誤狀態
  8647. CheckSystemErrorFunction(gun_index);
  8648. // 確認 Relay Welding or Driving Fault
  8649. CheckRelayWeldingOrDrivingFault(gun_index);
  8650. // 收集各槍的錯誤狀態
  8651. CollectError(gun_index);
  8652. ocpp_chk_reserved_cmd(gun_index);
  8653. ocpp_chk_availability_cmd(gun_index);
  8654. ocpp_chk_unlock_cmd(gun_index);
  8655. ocpp_default_errorCode_cmd(gun_index);
  8656. // check charging profile confirm flag
  8657. CheckOcppProfileConfirm(gun_index);
  8658. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  8659. switch(chargingInfo[gun_index]->SystemStatus)
  8660. {
  8661. case SYS_MODE_IDLE:
  8662. case SYS_MODE_RESERVATION:
  8663. case SYS_MODE_MAINTAIN:
  8664. case SYS_MODE_FAULT:
  8665. {
  8666. if ((chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE ||
  8667. chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION) &&
  8668. isModeChange(gun_index))
  8669. {
  8670. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  8671. {
  8672. PRINTF_FUNC("================= SYS_MODE_IDLE (%x) ================ \n", gun_index);
  8673. chargingInfo[gun_index]->ReservationId = -1;
  8674. }
  8675. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  8676. {
  8677. PRINTF_FUNC("================= SYS_MODE_RESERVATION (%x) ================ \n", gun_index);
  8678. PRINTF_FUNC("================= Id : %s ================ \n", ShmDcCommonData->_reserved_UserId[gun_index]);
  8679. }
  8680. chargingInfo[gun_index]->PresentChargedDuration = 0;
  8681. chargingInfo[gun_index]->RemainChargingDuration = 0;
  8682. chargingInfo[gun_index]->PresentChargingVoltage = 0;
  8683. chargingInfo[gun_index]->PresentChargingCurrent = 0;
  8684. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "00:00:00");
  8685. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "00:00:00");
  8686. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  8687. strcpy((char *)chargingInfo[gun_index]->EVCCID, "");
  8688. _isSendStartTransaction[gun_index] = false;
  8689. ocpp_auto_response_StopTransationConf(gun_index);
  8690. ocpp_auto_response_StopTransationConfPH(gun_index);
  8691. ClearEnergyTimePeriod(gun_index);
  8692. ocpp_clear_stopReason(gun_index);
  8693. ocpp_clear_stopTansaction_Idtag(gun_index);//LWADD
  8694. ocpp_auto_response_StartTransationConf(gun_index);
  8695. ocpp_auto_response_StartTransationConfPH(gun_index);
  8696. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_WAIT;
  8697. ShmDcCommonData->_authWithCcidFlag[gun_index] = _CCID_NONE;
  8698. ShmDcCommonData->startTransactionID[gun_index] = 0;
  8699. ShmDcCommonData->_ctepEnergyCost [gun_index] = 0;
  8700. ShmDcCommonData->_ctepParkingFee [gun_index] = 0;
  8701. ShmDcCommonData->_ctepTotalCost [gun_index] = 0;
  8702. PresentChargedAcEnergyClear(gun_index);
  8703. for(byte _vCount = 0; _vCount < VOLTAGE_SAMPLE_COUNT; _vCount++)
  8704. {
  8705. ShmDcCommonData->_outputVoltageSapmle[gun_index][_vCount] = 0;
  8706. }
  8707. }
  8708. if (chargingInfo[gun_index]->deratingByConnOtp.deratingIndex != 0)
  8709. chargingInfo[gun_index]->deratingByConnOtp.deratingIndex = 0;
  8710. if (chargingInfo[gun_index]->IsAvailable == NO)
  8711. {
  8712. SetChargerMode(gun_index, SYS_MODE_MAINTAIN);
  8713. }
  8714. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  8715. {
  8716. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  8717. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  8718. else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_IDLE &&
  8719. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_RESERVATION &&
  8720. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_MAINTAIN &&
  8721. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_FAULT)
  8722. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8723. ClearDetectPluginFlag();
  8724. ocpp_set_errCode_cmd(gun_index);
  8725. SetChargerMode(gun_index, SYS_MODE_FAULT);
  8726. }
  8727. else
  8728. {
  8729. if (PrecheckIsPass(gun_index))
  8730. {
  8731. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_FAULT)
  8732. {
  8733. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8734. SetChargerMode(gun_index, SYS_MODE_IDLE);
  8735. }
  8736. CcidAuthorizationProcessing(gun_index);
  8737. //
  8738. {
  8739. // Idle 正常程序起點
  8740. // 判斷是否有啟用檢查插槍
  8741. if(isDetectPlugin())
  8742. {
  8743. // 卡號驗證成功後,等待充電槍插入充電車
  8744. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  8745. {
  8746. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  8747. chargingInfo[gun_index]->IsAvailable)
  8748. {
  8749. PRINTF_FUNC("Conn %d Start Charging : Remote \n", gun_index);
  8750. chargingInfo[gun_index]->RemoteStartFlag = NO;
  8751. chargingInfo[gun_index]->isRemoteStart = YES;
  8752. ChangeGunSelectByIndex(gun_index);
  8753. AddPlugInTimes(gun_index);
  8754. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8755. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  8756. ClearDetectPluginFlag();
  8757. continue;
  8758. }
  8759. }
  8760. else if (chargingInfo[gun_index]->ReservedStartFlag == YES)
  8761. {
  8762. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  8763. chargingInfo[gun_index]->IsAvailable)
  8764. {
  8765. chargingInfo[gun_index]->ReservedStartFlag = NO;
  8766. ChangeGunSelectByIndex(gun_index);
  8767. AddPlugInTimes(gun_index);
  8768. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmDcCommonData->_reserved_UserId[gun_index]);
  8769. PRINTF_FUNC("Conn %d Start Charging : Reserved CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  8770. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8771. strcpy((char *)ShmDcCommonData->_reserved_UserId[gun_index], "");
  8772. // 當前操作的槍號,進入 Preparing
  8773. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8774. ClearDetectPluginFlag();
  8775. continue;
  8776. }
  8777. }
  8778. else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE)
  8779. {
  8780. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  8781. chargingInfo[gun_index]->IsAvailable &&
  8782. chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  8783. {
  8784. ChangeGunSelectByIndex(gun_index);
  8785. AddPlugInTimes(gun_index);
  8786. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  8787. PRINTF_FUNC("Conn %d Start Charging : RFID CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  8788. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8789. // 當前操作的槍號,進入 Preparing
  8790. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8791. ClearDetectPluginFlag();
  8792. continue;
  8793. }
  8794. }
  8795. if (!isCardScan &&
  8796. (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected ||
  8797. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)))
  8798. {
  8799. // LCM => Waiting for plugging
  8800. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8801. }
  8802. }
  8803. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  8804. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  8805. {
  8806. PRINTF_FUNC("Conn %d Start Charging : Plug and charing \n", gun_index);
  8807. bool isCanStartChargingFlag = GetStartChargingByAlterMode(gun_index);
  8808. if (isCanStartChargingFlag)
  8809. {
  8810. ChangeGunSelectByIndex(gun_index);
  8811. AddPlugInTimes(gun_index);
  8812. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8813. ClearDetectPluginFlag();
  8814. ocpp_get_freeVendor(gun_index);
  8815. continue;
  8816. }
  8817. }
  8818. else
  8819. {
  8820. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8821. {
  8822. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8823. ocpp_remote_start_wait_cmd()))
  8824. {
  8825. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  8826. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  8827. {
  8828. if (ac_chargingInfo[0]->ConnectorPlugIn == AC_SYS_A)
  8829. {
  8830. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8831. }
  8832. else
  8833. {
  8834. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8835. }
  8836. }
  8837. else
  8838. {
  8839. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8840. }
  8841. }
  8842. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  8843. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_WAIT_FOR_PLUG)
  8844. {
  8845. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8846. }
  8847. // else if (ChkAcModuleStatusIsNone() &&
  8848. // (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING && ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL))
  8849. // {
  8850. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8851. // }
  8852. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  8853. }
  8854. }
  8855. } // Idle 正常程序終點
  8856. }
  8857. else
  8858. {
  8859. SetChargerMode(gun_index, SYS_MODE_FAULT);
  8860. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  8861. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  8862. }
  8863. ReleaseSysAlarmCode(gun_index);
  8864. }
  8865. }
  8866. break;
  8867. case SYS_MODE_MODE_REASSIGN_CHECK:
  8868. {
  8869. if (isModeChange(gun_index))
  8870. {
  8871. PRINTF_FUNC("================= SYS_MODE_MODE_REASSIGN_CHECK (%x) ================ \n", gun_index);
  8872. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8873. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  8874. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  8875. StopSystemTimeoutDet();
  8876. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE;
  8877. }
  8878. ocpp_clear_errorCode_cmd(gun_index);
  8879. if (ShmSmartBoxData->ConnectorStatus[gun_index].ConnectorStaus == _CONNECTOR_STATUS_WAIT)
  8880. {
  8881. PRINTF_FUNC("Conn_%d wait for Psu Module. \n", gun_index);
  8882. SetChargerMode(gun_index, SYS_MODE_REASSIGN);
  8883. }
  8884. else if (ShmSmartBoxData->ConnectorStatus[gun_index].ConnectorStaus == _CONNECTOR_STATUS_USING)
  8885. {
  8886. PRINTF_FUNC("Conn_%d start charging with Psu Module. \n", gun_index);
  8887. SetChargerMode(gun_index, SYS_MODE_PREPARING);
  8888. }
  8889. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8890. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8891. {
  8892. if (IsNoneSelectAcModule())
  8893. {
  8894. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8895. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8896. {
  8897. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8898. }
  8899. }
  8900. else
  8901. {
  8902. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8903. ocpp_remote_start_wait_cmd ()))
  8904. {
  8905. DisplayPageChkByAcModule();
  8906. }
  8907. }
  8908. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8909. }
  8910. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8911. {
  8912. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8913. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8914. }
  8915. }
  8916. break;
  8917. case SYS_MODE_REASSIGN:
  8918. {
  8919. if (isModeChange(gun_index))
  8920. {
  8921. PRINTF_FUNC("================== SYS_MODE_REASSIGN (%x) ================ \n", gun_index);
  8922. }
  8923. ocpp_clear_errorCode_cmd(gun_index);
  8924. if (ShmSmartBoxData->ConnectorStatus[gun_index].ConnectorStaus == _CONNECTOR_STATUS_USING)
  8925. SetChargerMode(gun_index, SYS_MODE_PREPARING);
  8926. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8927. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8928. {
  8929. if (IsNoneSelectAcModule())
  8930. {
  8931. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8932. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8933. {
  8934. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8935. }
  8936. }
  8937. else
  8938. {
  8939. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8940. ocpp_remote_start_wait_cmd ()))
  8941. {
  8942. DisplayPageChkByAcModule();
  8943. }
  8944. }
  8945. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8946. }
  8947. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8948. {
  8949. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8950. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8951. }
  8952. }
  8953. break;
  8954. case SYS_MODE_PREPARING:
  8955. {
  8956. if (isModeChange(gun_index))
  8957. {
  8958. PRINTF_FUNC("=================== SYS_MODE_PREPARING (%x) =============== \n", gun_index);
  8959. StopGunInfoTimeoutDet(gun_index);
  8960. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  8961. }
  8962. ocpp_clear_errorCode_cmd(gun_index);
  8963. if (chargingInfo[gun_index]->AvailableChargingPower > 0 &&
  8964. ShmPsuData->Work_Step == _WORK_CHARGING)
  8965. {
  8966. SetChargerMode(gun_index, SYS_MODE_PREPARE_FOR_EV);
  8967. }
  8968. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8969. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8970. {
  8971. if (IsNoneSelectAcModule())
  8972. {
  8973. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8974. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8975. {
  8976. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8977. }
  8978. }
  8979. else
  8980. {
  8981. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8982. ocpp_remote_start_wait_cmd ()))
  8983. {
  8984. DisplayPageChkByAcModule();
  8985. }
  8986. }
  8987. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8988. }
  8989. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8990. {
  8991. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8992. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8993. }
  8994. }
  8995. break;
  8996. case SYS_MODE_PREPARE_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  8997. {
  8998. if (isModeChange(gun_index))
  8999. {
  9000. PRINTF_FUNC("================== SYS_MODE_PREPARE_FOR_EV (%x) ================ \n", gun_index);
  9001. _presentChargingTimeBuf[gun_index] = 0;
  9002. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  9003. StopGunInfoTimeoutDet(gun_index);
  9004. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  9005. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_WAIT;
  9006. // Start Transation
  9007. PRINTF_FUNC("StartUserId = %s \n", chargingInfo[gun_index]->StartUserId);
  9008. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9009. strcmp((char *)chargingInfo[gun_index]->StartUserId, "") != EQUAL ||
  9010. chargingInfo[gun_index]->isRemoteStart == YES)
  9011. {
  9012. _isSendStartTransaction[gun_index] = true;
  9013. ocpp_set_startTransation_cmd(gun_index);
  9014. ocpp_set_startTransationPH_cmd(gun_index);
  9015. GetTimespecFunc(&_startTransation_time[gun_index]);
  9016. }
  9017. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  9018. }
  9019. ocpp_clear_errorCode_cmd(gun_index);
  9020. CheckCcidAuthorizationFunction(gun_index);
  9021. if (!_isSendStartTransaction[gun_index] &&
  9022. chargingInfo [gun_index]->Type == _Type_CCS &&
  9023. ShmDcCommonData->enObtainAuthorizbyCCID)
  9024. {
  9025. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_AUTHCOMP)
  9026. {
  9027. PRINTF_FUNC("_CCID_AUTHCOMP \n");
  9028. _isSendStartTransaction[gun_index] = true;
  9029. ocpp_set_startTransation_cmd(gun_index);
  9030. ocpp_set_startTransationPH_cmd(gun_index);
  9031. GetTimespecFunc(&_startTransation_time[gun_index]);
  9032. }
  9033. else if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_AUTHFAIL)
  9034. {
  9035. PRINTF_FUNC("_CCID_AUTHFAIL \n");
  9036. ChargingTerminalProcess(gun_index);
  9037. }
  9038. }
  9039. ShmDcCommonData->startTransactionFlag[gun_index] = ocpp_is_resPass_StartTransactionConf(gun_index);
  9040. if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_PASS)
  9041. {
  9042. if (ocpp_chk_invalid_id_cmd(gun_index))
  9043. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_FAIL;
  9044. }
  9045. else
  9046. {
  9047. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  9048. {
  9049. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING ||
  9050. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9051. !ocpp_get_startTransation_req(gun_index) ||
  9052. strcmp((char *)chargingInfo[gun_index]->StartUserId, "") != EQUAL ||
  9053. _isTestRun)
  9054. {
  9055. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_PASS;
  9056. }
  9057. }
  9058. }
  9059. // Offline policy - No charging
  9060. ChkNoCharging(gun_index);
  9061. // 小板停止
  9062. ChkConnectorBoardStop(gun_index);
  9063. // psu 停止
  9064. ChkPsuErrorStop(gun_index);
  9065. if (ocpp_chk_remoteStop_cmd(gun_index) == YES ||
  9066. WifiScheduleStop(gun_index) ||
  9067. (_isSendStartTransaction[gun_index] && ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_FAIL) ||
  9068. (_isSendStartTransaction[gun_index] && ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_WAIT && GetTimeoutValue(&_startTransation_time[gun_index]) > 45))
  9069. {
  9070. // 後臺要求停止
  9071. ChargingTerminalProcess(gun_index);
  9072. if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_FAIL)
  9073. PRINTF_FUNC("StartTransaction req fail. (%d) \n", gun_index);
  9074. else if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_WAIT)
  9075. PRINTF_FUNC("StartTransaction req timeout. (%d) \n", gun_index);
  9076. }
  9077. // 檢查車端的槍鎖是否為鎖上
  9078. if (isEvGunLocked(gun_index) == YES)
  9079. {
  9080. SetChargerMode(gun_index, SYS_MODE_PREPARE_FOR_EVSE);
  9081. }
  9082. // LCM => Pre-charging
  9083. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9084. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9085. {
  9086. if (IsNoneSelectAcModule())
  9087. {
  9088. // if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9089. // ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9090. // {
  9091. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9092. // }
  9093. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  9094. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  9095. { }
  9096. else
  9097. {
  9098. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9099. }
  9100. }
  9101. else
  9102. {
  9103. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9104. ocpp_remote_start_wait_cmd ()))
  9105. {
  9106. DisplayPageChkByAcModule();
  9107. }
  9108. }
  9109. if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_NONE)
  9110. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9111. }
  9112. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9113. {
  9114. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  9115. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  9116. {}
  9117. else
  9118. {
  9119. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9120. }
  9121. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9122. }
  9123. }
  9124. break;
  9125. case SYS_MODE_PREPARE_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  9126. {
  9127. if (isModeChange(gun_index))
  9128. {
  9129. PRINTF_FUNC("================== SYS_MODE_PREPARE_FOR_EVSE (%x) ================ \n", gun_index);
  9130. StopGunInfoTimeoutDet(gun_index);
  9131. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  9132. }
  9133. ocpp_clear_errorCode_cmd(gun_index);
  9134. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  9135. {
  9136. // 檢查樁端的 GFD 結果
  9137. if (isPrechargeStatus_chademo(gun_index) >= 6)
  9138. {
  9139. // 當前操作的槍號,進入 Charging
  9140. SetChargerMode(gun_index, SYS_MODE_CHARGING);
  9141. }
  9142. }
  9143. else if (chargingInfo[gun_index]->Type == _Type_GB)
  9144. {
  9145. // 檢查樁端的 GFD 結果
  9146. if (isPrechargeStatus_gb(gun_index) >= 6)
  9147. {
  9148. SetChargerMode(gun_index, SYS_MODE_CHARGING);
  9149. }
  9150. }
  9151. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  9152. {
  9153. // 檢查樁端的 GFD 結果
  9154. if ((chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  9155. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING))
  9156. {
  9157. SetChargerMode(gun_index, SYS_MODE_CCS_PRECHARGE_STEP0);
  9158. }
  9159. }
  9160. RecordGfdResult(gun_index);
  9161. // Offline policy - No charging
  9162. ChkNoCharging ( gun_index );
  9163. // 小板停止
  9164. ChkConnectorBoardStop(gun_index);
  9165. // psu 停止
  9166. ChkPsuErrorStop(gun_index);
  9167. // 後台要求停止
  9168. ChkBackendStop(gun_index);
  9169. // LCM => Pre-charging
  9170. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9171. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9172. {
  9173. if (IsNoneSelectAcModule())
  9174. {
  9175. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9176. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9177. {
  9178. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9179. }
  9180. }
  9181. else
  9182. {
  9183. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9184. ocpp_remote_start_wait_cmd ()))
  9185. {
  9186. DisplayPageChkByAcModule();
  9187. }
  9188. }
  9189. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9190. }
  9191. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9192. {
  9193. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9194. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9195. }
  9196. }
  9197. break;
  9198. case SYS_MODE_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  9199. {
  9200. if (isModeChange(gun_index))
  9201. {
  9202. PRINTF_FUNC("================== SYS_MODE_CHARGING (%x) ================ \n", gun_index);
  9203. StopGunInfoTimeoutDet(gun_index);
  9204. ftime(&startChargingTime[gun_index]);
  9205. ocpp_clear_idTag_cmd(gun_index);
  9206. ChangeStartOrStopDateTime(YES, gun_index);
  9207. if (_isTestRun == YES)
  9208. {
  9209. GetTimespecFunc(&_autoRuntestTime);
  9210. }
  9211. chargingInfo[gun_index]->EvBatteryStartSoc = chargingInfo[gun_index]->EvBatterySoc;
  9212. }
  9213. ocpp_clear_errorCode_cmd(gun_index);
  9214. if (_isTestRun == YES)
  9215. {
  9216. if (GetTimeoutValue(&_autoRuntestTime) > 60)
  9217. {
  9218. ChargingTerminalProcess(gun_index);
  9219. }
  9220. }
  9221. SavePresentChargedDuration(gun_index);
  9222. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  9223. {
  9224. if (ShmDcCommonData->minOutputCur != 0 &&
  9225. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  9226. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  9227. {
  9228. // UCP
  9229. RecordAlarmCode(gun_index, "012320");
  9230. ChargingTerminalProcess(gun_index);
  9231. }
  9232. }
  9233. else if (chargingInfo[gun_index]->Type == _Type_GB)
  9234. {
  9235. if (ShmDcCommonData->minOutputCur != 0 &&
  9236. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  9237. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  9238. {
  9239. // UCP
  9240. RecordAlarmCode(gun_index, "012322");
  9241. ChargingTerminalProcess(gun_index);
  9242. }
  9243. }
  9244. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  9245. {
  9246. if (ShmDcCommonData->minOutputCur != 0 &&
  9247. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  9248. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  9249. {
  9250. // UCP
  9251. RecordAlarmCode(gun_index, "012321");
  9252. ChargingTerminalProcess(gun_index);
  9253. }
  9254. }
  9255. RecordGfdResult(gun_index);
  9256. // Offline policy - No charging
  9257. ChkNoCharging(gun_index);
  9258. // 小板停止
  9259. ChkConnectorBoardStop(gun_index);
  9260. // psu 停止
  9261. ChkPsuErrorStop(gun_index);
  9262. // 後台要求停止
  9263. ChkBackendStop(gun_index);
  9264. // 設定停止
  9265. ChkConfigurationStop(gun_index);
  9266. // LCM => Charging
  9267. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9268. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9269. {
  9270. if (IsNoneSelectAcModule() || !stopChargingChkByCard)
  9271. {
  9272. // if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9273. // ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9274. // {
  9275. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9276. // }
  9277. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  9278. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  9279. { }
  9280. else
  9281. {
  9282. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9283. }
  9284. }
  9285. else
  9286. {
  9287. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9288. ocpp_remote_start_wait_cmd ()))
  9289. {
  9290. DisplayPageChkByAcModule();
  9291. }
  9292. }
  9293. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  9294. }
  9295. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9296. {
  9297. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  9298. if (!stopChargingChkByCard)
  9299. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9300. }
  9301. }
  9302. break;
  9303. case SYS_MODE_ALARM:
  9304. case SYS_MODE_TERMINATING:
  9305. {
  9306. if (isModeChange(gun_index))
  9307. {
  9308. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip ||
  9309. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  9310. ocpp_set_stopReason_by_cmd(gun_index, "EmergencyStop");
  9311. else
  9312. ocpp_set_stopReason_by_cmd(gun_index, "Local");
  9313. ocpp_copy_stopReason2maintainServer(gun_index);
  9314. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  9315. {
  9316. PRINTF_FUNC("================== SYS_MODE_ALARM (%x) ================ \n", gun_index);
  9317. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  9318. {
  9319. ocpp_auto_response_StartTransationConf(gun_index);
  9320. ocpp_auto_response_StartTransationConfPH(gun_index);
  9321. if (_isSendStartTransaction[gun_index])
  9322. {
  9323. ocpp_set_stopTransation_cmd(gun_index);
  9324. ocpp_set_stopTransationPH_cmd(gun_index);
  9325. }
  9326. }
  9327. TheEndCharging(gun_index);
  9328. }
  9329. else
  9330. {
  9331. PRINTF_FUNC("================== SYS_MODE_TERMINATING (%x) ================ \n", gun_index);
  9332. }
  9333. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = NO;
  9334. StopGunInfoTimeoutDet(gun_index);
  9335. }
  9336. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  9337. {
  9338. ocpp_set_errCode_cmd(gun_index);
  9339. }
  9340. if (chargingInfo[gun_index]->Type == _Type_Chademo) { }
  9341. else if (chargingInfo[gun_index]->Type == _Type_GB) { }
  9342. else if (chargingInfo[gun_index]->Type == _Type_CCS) { }
  9343. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  9344. {
  9345. if (chargingInfo [gun_index]->ConnectorPlugIn == NO)
  9346. {
  9347. if (ShmDcCommonData->_returnIdleHoldFlag [gun_index] == NO &&
  9348. GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 10)
  9349. {
  9350. ShmDcCommonData->_returnIdleHoldFlag [gun_index] = YES;
  9351. StopGunInfoTimeoutDet ( gun_index );
  9352. StartGunInfoTimeoutDet ( gun_index, Timeout_EvseCompleteDet );
  9353. }
  9354. else if (ShmDcCommonData->_returnIdleHoldFlag [gun_index] == YES &&
  9355. GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 2)
  9356. {
  9357. SetChargerMode ( gun_index, SYS_MODE_IDLE );
  9358. }
  9359. }
  9360. if (_isTestRun == YES &&
  9361. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  9362. {
  9363. SetChargerMode(gun_index, SYS_MODE_IDLE);
  9364. }
  9365. }
  9366. else
  9367. {
  9368. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  9369. {
  9370. if (isEvGunLocked(gun_index) == NO ||
  9371. isPrechargeStatus_chademo(gun_index) <= 0)
  9372. {
  9373. SetChargerMode(gun_index, SYS_MODE_COMPLETE);
  9374. }
  9375. }
  9376. else if (chargingInfo[gun_index]->Type == _Type_GB)
  9377. {
  9378. if (isEvGunLocked(gun_index) == NO ||
  9379. isPrechargeStatus_gb(gun_index) <= 0)
  9380. {
  9381. SetChargerMode(gun_index, SYS_MODE_COMPLETE);
  9382. }
  9383. }
  9384. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  9385. {
  9386. if (isEvGunLocked(gun_index) == NO &&
  9387. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  9388. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14))
  9389. {
  9390. SetChargerMode(gun_index, SYS_MODE_COMPLETE);
  9391. }
  9392. }
  9393. }
  9394. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9395. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9396. {
  9397. if (IsNoneSelectAcModule())
  9398. {
  9399. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9400. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9401. {
  9402. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9403. }
  9404. }
  9405. else
  9406. {
  9407. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9408. ocpp_remote_start_wait_cmd ()))
  9409. {
  9410. DisplayPageChkByAcModule();
  9411. }
  9412. }
  9413. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9414. }
  9415. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9416. {
  9417. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9418. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9419. }
  9420. }
  9421. break;
  9422. case SYS_MODE_COMPLETE:
  9423. {
  9424. if (isModeChange(gun_index))
  9425. {
  9426. PRINTF_FUNC("================== SYS_MODE_COMPLETE (%x) ================ \n", gun_index);
  9427. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  9428. {
  9429. ocpp_set_errCode_cmd(gun_index);
  9430. ocpp_auto_response_StartTransationConf(gun_index);
  9431. ocpp_auto_response_StartTransationConfPH(gun_index);
  9432. if (_isSendStartTransaction[gun_index])
  9433. {
  9434. ocpp_set_stopTransation_cmd(gun_index);
  9435. ocpp_set_stopTransationPH_cmd(gun_index);
  9436. }
  9437. }
  9438. TheEndCharging(gun_index);
  9439. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = NO;
  9440. }
  9441. ocpp_clear_errorCode_cmd(gun_index);
  9442. if (chargingInfo[gun_index]->ConnectorPlugIn == NO)
  9443. {
  9444. if (ShmDcCommonData->_returnIdleHoldFlag[gun_index] == NO &&
  9445. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  9446. {
  9447. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = YES;
  9448. StopGunInfoTimeoutDet(gun_index);
  9449. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  9450. }
  9451. else if (ShmDcCommonData->_returnIdleHoldFlag[gun_index] == YES &&
  9452. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 2)
  9453. {
  9454. SetChargerMode(gun_index, SYS_MODE_IDLE);
  9455. }
  9456. }
  9457. if (_isTestRun == YES &&
  9458. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  9459. {
  9460. SetChargerMode(gun_index, SYS_MODE_IDLE);
  9461. }
  9462. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9463. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9464. {
  9465. if (IsNoneSelectAcModule())
  9466. {
  9467. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9468. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9469. {
  9470. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9471. }
  9472. }
  9473. else
  9474. {
  9475. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9476. ocpp_remote_start_wait_cmd ()))
  9477. {
  9478. DisplayPageChkByAcModule();
  9479. }
  9480. }
  9481. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9482. }
  9483. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9484. {
  9485. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9486. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9487. }
  9488. }
  9489. break;
  9490. case SYS_MODE_CCS_PRECHARGE_STEP0:
  9491. {
  9492. if (isModeChange(gun_index))
  9493. {
  9494. PRINTF_FUNC("================== SYS_MODE_CCS_PRECHARGE_STEP0 (%x) ================ \n", gun_index);
  9495. StopGunInfoTimeoutDet(gun_index);
  9496. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  9497. }
  9498. ocpp_clear_errorCode_cmd(gun_index);
  9499. RecordGfdResult(gun_index);
  9500. // Offline policy - No charging
  9501. ChkNoCharging(gun_index);
  9502. // 小板停止
  9503. ChkConnectorBoardStop(gun_index);
  9504. // psu 停止
  9505. ChkPsuErrorStop(gun_index);
  9506. // 後台要求停止
  9507. ChkBackendStop(gun_index);
  9508. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  9509. // 切換 D+ Relay to Precharge Relay
  9510. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  9511. {
  9512. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  9513. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  9514. {
  9515. if (isPrechargeStatus_ccs(gun_index) == 39)
  9516. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PreChargeResponse (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  9517. else if (isPrechargeStatus_ccs(gun_index) == 40)
  9518. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PowerDeliveryRequest start (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  9519. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  9520. }
  9521. }
  9522. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  9523. {
  9524. SetChargerMode(gun_index, SYS_MODE_CCS_PRECHARGE_STEP1);
  9525. }
  9526. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9527. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9528. {
  9529. if (IsNoneSelectAcModule())
  9530. {
  9531. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9532. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9533. {
  9534. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9535. }
  9536. }
  9537. else
  9538. {
  9539. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9540. ocpp_remote_start_wait_cmd ()))
  9541. {
  9542. DisplayPageChkByAcModule();
  9543. }
  9544. }
  9545. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9546. }
  9547. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9548. {
  9549. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9550. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9551. }
  9552. break;
  9553. }
  9554. case SYS_MODE_CCS_PRECHARGE_STEP1:
  9555. {
  9556. if (isModeChange(gun_index))
  9557. {
  9558. PRINTF_FUNC("================== SYS_MODE_CCS_PRECHARGE_STEP1 (%x) ================ \n", gun_index);
  9559. }
  9560. ocpp_clear_errorCode_cmd(gun_index);
  9561. RecordGfdResult(gun_index);
  9562. // Offline policy - No charging
  9563. ChkNoCharging(gun_index);
  9564. // 小板停止
  9565. ChkConnectorBoardStop(gun_index);
  9566. // psu 停止
  9567. ChkPsuErrorStop(gun_index);
  9568. // 後台要求停止
  9569. ChkBackendStop(gun_index);
  9570. // 等待小板通知進入充電
  9571. // 切換 D+ Relay to Precharge Relay
  9572. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  9573. {
  9574. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  9575. SetChargerMode(gun_index, SYS_MODE_CHARGING);
  9576. }
  9577. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9578. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9579. {
  9580. if (IsNoneSelectAcModule())
  9581. {
  9582. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9583. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9584. {
  9585. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9586. }
  9587. }
  9588. else
  9589. {
  9590. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9591. ocpp_remote_start_wait_cmd ()))
  9592. {
  9593. DisplayPageChkByAcModule();
  9594. }
  9595. }
  9596. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9597. }
  9598. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9599. {
  9600. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9601. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9602. }
  9603. break;
  9604. }
  9605. }
  9606. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE ||
  9607. chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION ||
  9608. chargingInfo[gun_index]->SystemStatus == SYS_MODE_MAINTAIN ||
  9609. chargingInfo[gun_index]->SystemStatus == SYS_MODE_FAULT)
  9610. {
  9611. ocpp_chk_notificationMessage_cmd(gun_index);
  9612. }
  9613. }
  9614. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  9615. {
  9616. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  9617. }
  9618. else
  9619. {
  9620. bool dcPageRun = false;
  9621. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  9622. {
  9623. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE)
  9624. ChangeLcmByIndex(_LCM_IDLE);
  9625. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_PREPARING)
  9626. ChangeLcmByIndex(_LCM_PRE_CHARGE);
  9627. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  9628. ChangeLcmByIndex(_LCM_CHARGING);
  9629. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_TERMINATING ||
  9630. ac_chargingInfo[0]->SystemStatus == SYS_MODE_COMPLETE)
  9631. ChangeLcmByIndex(_LCM_COMPLETE);
  9632. else
  9633. dcPageRun = true;
  9634. }
  9635. else
  9636. dcPageRun = true;
  9637. if (dcPageRun)
  9638. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  9639. }
  9640. bool isCharging = false;
  9641. SystemAlarmCheck();
  9642. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  9643. {
  9644. AlarmCheck(chargingInfo[gun_index]->Type);
  9645. if (_isTestRun == YES)
  9646. {
  9647. if (IsGunUsing(gun_index))
  9648. {
  9649. isCharging = true;
  9650. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_COMPLETE ||
  9651. chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  9652. {
  9653. GetTimespecFunc(&_autoRuntestTime);
  9654. }
  9655. }
  9656. }
  9657. }
  9658. if (_isTestRun == YES)
  9659. {
  9660. if (!isCharging)
  9661. {
  9662. if (GetTimeoutValue(&_autoRuntestTime) >= 60)
  9663. {
  9664. DetectPluginStart();
  9665. GetTimespecFunc(&_autoRuntestTime);
  9666. }
  9667. }
  9668. else
  9669. {
  9670. ClearDetectPluginFlag();
  9671. }
  9672. }
  9673. write(wtdFd, "a", 1);
  9674. usleep(whileLoopTime);
  9675. }
  9676. return FAIL;
  9677. }