main.c 350 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <stdarg.h>
  15. #include <stdio.h> /*標準輸入輸出定義*/
  16. #include <stdlib.h> /*標準函數庫定義*/
  17. #include <unistd.h> /*Unix 標準函數定義*/
  18. #include <fcntl.h> /*檔控制定義*/
  19. #include <termios.h> /*PPSIX 終端控制定義*/
  20. #include <errno.h> /*錯誤號定義*/
  21. #include <string.h>
  22. #include <stdint.h>
  23. #include <time.h>
  24. #include <ctype.h>
  25. #include <ifaddrs.h>
  26. #include <math.h>
  27. #include <stdbool.h>
  28. #include <dirent.h>
  29. #include "../../define.h"
  30. #include "CheckSystemTask.h"
  31. #include "Config.h"
  32. #include "timeout.h"
  33. #define DEBUG_ALSTON 0
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. #define MODELNAME_FAIL 0
  38. #define BUFFER_SIZE 128
  39. #define YES 1
  40. #define NO 0
  41. #define NORMAL 0
  42. #define ABNORMAL 1
  43. #define EQUAL 0
  44. #define BTN_RELEASE 0
  45. #define BTN_PRESS 1
  46. #define MAX_BUF 64
  47. //#define MtdBlockSize 0x600000
  48. #define MtdBlockSize 0x300000
  49. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  50. #define UPGRADE_FAN 0x02
  51. #define UPGRADE_RB 0x03
  52. #define UPGRADE_PRI 0x04
  53. #define UPGRADE_AC 0x05
  54. #define UPGRADE_LED 0x06
  55. #define SYSTEM_MIN_VOL 80
  56. #define MIN_OUTPUT_CUR 0
  57. #define AC_OUTPUT_VOL 220
  58. #define NO_RESERVE -1
  59. #define NO_DEFINE 255
  60. #define DEFAULT_AC_INDEX 2
  61. #define PSU_MIN_CUR 100
  62. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  63. #define NETWORK_DB_FILE "/Storage/EventLog/Eventlog.db"
  64. #define GUN_OTP_VALUE 150
  65. #define GUN_OTP_RECOVERY 140
  66. #define UNDEFINED_TEMP 255
  67. #define uSEC_VAL 1000000
  68. #define SELFTEST_TIMEOUT 60
  69. #define AUTHORIZE_TIMEOUT 15
  70. #define AUTHORIZE_COMP_TIMEOUT 3
  71. #define AUTHORIZE_FAIL_TIMEOUT 3
  72. #define AUTHORIZE_STOP_TIMEOUT 30
  73. #define RETURN_TO_CHARGING_PAGE 30
  74. #define GUN_PREPARE_TIMEOUT 30
  75. #define GUN_EV_WAIT_TIMEOUT 120
  76. #define GUN_EVSE_WAIT_TIMEOUT 60
  77. #define GUN_COMP_WAIT_TIMEOUT 3
  78. #define GUN_PRECHARGING_TIMEOUT 60
  79. #define TPC_PLUG_DETECT_TIMEOUT 60 //LW_tpc plug detect
  80. #define TIMEOUT_SPEC_HANDSHAKING 180000
  81. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  82. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  83. int whileLoopTime = 10000; // 10 ms
  84. int wtdFd = -1;
  85. byte _authorizeIndex = NO_DEFINE;
  86. bool IsAuthorizingMode();
  87. void ClearAuthorizedFlag();
  88. bool isDetectPlugin();
  89. void ClearDetectPluginFlag();
  90. int mystrcmp(unsigned char *p1, unsigned char *p2);
  91. void CheckTask();
  92. long long DiffTimebWithNow(struct timeb ST);
  93. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  94. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  95. void ChargingTerminalProcess(byte gunIndex);
  96. void ChkPrimaryStatus();
  97. void StartSystemTimeoutDet(unsigned char flag);
  98. void StopSystemTimeoutDet();
  99. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  100. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  101. int StoreLogMsg_1(const char *fmt, ...);
  102. int GetTimeoutValue(struct timespec *startTime);
  103. void gpio_set_value(unsigned int gpio, unsigned int value);
  104. void PRINTF_FUNC(char *string, ...);
  105. void ChangeGunSelectByIndex(byte sel);
  106. void ChargingAlarmProcess(byte gunIndex);
  107. void CheckSystemErrorFunction(byte index);
  108. int opcc_chk_reserve_expired(byte gunIndex);
  109. void ocpp_process_start();
  110. void ocpp_auto_response_BootNotification();
  111. void ocpp_auto_response_StartTransationConf(byte gunIndex);
  112. void ocpp_auto_response_StopTransationConf(byte gunIndex);
  113. bool ocpp_chk_authrization_cmd();
  114. void ocpp_chk_reset_cmd(byte isforce);
  115. void ocpp_chk_reserved_cmd(byte gunIndex);
  116. void ocpp_chk_availability_cmd(byte gunIndex);
  117. void ocpp_chk_unlock_cmd(byte gunIndex);
  118. bool ocpp_chk_remoteStop_cmd(byte gunIndex);
  119. void ocpp_chk_remoteStart_cmd();
  120. void ocpp_chk_update_cmd();
  121. bool ocpp_chk_invalid_id_cmd(byte gunIndex);
  122. void ocpp_set_startTransation_cmd(byte gunIndex);
  123. void ocpp_set_stopTransation_cmd(byte gunIndex);
  124. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString);
  125. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason);
  126. void ocpp_set_authorizeReq_cmd(byte value);
  127. void ocpp_set_authorizeConf_cmd(byte value);
  128. void ocpp_set_errCode_cmd(byte gunIndex);
  129. void ocpp_clear_errorCode_cmd(byte gunIndex);
  130. void ocpp_clear_idTag_cmd(byte gun_index);
  131. void ocpp_clear_stopReason(byte gun_index);
  132. bool ocpp_get_authorize_conf();
  133. unsigned short _ocpp_get_connect_timeout();
  134. void ocpp_chargingProfile_process(byte gun_index);
  135. void ocpp_set_startTransationPH_cmd(byte gunIndex);
  136. void ocpp_auto_response_StartTransationConfPH(byte gunIndex);
  137. void ocpp_set_stopTransationPH_cmd(byte gunIndex);
  138. void ocpp_auto_response_StopTransationConfPH(byte gunIndex);
  139. void ocpp_copy_stopReason2maintainServer(byte gunIndex);
  140. void RecordAlarmCode(byte gunIndex, char *code);
  141. void ReleaseSysAlarmCode(byte gunIndex);
  142. void ReviewCriticalAlarm();
  143. int DB_Open(sqlite3 *db);
  144. int DB_Insert_Record(sqlite3 *db, int gun_index);
  145. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  146. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  147. void InitialDHCP();
  148. int GetStartScheduleTime(unsigned char *time);
  149. void KillAllTask();
  150. void CheckSystemTaskAlive();
  151. void CheckConnectorOTP(byte _index);
  152. void _DetectTpcPlugInTimeout();
  153. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  154. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  155. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  156. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  157. struct StatusCodeData *ShmStatusCodeData;
  158. struct PsuData *ShmPsuData;
  159. struct CHAdeMOData *ShmCHAdeMOData;
  160. struct GBTData *ShmGBTData;
  161. struct CcsData *ShmCcsData;
  162. struct PrimaryMcuData *ShmPrimaryMcuData;
  163. struct FanModuleData *ShmFanModuleData;
  164. struct RelayModuleData *ShmRelayModuleData;
  165. struct LedModuleData *ShmLedModuleData;
  166. struct OCPP16Data *ShmOCPP16Data;
  167. struct OCPP20Data *ShmOCPP20Data;
  168. struct OCPP16Data *ShmOCPP16DataPH;
  169. struct MeterInformation *ShmCsuMeterData;
  170. struct DcCommonInformation *ShmDcCommonData;
  171. struct SmartBoxData *ShmSmartBoxData;
  172. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  173. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  174. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  175. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  176. int _presentChargingTimeBuf[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  177. struct timespec _startTransation_time[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  178. float _ChargingProfilePwBuffer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  179. float _AcChargingProfileCurBuffer;
  180. bool _isSendStartTransaction[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  181. GunErr errCollect;
  182. struct timespec _autoRuntestTime;
  183. byte _isTestRun = NO;
  184. // for initial index to check EV board type is correct
  185. byte _gunIndex = 0;
  186. byte _acgunIndex = 0;
  187. byte _chademoIndex = 0;
  188. byte _ccsIndex = 0;
  189. byte _gb_Index = 0;
  190. byte _ac_Index = 0;
  191. byte bd0_1_status = 0;
  192. byte bd0_2_status = 0;
  193. byte bd1_1_status = 0;
  194. byte bd1_2_status = 0;
  195. bool isCardScan = false;
  196. bool isModelNameMatch = true;
  197. int rfidFd = -1;
  198. char* rfidPortName = "/dev/ttyS2";
  199. char* fwVersion = "V3.01.00.0000.00";
  200. sqlite3 *localDb;
  201. bool isDb_ready;
  202. sqlite3 *networkDb;
  203. char MyDefaultPriceString[128];
  204. byte reset4gStep = RESET_4G_STEP_NONE;
  205. // 避免槍溫偵測誤判
  206. byte gunTempAllowCount[2] = {0, 0};
  207. //================================================
  208. // initial can-bus
  209. //================================================
  210. int InitCanBus()
  211. {
  212. int s0,nbytes;
  213. struct timeval tv;
  214. struct ifreq ifr0;
  215. struct sockaddr_can addr0;
  216. system("/sbin/ip link set can0 down");
  217. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  218. system("/sbin/ip link set can0 up");
  219. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  220. tv.tv_sec = 0;
  221. tv.tv_usec = 10000;
  222. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  223. {
  224. #ifdef SystemLogMessage
  225. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  226. #endif
  227. }
  228. nbytes=40960;
  229. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  230. {
  231. #ifdef SystemLogMessage
  232. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  233. #endif
  234. }
  235. nbytes=40960;
  236. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  237. {
  238. #ifdef SystemLogMessage
  239. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  240. #endif
  241. }
  242. strcpy(ifr0.ifr_name, "can0" );
  243. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  244. addr0.can_family = AF_CAN;
  245. addr0.can_ifindex = ifr0.ifr_ifindex;
  246. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  247. return s0;
  248. }
  249. //================================================
  250. // initial uart port
  251. //================================================
  252. char *_priPortName = "/dev/ttyS1";
  253. char *_485PortName = "/dev/ttyS5";
  254. int InitComPort(byte target)
  255. {
  256. int fd;
  257. struct termios tios;
  258. if(target == UPGRADE_PRI)
  259. fd = open(_priPortName, O_RDWR);
  260. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  261. fd = open(_485PortName, O_RDWR);
  262. if(fd<=0)
  263. {
  264. #ifdef SystemLogMessage
  265. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  266. #endif
  267. return -1;
  268. }
  269. ioctl (fd, TCGETS, &tios);
  270. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  271. tios.c_lflag = 0;
  272. tios.c_iflag = 0;
  273. tios.c_oflag = 0;
  274. tios.c_cc[VMIN]=0;
  275. tios.c_cc[VTIME]=(unsigned char)5;
  276. tios.c_lflag=0;
  277. tcflush(fd, TCIFLUSH);
  278. ioctl (fd, TCSETS, &tios);
  279. return fd;
  280. }
  281. //=================================
  282. // Common routine
  283. //=================================
  284. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  285. {
  286. strncpy(dest, src + start, cnt);
  287. dest[cnt] = 0;
  288. }
  289. int InitWatchDog()
  290. {
  291. int fd;
  292. int timeout = 180;
  293. system("/usr/bin/fuser -k /dev/watchdog");
  294. sleep(1);
  295. system("echo V > /dev/watchdog");
  296. sleep(1);
  297. fd = open("/dev/watchdog", O_RDWR);
  298. if(fd <= 0)
  299. {
  300. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  301. }
  302. ioctl(fd, _IOWR('W', 6, int), &timeout);
  303. return fd;
  304. }
  305. void CloseWatchDog()
  306. {
  307. if (wtdFd > 0)
  308. {
  309. close(wtdFd);
  310. sleep(1);
  311. system("/usr/bin/fuser -k /dev/watchdog");
  312. sleep(1);
  313. system("echo V > /dev/watchdog");
  314. sleep(1);
  315. }
  316. }
  317. int StoreLogMsg_1(const char *fmt, ...)
  318. {
  319. char Buf[4096+256];
  320. char buffer[4096];
  321. va_list args;
  322. struct timeb SeqEndTime;
  323. struct tm *tm;
  324. va_start(args, fmt);
  325. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  326. va_end(args);
  327. memset(Buf,0,sizeof(Buf));
  328. ftime(&SeqEndTime);
  329. SeqEndTime.time = time(NULL);
  330. tm=localtime(&SeqEndTime.time);
  331. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES ||
  332. DEBUG_ALSTON)
  333. {
  334. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  335. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  336. printf("%s \n", Buf);
  337. }
  338. else
  339. {
  340. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog_%s_Log",
  341. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  342. buffer,
  343. tm->tm_year+1900,tm->tm_mon+1,
  344. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  345. system(Buf);
  346. }
  347. return rc;
  348. }
  349. //unsigned long GetTimeoutValue(struct timeval _sour_time)
  350. //{
  351. // struct timeval _end_time;
  352. // gettimeofday(&_end_time, NULL);
  353. //
  354. // return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  355. //}
  356. int GetTimeoutValue(struct timespec *startTime)
  357. {
  358. struct timespec endTime;
  359. clock_gettime(CLOCK_MONOTONIC_COARSE, &endTime);
  360. return endTime.tv_sec - startTime->tv_sec;
  361. }
  362. void GetTimespecFunc(struct timespec *time)
  363. {
  364. clock_gettime(CLOCK_MONOTONIC_COARSE, time);
  365. }
  366. int mystrcmp(unsigned char *p1, unsigned char *p2)
  367. {
  368. while(*p1==*p2)
  369. {
  370. if(*p1=='\0' || *p2=='\0')
  371. break;
  372. p1++;
  373. p2++;
  374. }
  375. if(*p1=='\0' && *p2=='\0')
  376. return(PASS);
  377. else
  378. return(FAIL);
  379. }
  380. int DiffTimeb(struct timeb ST, struct timeb ET)
  381. {
  382. //return milli-second
  383. unsigned int StartTime,StopTime;
  384. StartTime=(unsigned int)ST.time;
  385. StopTime=(unsigned int)ET.time;
  386. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  387. return (StopTime-StartTime);
  388. }
  389. int DiffTimebWithNowSec(struct timeb ST)
  390. {
  391. //return milli-second
  392. struct timeb ET;
  393. unsigned int StartTime,StopTime;
  394. ftime(&ET);
  395. StartTime=(unsigned int)ST.time;
  396. StopTime=(unsigned int)ET.time;
  397. return (StopTime-StartTime);
  398. }
  399. int CheckTimeOut(uint8_t *start)
  400. {
  401. int result = YES;
  402. struct ParsingResult
  403. {
  404. int result;
  405. int scanedElement;
  406. int year;
  407. int month;
  408. int mday;
  409. int hour;
  410. int min;
  411. int sec;
  412. int tz_hour;
  413. int tz_min;
  414. float minSec;
  415. }parsingResult;
  416. struct tm tmStart;
  417. struct timeb tbStart;
  418. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  419. if(strstr((char*)start, ".") != NULL)
  420. {
  421. // Original data with mini second
  422. if(strstr((char*)start, "Z") != NULL)
  423. {
  424. // Original data with Z
  425. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  426. &parsingResult.year,
  427. &parsingResult.month,
  428. &parsingResult.mday,
  429. &parsingResult.hour,
  430. &parsingResult.min,
  431. &parsingResult.sec,
  432. &parsingResult.minSec);
  433. }
  434. else
  435. {
  436. // Original data without Z
  437. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  438. &parsingResult.year,
  439. &parsingResult.month,
  440. &parsingResult.mday,
  441. &parsingResult.hour,
  442. &parsingResult.min,
  443. &parsingResult.sec,
  444. &parsingResult.minSec,
  445. &parsingResult.tz_hour,
  446. &parsingResult.tz_min);
  447. }
  448. }
  449. else
  450. {
  451. // Original data without mini second
  452. if(strstr((char*)start, "Z") != NULL)
  453. {
  454. // Original data with Z
  455. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  456. &parsingResult.year,
  457. &parsingResult.month,
  458. &parsingResult.mday,
  459. &parsingResult.hour,
  460. &parsingResult.min,
  461. &parsingResult.sec);
  462. }
  463. else
  464. {
  465. // Original data without Z
  466. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  467. &parsingResult.year,
  468. &parsingResult.month,
  469. &parsingResult.mday,
  470. &parsingResult.hour,
  471. &parsingResult.min,
  472. &parsingResult.sec,
  473. &parsingResult.tz_hour,
  474. &parsingResult.tz_min);
  475. }
  476. }
  477. if(parsingResult.scanedElement >= 6)
  478. {
  479. tmStart.tm_year = parsingResult.year - 1900;
  480. tmStart.tm_mon = parsingResult.month - 1;
  481. tmStart.tm_mday = parsingResult.mday;
  482. tmStart.tm_hour = parsingResult.hour;
  483. tmStart.tm_min = parsingResult.min;
  484. tmStart.tm_sec = parsingResult.sec;
  485. tmStart.tm_gmtoff = 0;
  486. tbStart.time = mktime(&tmStart);
  487. tbStart.timezone = 0;
  488. tbStart.millitm = 0;
  489. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  490. if(DiffTimebWithNowSec(tbStart) <= 0)
  491. result = NO;
  492. else
  493. result = YES;
  494. }
  495. else
  496. {
  497. PRINTF_FUNC("Start date parsing error.\n");
  498. }
  499. return result;
  500. }
  501. void SetChargerMode(byte gun_index, byte mode)
  502. {
  503. chargingInfo[gun_index]->SystemStatus = mode;
  504. }
  505. void PRINTF_FUNC(char *string, ...)
  506. {
  507. va_list args;
  508. char buffer[4096];
  509. va_start(args, string);
  510. vsnprintf(buffer, sizeof(buffer), string, args);
  511. va_end(args);
  512. DEBUG_INFO_MSG("%s ", buffer);
  513. }
  514. long long DiffTimebWithNow(struct timeb ST)
  515. {
  516. //return milli-second
  517. struct timeb ET;
  518. long long StartTime, StopTime;
  519. ftime(&ET);
  520. StartTime = (long long)ST.time;
  521. StopTime = (long long)ET.time;
  522. return ((StopTime - StartTime)*1000) + (ET.millitm - ST.millitm);
  523. }
  524. bool CheckWebConfigChargingTimeout(byte gunIndex)
  525. {
  526. bool result = false;
  527. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  528. {
  529. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  530. result = true;
  531. }
  532. // 如果後臺斷線的要多看 offline 設定
  533. if (!result && ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  534. {
  535. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  536. {
  537. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  538. result = true;
  539. }
  540. }
  541. return result;
  542. }
  543. bool CheckWebConfigChargingEnergy(byte gunIndex)
  544. {
  545. bool result = false;
  546. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  547. {
  548. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  549. result = true;
  550. }
  551. // 如果後臺斷線的要多看 offline 設定
  552. if (!result && ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  553. {
  554. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  555. {
  556. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  557. result = true;
  558. }
  559. }
  560. return result;
  561. }
  562. bool CheckWebConfigChargingSOC(byte gunIndex)
  563. {
  564. bool result = false;
  565. if (ShmSysConfigAndInfo->SysConfig.MaxChargingSoc > 0)
  566. {
  567. if (chargingInfo[gunIndex]->EvBatterySoc >= ShmSysConfigAndInfo->SysConfig.MaxChargingSoc)
  568. result = true;
  569. }
  570. return result;
  571. }
  572. bool CheckOcppChargingTimeout(byte gunIndex)
  573. {
  574. bool result = false;
  575. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  576. {
  577. if (ShmOCPP16Data->SessionTarget[gunIndex].targetDuration > 0)
  578. {
  579. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmOCPP16Data->SessionTarget[gunIndex].targetDuration * 60))
  580. {
  581. PRINTF_FUNC("CheckOcppChargingTimeout. G_(%d) - (%d) \n", gunIndex,
  582. chargingInfo[gunIndex]->PresentChargedDuration);
  583. result = true;
  584. }
  585. }
  586. }
  587. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  588. {
  589. if (ShmOCPP20Data->SessionTarget[gunIndex].targetDuration > 0)
  590. {
  591. if (chargingInfo [gunIndex]->PresentChargedDuration >= (ShmOCPP20Data->SessionTarget[gunIndex].targetDuration * 60))
  592. {
  593. PRINTF_FUNC ( "CheckOcppChargingTimeout. G_(%d) - (%d) \n", gunIndex,
  594. chargingInfo [gunIndex]->PresentChargedDuration );
  595. result = true;
  596. }
  597. }
  598. }
  599. return result;
  600. }
  601. bool CheckOcppChargingEnergy(byte gunIndex)
  602. {
  603. bool result = false;
  604. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  605. {
  606. if (ShmOCPP16Data->SessionTarget[gunIndex].targetEnergy > 0)
  607. {
  608. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmOCPP16Data->SessionTarget[gunIndex].targetEnergy)
  609. {
  610. PRINTF_FUNC ( "CheckOcppChargingEnergy. G_(%d) - (%f) \n", gunIndex,
  611. chargingInfo[gunIndex]->PresentChargedEnergy );
  612. result = true;
  613. }
  614. }
  615. }
  616. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  617. {
  618. if (ShmOCPP20Data->SessionTarget[gunIndex].targetEnergy > 0)
  619. {
  620. if (chargingInfo [gunIndex]->PresentChargedEnergy >= ShmOCPP20Data->SessionTarget[gunIndex].targetEnergy)
  621. {
  622. PRINTF_FUNC ( "CheckOcppChargingEnergy. G_(%d) - (%f) \n", gunIndex,
  623. chargingInfo [gunIndex]->PresentChargedEnergy );
  624. result = true;
  625. }
  626. }
  627. }
  628. return result;
  629. }
  630. bool CheckOcppChargingSOC(byte gunIndex)
  631. {
  632. bool result = false;
  633. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  634. {
  635. if (ShmOCPP16Data->SessionTarget[gunIndex].targetSoc > 0)
  636. {
  637. if (chargingInfo [gunIndex]->EvBatterySoc >= (ShmOCPP16Data->SessionTarget[gunIndex].targetSoc))
  638. {
  639. PRINTF_FUNC ( "CheckOcppChargingSOC. G_(%d) - (%d) \n", gunIndex,
  640. chargingInfo [gunIndex]->PresentChargedDuration );
  641. result = true;
  642. }
  643. }
  644. }
  645. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  646. {
  647. if (ShmOCPP20Data->SessionTarget[gunIndex].targetSoc > 0)
  648. {
  649. if (chargingInfo [gunIndex]->EvBatterySoc >= (ShmOCPP20Data->SessionTarget[gunIndex].targetSoc))
  650. {
  651. PRINTF_FUNC ( "CheckOcppChargingSOC. G_(%d) - (%d) \n", gunIndex,
  652. chargingInfo [gunIndex]->PresentChargedDuration );
  653. result = true;
  654. }
  655. }
  656. }
  657. return result;
  658. }
  659. bool IsGunUsing(byte dcGunIndex)
  660. {
  661. bool result = false;
  662. if((chargingInfo[dcGunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  663. chargingInfo[dcGunIndex]->SystemStatus <= SYS_MODE_ALARM) ||
  664. (chargingInfo[dcGunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  665. chargingInfo[dcGunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  666. {
  667. result = true;
  668. }
  669. return result;
  670. }
  671. //==========================================
  672. // Log
  673. //==========================================
  674. void InfoFwSlotStatusLog()
  675. {
  676. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  677. {
  678. if ((bd0_1_status == 0 && bd0_2_status == 1) || (bd1_1_status == 0 && bd1_2_status == 1))
  679. PRINTF_FUNC("Connector 1 : Chademo [Ver. = %s]", ShmCHAdeMOData->evse[chargingInfo[0]->type_index].version);
  680. else if ((bd0_1_status == 1 && bd0_2_status == 0) || (bd1_1_status == 1 && bd1_2_status == 0))
  681. PRINTF_FUNC("Connector 1 : CCS [Ver. = %s]", ShmCcsData->V2GMessage_DIN70121[chargingInfo[0]->type_index].version);
  682. else if ((bd0_1_status == 1 && bd0_2_status == 1) || (bd1_1_status == 1 && bd1_2_status == 1))
  683. PRINTF_FUNC("Connector 1 : GB [Ver. = %s]", ShmGBTData->evse[chargingInfo[0]->type_index].version);
  684. }
  685. else
  686. {
  687. if (bd0_1_status == 0 && bd0_2_status == 1)
  688. PRINTF_FUNC("Connector 1 : Chademo [Ver. = %s]", ShmCHAdeMOData->evse[chargingInfo[0]->type_index].version);
  689. else if (bd0_1_status == 1 && bd0_2_status == 0)
  690. PRINTF_FUNC("Connector 1 : CCS [Ver. = %s]", ShmCcsData->V2GMessage_DIN70121[chargingInfo[0]->type_index].version);
  691. else if (bd0_1_status == 1 && bd0_2_status == 1)
  692. PRINTF_FUNC("Connector 1 : GB [Ver. = %s]", ShmGBTData->evse[chargingInfo[0]->type_index].version);
  693. if (bd1_1_status == 0 && bd1_2_status == 1)
  694. PRINTF_FUNC("Connector 2 : Chademo [Ver. = %s]", ShmCHAdeMOData->evse[chargingInfo[1]->type_index].version);
  695. else if (bd1_1_status == 1 && bd1_2_status == 0)
  696. PRINTF_FUNC("Connector 2 : CCS [Ver. = %s]", ShmCcsData->V2GMessage_DIN70121[chargingInfo[1]->type_index].version);
  697. else if (bd1_1_status == 1 && bd1_2_status == 1)
  698. PRINTF_FUNC("Connector 2 : GB [Ver. = %s]", ShmGBTData->evse[chargingInfo[1]->type_index].version);
  699. }
  700. }
  701. void InfoSystemType()
  702. {
  703. if (ShmDcCommonData->systemType == _SYSTEM_TYPE_SIMPLE)
  704. PRINTF_FUNC("DW or DM Serial.");
  705. else if (ShmDcCommonData->systemType == _SYSTEM_TYPE_STANDARD)
  706. PRINTF_FUNC("DS Serial - Standard");
  707. else if (ShmDcCommonData->systemType == _SYSTEM_TYPE_SMART)
  708. PRINTF_FUNC("DS Serial - Dynamic");
  709. }
  710. void InfoLcmVersion()
  711. {
  712. PRINTF_FUNC("LCM Version = V.%03d ", ShmDcCommonData->LcmFwVersion);
  713. }
  714. void CheckHwSlotStatusLog(byte index)
  715. {
  716. if (chargingInfo[index]->Type == _Type_Chademo)
  717. {
  718. PRINTF_FUNC("Hw check : Connector %d, Type : Chademo, Evboard_id = %d \n",
  719. index, chargingInfo[index]->Evboard_id);
  720. }
  721. else if (chargingInfo[index]->Type == _Type_CCS)
  722. {
  723. PRINTF_FUNC("Hw check : Connector %d, Type : CCS, Evboard_id = %d \n",
  724. index, chargingInfo[index]->Evboard_id);
  725. }
  726. else if (chargingInfo[index]->Type == _Type_GB)
  727. {
  728. PRINTF_FUNC("Hw check : Connector %d, Type : GB, Evboard_id = %d \n",
  729. index, chargingInfo[index]->Evboard_id);
  730. }
  731. }
  732. //==========================================
  733. // Check interface status
  734. //==========================================
  735. int isInterfaceUp(const char *interface)
  736. {
  737. int result = FAIL;
  738. FILE *fp;
  739. char cmd[256];
  740. char buf[512];
  741. strcpy(cmd, "ifconfig");
  742. fp = popen(cmd, "r");
  743. if(fp != NULL)
  744. {
  745. while(fgets(buf, sizeof(buf), fp) != NULL)
  746. {
  747. if(strstr(buf, interface) > 0)
  748. {
  749. result = PASS;
  750. }
  751. }
  752. }
  753. pclose(fp);
  754. return result;
  755. }
  756. //=================================
  757. // Create all share memory
  758. //=================================
  759. int CreateShareMemory()
  760. {
  761. int MeterSMId;
  762. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  763. {
  764. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  765. return 0;
  766. }
  767. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  768. {
  769. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  770. return 0;
  771. }
  772. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  773. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  774. {
  775. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  776. return 0;
  777. }
  778. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  779. {
  780. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  781. return 0;
  782. }
  783. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  784. //creat ShmPsuData
  785. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  786. {
  787. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  788. return 0;
  789. }
  790. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  791. {
  792. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  793. return 0;
  794. }
  795. memset(ShmPsuData, 0, sizeof(struct PsuData));
  796. if(CHAdeMO_QUANTITY > 0)
  797. {
  798. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  799. {
  800. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  801. return 0;
  802. }
  803. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  804. {
  805. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  806. return 0;
  807. }
  808. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  809. }
  810. if(GB_QUANTITY > 0)
  811. {
  812. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  813. {
  814. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  815. return 0;
  816. }
  817. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  818. {
  819. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  820. return 0;
  821. }
  822. memset(ShmGBTData, 0, sizeof(struct GBTData));
  823. }
  824. //creat ShmCcsData
  825. if(CCS_QUANTITY > 0)
  826. {
  827. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  828. {
  829. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  830. return 0;
  831. }
  832. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  833. {
  834. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  835. return 0;
  836. }
  837. memset(ShmCcsData, 0, sizeof(struct CcsData));
  838. }
  839. //creat ShmPrimaryMcuData
  840. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  841. {
  842. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  843. return 0;
  844. }
  845. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  846. {
  847. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  848. return 0;
  849. }
  850. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  851. //creat ShmFanModuleData
  852. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  853. {
  854. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  855. return 0;
  856. }
  857. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  858. {
  859. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  860. return 0;
  861. }
  862. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  863. //creat ShmRelayModuleData
  864. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  865. {
  866. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  867. return 0;
  868. }
  869. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  870. {
  871. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  872. return 0;
  873. }
  874. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  875. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  876. {
  877. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmLedModuleData NG \n");
  878. return 0;
  879. }
  880. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  881. {
  882. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmLedModuleData NG \n");
  883. return 0;
  884. }
  885. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  886. //creat ShmOCPP16Data
  887. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  888. {
  889. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  890. return 0;
  891. }
  892. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  893. {
  894. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  895. return 0;
  896. }
  897. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  898. if ((MeterSMId = shmget ( ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777 )) < 0)
  899. {
  900. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmOCPP16DataPH NG \n" );
  901. return 0;
  902. }
  903. else if ((ShmOCPP16DataPH = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  904. {
  905. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmOCPP16DataPH NG \n" );
  906. return 0;
  907. }
  908. memset ( ShmOCPP16DataPH , 0, sizeof(struct OCPP16Data) );
  909. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  910. {
  911. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget OCPP20Data NG\n");
  912. return 0;
  913. }
  914. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  915. {
  916. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat OCPP20Data NG\n");
  917. return 0;
  918. }
  919. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  920. if ((MeterSMId = shmget ( ShmSmartBoxKey, sizeof(struct SmartBoxData), IPC_CREAT | 0777 )) < 0)
  921. {
  922. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmget ShmSmartBoxKey NG \n" );
  923. return 0;
  924. }
  925. else if ((ShmSmartBoxData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  926. {
  927. DEBUG_ERROR_MSG( "[main]CreatShareMemory:shmat ShmSmartBoxData NG \n" );
  928. return 0;
  929. }
  930. memset ( ShmSmartBoxData, 0, sizeof(struct SmartBoxData) );
  931. if ((MeterSMId = shmget(ShmCsuMeterKey, sizeof(struct MeterInformation), IPC_CREAT | 0777)) < 0)
  932. {
  933. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCsuMeterKey NG \n");
  934. return 0;
  935. }
  936. else if ((ShmCsuMeterData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  937. {
  938. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCsuMeterData NG \n");
  939. return 0;
  940. }
  941. memset(ShmCsuMeterData, 0, sizeof(struct MeterInformation));
  942. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  943. {
  944. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCommonKey NG \n");
  945. return 0;
  946. }
  947. else if ((ShmDcCommonData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  948. {
  949. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCommonKey NG \n");
  950. return 0;
  951. }
  952. byte buf = 0;
  953. if (ShmDcCommonData->rebootCount == 1)
  954. buf = ShmDcCommonData->rebootCount;
  955. memset(ShmDcCommonData, 0, sizeof(struct DcCommonInformation));
  956. ShmDcCommonData->rebootCount = buf;
  957. return 1;
  958. }
  959. void SetupRebootCount(byte value)
  960. {
  961. int MeterSMId;
  962. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  963. {
  964. DEBUG_ERROR_MSG("[main]RecordRebootCount:shmget ShmCommonKey NG \n");
  965. }
  966. else
  967. {
  968. ShmDcCommonData->rebootCount = value;
  969. shmctl(MeterSMId, IPC_SET, 0);
  970. }
  971. }
  972. //=================================
  973. // LCM Page
  974. //=================================
  975. void ChangeLcmByIndex(byte page_index)
  976. {
  977. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL ||
  978. page_index == _LCM_COMPLETE || page_index == _LCM_FIX || page_index == _LCM_EMC)
  979. {
  980. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0 &&
  981. (page_index == _LCM_FIX || page_index == _LCM_EMC))
  982. {
  983. bool isUpdate = false;
  984. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  985. {
  986. if (chargingInfo[_index]->SystemStatus == SYS_MODE_UPDATE)
  987. {
  988. isUpdate = true;
  989. break;
  990. }
  991. }
  992. if (!isUpdate &&
  993. ShmDcCommonData->LcmFwVersion > 0 &&
  994. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  995. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_EMC;
  996. else
  997. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_FIX;
  998. }
  999. else
  1000. {
  1001. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  1002. }
  1003. }
  1004. }
  1005. //======================================================
  1006. // Peripheral initial
  1007. //======================================================
  1008. void InitGPIO()
  1009. {
  1010. /*****************0~3, 4 bank, bank x 32+ num*********************/
  1011. /***************************************************************/
  1012. /*************** GPIO 0 ***************************************/
  1013. /***************************************************************/
  1014. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  1015. system("echo 22 > /sys/class/gpio/export");
  1016. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  1017. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  1018. system("echo 23 > /sys/class/gpio/export");
  1019. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  1020. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  1021. system("echo 26 > /sys/class/gpio/export");
  1022. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  1023. system("echo 1 > /sys/class/gpio/gpio26/value");
  1024. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  1025. system("echo 27 > /sys/class/gpio/export");
  1026. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  1027. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  1028. system("echo 29 > /sys/class/gpio/export");
  1029. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  1030. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  1031. system("echo 19 > /sys/class/gpio/export");
  1032. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  1033. system("echo 1 > /sys/class/gpio/gpio19/value");
  1034. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  1035. system("echo 20 > /sys/class/gpio/export");
  1036. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  1037. /***************************************************************/
  1038. /*************** GPIO 1 ***************************************/
  1039. /***************************************************************/
  1040. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  1041. system("echo 44 > /sys/class/gpio/export");
  1042. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  1043. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  1044. system("echo 45 > /sys/class/gpio/export");
  1045. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  1046. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  1047. system("echo 46 > /sys/class/gpio/export");
  1048. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  1049. system("echo 0 > /sys/class/gpio/gpio46/value");
  1050. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  1051. system("echo 47 > /sys/class/gpio/export");
  1052. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  1053. /***************************************************************/
  1054. /*************** GPIO 2 ***************************************/
  1055. /***************************************************************/
  1056. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  1057. system("echo 89 > /sys/class/gpio/export");
  1058. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  1059. system("echo 1 > /sys/class/gpio/gpio89/value");
  1060. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  1061. system("echo 87 > /sys/class/gpio/export");
  1062. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  1063. system("echo 0 > /sys/class/gpio/gpio87/value");
  1064. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  1065. system("echo 88 > /sys/class/gpio/export");
  1066. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  1067. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  1068. system("echo 86 > /sys/class/gpio/export");
  1069. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  1070. /***************************************************************/
  1071. /*************** GPIO 3 ***************************************/
  1072. /***************************************************************/
  1073. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  1074. system("echo 111 > /sys/class/gpio/export");
  1075. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  1076. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  1077. system("echo 114 > /sys/class/gpio/export");
  1078. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  1079. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  1080. system("echo 113 > /sys/class/gpio/export");
  1081. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  1082. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  1083. system("echo 110 > /sys/class/gpio/export");
  1084. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  1085. system("echo 0 > /sys/class/gpio/gpio110/value");
  1086. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  1087. system("echo 115 > /sys/class/gpio/export");
  1088. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  1089. system("echo 0 > /sys/class/gpio/gpio115/value");
  1090. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  1091. system("echo 112 > /sys/class/gpio/export");
  1092. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  1093. system("echo 1 > /sys/class/gpio/gpio112/value");
  1094. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  1095. system("echo 116 > /sys/class/gpio/export");
  1096. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  1097. system("echo 0 > /sys/class/gpio/gpio116/value");
  1098. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  1099. system("echo 103 > /sys/class/gpio/export");
  1100. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  1101. system("echo 0 > /sys/class/gpio/gpio103/value");
  1102. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  1103. system("echo 104 > /sys/class/gpio/export");
  1104. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  1105. system("echo 0 > /sys/class/gpio/gpio104/value");
  1106. #ifdef SystemLogMessage
  1107. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK \n");
  1108. #endif
  1109. }
  1110. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1111. {
  1112. int fd,wrd;
  1113. unsigned char *buf;
  1114. unsigned int ChkSum,ChkSumOrg;
  1115. if((buf=malloc(MtdBlockSize))==NULL)
  1116. {
  1117. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  1118. if(ShmStatusCodeData!=NULL)
  1119. {
  1120. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1121. }
  1122. sleep(5);
  1123. system("reboot -f");
  1124. sleep(5);
  1125. system("reboot -f");
  1126. }
  1127. memset(buf, 0, MtdBlockSize);
  1128. //================================================
  1129. // Load configuration from mtdblock10
  1130. //================================================
  1131. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1132. //fd = open("/dev/mtdblock10", O_RDWR);
  1133. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1134. if (fd < 0)
  1135. {
  1136. free(buf);
  1137. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  1138. if(ShmStatusCodeData!=NULL)
  1139. {
  1140. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1141. }
  1142. sleep(5);
  1143. system("reboot -f");
  1144. sleep(5);
  1145. system("reboot -f");
  1146. }
  1147. wrd=read(fd, buf, MtdBlockSize);
  1148. close(fd);
  1149. if(wrd<MtdBlockSize)
  1150. {
  1151. free(buf);
  1152. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  1153. if(ShmStatusCodeData!=NULL)
  1154. {
  1155. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1156. }
  1157. sleep(5);
  1158. system("reboot -f");
  1159. sleep(5);
  1160. system("reboot -f");
  1161. }
  1162. ChkSum=0;
  1163. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1164. // {
  1165. // ChkSum+=buf[wrd];
  1166. // }
  1167. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1168. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1169. {
  1170. ChkSum+=buf[wrd];
  1171. }
  1172. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1173. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1174. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1175. //================================================
  1176. // Load configuration from mtdblock11
  1177. //================================================
  1178. if(ChkSum!=ChkSumOrg)
  1179. {
  1180. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  1181. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1182. //fd = open("/dev/mtdblock11", O_RDWR);
  1183. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1184. if (fd < 0)
  1185. {
  1186. free(buf);
  1187. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  1188. if(ShmStatusCodeData!=NULL)
  1189. {
  1190. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1191. }
  1192. sleep(5);
  1193. system("reboot -f");
  1194. sleep(5);
  1195. system("reboot -f");
  1196. }
  1197. memset(buf, 0, MtdBlockSize);
  1198. wrd=read(fd, buf,MtdBlockSize);
  1199. close(fd);
  1200. if(wrd<MtdBlockSize)
  1201. {
  1202. free(buf);
  1203. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  1204. if(ShmStatusCodeData!=NULL)
  1205. {
  1206. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1207. }
  1208. sleep(5);
  1209. system("reboot -f");
  1210. sleep(5);
  1211. system("reboot -f");
  1212. }
  1213. ChkSum=0;
  1214. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1215. // {
  1216. // ChkSum+=buf[wrd];
  1217. // }
  1218. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1219. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1220. {
  1221. ChkSum+=buf[wrd];
  1222. }
  1223. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1224. //================================================
  1225. // Load configuration from mtdblock12 (Factory default)
  1226. //================================================
  1227. if(ChkSum!=ChkSumOrg)
  1228. {
  1229. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  1230. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1231. //fd = open("/dev/mtdblock12", O_RDWR);
  1232. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1233. if (fd < 0)
  1234. {
  1235. free(buf);
  1236. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  1237. if(ShmStatusCodeData!=NULL)
  1238. {
  1239. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1240. }
  1241. sleep(5);
  1242. system("reboot -f");
  1243. sleep(5);
  1244. system("reboot -f");
  1245. }
  1246. memset(buf, 0, MtdBlockSize);
  1247. wrd=read(fd, buf,MtdBlockSize);
  1248. close(fd);
  1249. if(wrd<MtdBlockSize)
  1250. {
  1251. free(buf);
  1252. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  1253. if(ShmStatusCodeData!=NULL)
  1254. {
  1255. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1256. }
  1257. sleep(5);
  1258. system("reboot -f");
  1259. sleep(5);
  1260. system("reboot -f");
  1261. }
  1262. ChkSum=0;
  1263. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1264. // {
  1265. // ChkSum+=buf[wrd];
  1266. // }
  1267. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1268. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1269. {
  1270. ChkSum+=buf[wrd];
  1271. }
  1272. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1273. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1274. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1275. if(ChkSum!=ChkSumOrg)
  1276. {
  1277. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  1278. free(buf);
  1279. //system("cd /root;./FactoryConfig -m");
  1280. sleep(5);
  1281. char facBuf[256];
  1282. sprintf(facBuf, "cd /root;./FactoryConfig -m %s %s",
  1283. ShmSysConfigAndInfo->SysConfig.ModelName,
  1284. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  1285. system(facBuf);
  1286. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1287. system("sync");
  1288. sleep(5);
  1289. system("reboot -f");
  1290. sleep(5);
  1291. system("reboot -f");
  1292. return FAIL;
  1293. }
  1294. }
  1295. }
  1296. //load OK
  1297. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1298. free(buf);
  1299. system("rm -f /mnt/EvseConfig.bin");
  1300. // SysConfig in flash is empty (0xffffffff)
  1301. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1302. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1303. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1304. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1305. {
  1306. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1307. {
  1308. PRINTF_FUNC("Model name over length.\n");
  1309. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1310. }
  1311. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1312. {
  1313. PRINTF_FUNC("Model serial number over length.\n");
  1314. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1315. }
  1316. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1317. {
  1318. PRINTF_FUNC("System id over length.\n");
  1319. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1320. }
  1321. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1322. {
  1323. PRINTF_FUNC("Ethernet dhcp config is null.\n");
  1324. }
  1325. system("cd /root;./Module_FactoryConfig -m");
  1326. sleep(3);
  1327. system("/usr/bin/run_evse_restart.sh");
  1328. }
  1329. PRINTF_FUNC("*********************************************************** \n");
  1330. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  1331. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1332. {
  1333. memcpy((char*)ShmOCPP16Data->ChargeBoxId,
  1334. (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1335. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1336. }
  1337. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1338. {
  1339. memcpy((char*)ShmOCPP20Data->ChargeBoxId,
  1340. (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1341. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1342. }
  1343. return PASS;
  1344. }
  1345. int isRouteFail()
  1346. {
  1347. int result = YES;
  1348. FILE *fp;
  1349. char buf[512];
  1350. fp = popen("route -n", "r");
  1351. if(fp != NULL)
  1352. {
  1353. while(fgets(buf, sizeof(buf), fp) != NULL)
  1354. {
  1355. if(strstr(buf, "eth0") != NULL)
  1356. result = NO;
  1357. }
  1358. }
  1359. pclose(fp);
  1360. return result;
  1361. }
  1362. int isReachableInternet()
  1363. {
  1364. int result = FAIL;
  1365. FILE *fp;
  1366. char cmd[256];
  1367. char buf[512];
  1368. char tmp[512];
  1369. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1370. // {
  1371. // result = PASS;
  1372. // }
  1373. // else
  1374. {
  1375. strcpy(cmd, "ifconfig eth0");
  1376. fp = popen(cmd, "r");
  1377. if (fp != NULL)
  1378. {
  1379. while(fgets(buf, sizeof(buf), fp) != NULL)
  1380. {
  1381. if (strstr(buf, "inet addr:") > 0)
  1382. {
  1383. sscanf(buf, "%*s%s", tmp);
  1384. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1385. tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  1386. sscanf(buf, "%*s%*s%*s%s", tmp);
  1387. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1388. tmp, strspn(tmp, "Mask:"), strlen(tmp)-strspn(tmp, "Mask:"));
  1389. }
  1390. }
  1391. }
  1392. pclose(fp);
  1393. memset(buf, 0x00, sizeof(buf));
  1394. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1395. {
  1396. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1397. fp = popen(cmd, "r");
  1398. if(fp != NULL)
  1399. {
  1400. while(fgets(buf, sizeof(buf), fp) != NULL)
  1401. {
  1402. if(strstr(buf, "transmitted") > 0)
  1403. {
  1404. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1405. if(strstr(buf,"100%") != NULL)
  1406. {
  1407. }
  1408. else
  1409. {
  1410. result = PASS;
  1411. }
  1412. //DEBUG_INFO("%s",buf);
  1413. //DEBUG_INFO("%s\n",tmp);
  1414. }
  1415. }
  1416. }
  1417. pclose(fp);
  1418. }
  1419. }
  1420. return result;
  1421. }
  1422. void InitEthernet()
  1423. {
  1424. system("ifconfig eth0 down");// eth0 down
  1425. system("ifconfig eth1 down");// eth1 down
  1426. sleep(2);
  1427. char tmpbuf[256];
  1428. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1429. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1430. sleep(2);
  1431. //Init Eth0 for internet
  1432. memset(tmpbuf,0,256);
  1433. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1434. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1435. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1436. system(tmpbuf);
  1437. memset(tmpbuf,0,256);
  1438. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1439. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1440. system(tmpbuf);
  1441. //system("ifconfig lo up");
  1442. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1443. //Init Eth1 for administrator tool
  1444. memset(tmpbuf,0,256);
  1445. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  1446. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  1447. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  1448. system(tmpbuf);
  1449. //Run DHCP client if enabled
  1450. system("killall udhcpc");
  1451. system("rm -rf /etc/resolv.conf");
  1452. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1453. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1454. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1455. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1456. {
  1457. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1458. system(tmpbuf);
  1459. }
  1460. //Upgrade system id to /etc/hostname
  1461. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1462. system(tmpbuf);
  1463. pid_t pid = fork();
  1464. uint8_t cnt_pingDNS_Fail;
  1465. if(pid == 0)
  1466. {
  1467. for(;;)
  1468. {
  1469. if (isRouteFail())
  1470. {
  1471. //PRINTF_FUNC("eth0 not in route, restart eht0. \n");
  1472. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1473. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1474. {
  1475. InitialDHCP();
  1476. }
  1477. else
  1478. {
  1479. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1480. memset(tmpbuf,0,256);
  1481. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1482. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1483. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1484. system(tmpbuf);
  1485. memset(tmpbuf,0,256);
  1486. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1487. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1488. system(tmpbuf);
  1489. }
  1490. }
  1491. if(isReachableInternet() == PASS)
  1492. {
  1493. //ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1494. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1495. cnt_pingDNS_Fail = 0;
  1496. }
  1497. else
  1498. {
  1499. if(++cnt_pingDNS_Fail > 3)
  1500. {
  1501. //ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1502. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1503. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1504. {
  1505. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1506. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1507. system(tmpbuf);
  1508. }
  1509. else
  1510. {
  1511. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1512. memset(tmpbuf,0,256);
  1513. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1514. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1515. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1516. system(tmpbuf);
  1517. memset(tmpbuf,0,256);
  1518. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1519. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1520. system(tmpbuf);
  1521. }
  1522. cnt_pingDNS_Fail = 0;
  1523. }
  1524. }
  1525. bool ethResult = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet;
  1526. ShmSysConfigAndInfo->SysInfo.ethInternetConn = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet;
  1527. if(ethResult == YES)
  1528. {
  1529. system("/sbin/ifmetric eth0 0");
  1530. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') ||
  1531. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1532. system("/sbin/ifmetric mlan0 1");
  1533. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') ||
  1534. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1535. system("/sbin/ifmetric ppp0 2");
  1536. }
  1537. if (!ethResult &&
  1538. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1539. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1540. {
  1541. //ethResult = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn;
  1542. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1543. if (ethResult)
  1544. {
  1545. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1546. {
  1547. system("/sbin/ifmetric eth0 1");
  1548. system("/sbin/ifmetric mlan0 0");
  1549. }
  1550. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1551. system("/sbin/ifmetric ppp0 2");
  1552. }
  1553. }
  1554. if (!ethResult &&
  1555. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1556. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1557. {
  1558. //ethResult = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn;
  1559. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == YES ? NO : YES;
  1560. if (ethResult)
  1561. {
  1562. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1563. system("/sbin/ifmetric mlan0 2");
  1564. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1565. {
  1566. system("/sbin/ifmetric eth0 1");
  1567. system("/sbin/ifmetric ppp0 0");
  1568. }
  1569. }
  1570. }
  1571. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1572. sleep(5);
  1573. }
  1574. }
  1575. #ifdef SystemLogMessage
  1576. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK. \n");
  1577. #endif
  1578. }
  1579. int InitialRfidPort()
  1580. {
  1581. int uartO2 = open(rfidPortName, O_RDWR);
  1582. struct termios tios;
  1583. if (uartO2 != FAIL)
  1584. {
  1585. ioctl (uartO2, TCGETS, &tios);
  1586. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1587. tios.c_lflag = 0;
  1588. tios.c_iflag = 0;
  1589. tios.c_oflag = 0;
  1590. tios.c_cc[VMIN] = 0;
  1591. tios.c_cc[VTIME] = (unsigned char) 1;
  1592. tios.c_lflag = 0;
  1593. tcflush(uartO2, TCIFLUSH);
  1594. ioctl(uartO2, TCSETS, &tios);
  1595. }
  1596. if (uartO2 < 0)
  1597. {
  1598. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1599. }
  1600. return uartO2;
  1601. }
  1602. void GetMacAddress()
  1603. {
  1604. for (byte index = 0; index < 2; index++)
  1605. {
  1606. int fd;
  1607. struct ifreq ifr;
  1608. char tarEth[5];
  1609. char Mac[18];
  1610. sprintf(tarEth,"eth%d",index);
  1611. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1612. ifr.ifr_addr.sa_family = AF_INET;
  1613. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1614. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1615. close(fd);
  1616. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1617. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1618. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1619. if (index == 0)
  1620. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1621. else
  1622. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1623. }
  1624. }
  1625. void GetFirmwareVersion()
  1626. {
  1627. // Get CSU root file system version
  1628. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1629. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1630. for(uint8_t idx=0;idx<3;idx++)
  1631. {
  1632. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J' ||
  1633. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'K' ||
  1634. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'L' ||
  1635. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'S')
  1636. {
  1637. chademo++;
  1638. count++;
  1639. }
  1640. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1641. {
  1642. gb++;
  1643. count++;
  1644. }
  1645. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'U' ||
  1646. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'E' ||
  1647. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'T' ||
  1648. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'D' ||
  1649. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'M' ||
  1650. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'N' ||
  1651. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'R' ||
  1652. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'V' ||
  1653. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'F' ||
  1654. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'Y' ||
  1655. ShmSysConfigAndInfo->SysConfig.ModelName[7 + idx] == 'Z' )
  1656. {
  1657. ccs++;
  1658. count++;
  1659. }
  1660. }
  1661. if (count == 1)
  1662. {
  1663. if (chademo > 0)
  1664. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1665. else if (ccs > 0)
  1666. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1667. else if (gb > 0)
  1668. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1669. }
  1670. else
  1671. {
  1672. if (chademo == 1 && ccs == 1)
  1673. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1674. else if (chademo == 1 && gb == 1)
  1675. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1676. else if (ccs == 1 && gb == 1)
  1677. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1678. else if (chademo == 2)
  1679. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '7';
  1680. else if (ccs == 2)
  1681. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '8';
  1682. else if (gb == 2)
  1683. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '9';
  1684. }
  1685. // Get network option from model name
  1686. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1687. {
  1688. case 'B':
  1689. case 'U':
  1690. //Blue tooth
  1691. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1692. break;
  1693. case 'W':
  1694. // WIFI
  1695. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1696. break;
  1697. case 'T':
  1698. // 3G/4G
  1699. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1700. break;
  1701. case 'D':
  1702. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1703. break;
  1704. default:
  1705. // LAN
  1706. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1707. break;
  1708. }
  1709. // Get rating power from model name
  1710. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1711. // Get IEC or UL
  1712. char _buf[3] = {0};
  1713. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[3], 1);
  1714. if (strcmp(_buf, "E") == EQUAL || strcmp(_buf, "G") == EQUAL || strcmp(_buf, "C") == EQUAL ||
  1715. strcmp(_buf, "T") == EQUAL || strcmp(_buf, "K") == EQUAL || strcmp(_buf, "Z") == EQUAL ||
  1716. strcmp(_buf, "M") == EQUAL || strcmp(_buf, "P") == EQUAL || strcmp(_buf, "I") == EQUAL ||
  1717. strcmp(_buf, "F") == EQUAL)
  1718. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1719. else if (strcmp(_buf, "U") == EQUAL || strcmp(_buf, "B") == EQUAL || strcmp(_buf, "L") == EQUAL)
  1720. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1721. else if (strcmp(_buf, "J") == EQUAL)//LWADD
  1722. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_JARI;
  1723. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
  1724. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
  1725. if (strcmp(_buf, "PN") == EQUAL ||
  1726. strcmp(_buf, "N7") == EQUAL ||
  1727. strcmp(_buf, "NX") == EQUAL ||
  1728. strcmp(_buf, "OL") == EQUAL ||
  1729. strcmp(_buf, "DE") == EQUAL)
  1730. ShmDcCommonData->ShowLogoFlag = NO;
  1731. else
  1732. ShmDcCommonData->ShowLogoFlag = YES;
  1733. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'G')
  1734. ShmDcCommonData->chargerType = CHARGER_TYPE_SIMPLE;
  1735. else
  1736. ShmDcCommonData->chargerType = CHARGER_TYPE_STANDARD;
  1737. }
  1738. void InitialGunIndexToUnUse()
  1739. {
  1740. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  1741. {
  1742. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index = NO_DEFINE;
  1743. }
  1744. for (byte index = 0; index < CCS_QUANTITY; index++)
  1745. {
  1746. ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index = NO_DEFINE;
  1747. }
  1748. for (byte index = 0; index < GB_QUANTITY; index++)
  1749. {
  1750. ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index = NO_DEFINE;
  1751. }
  1752. for (byte index = 0; index < AC_QUANTITY; index++)
  1753. {
  1754. ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index = NO_DEFINE;
  1755. }
  1756. }
  1757. void InitialShareMemoryInfo()
  1758. {
  1759. FILE *fp;
  1760. char cmd[512];
  1761. char buf[512];
  1762. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1763. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1764. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1765. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1766. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1767. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1768. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1769. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1770. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1771. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1772. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1773. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1774. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1775. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1776. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1777. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1778. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1779. sprintf(cmd, "/bin/uname -r");
  1780. fp = popen(cmd, "r");
  1781. if(fp == NULL)
  1782. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1783. else
  1784. {
  1785. while(fgets(buf, sizeof(buf), fp) != NULL)
  1786. {
  1787. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1788. }
  1789. }
  1790. GetFirmwareVersion();
  1791. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1792. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1793. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1794. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1795. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1796. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1797. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1798. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1799. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1800. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1801. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1802. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1803. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1804. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1805. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1806. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1807. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1808. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1809. ShmSysConfigAndInfo->SysInfo.InternetConn = NO;
  1810. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = NO;
  1811. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1812. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1813. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1814. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1815. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1816. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = NO;
  1817. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1818. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1819. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1820. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1821. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail = NO;
  1822. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1823. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1824. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1825. ShmRelayModuleData->SelfTest_Comp = NO;
  1826. ShmFanModuleData->SelfTest_Comp = NO;
  1827. ShmLedModuleData->SelfTest_Comp = NO;
  1828. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_NORMAL;
  1829. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1830. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  1831. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1832. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOTTING;
  1833. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  1834. ShmFanModuleData->TestFanSpeed = 0;
  1835. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1836. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1837. InitialGunIndexToUnUse();
  1838. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1839. ShmDcCommonData->systemType = _SYSTEM_TYPE_SIMPLE;
  1840. ShmDcCommonData->CcsVersion = _CCS_VERSION_CHECK_TAG_V015S0;
  1841. ShmDcCommonData->psuKeepCommunication = NO;
  1842. ShmDcCommonData->acContactSwitch = NO;
  1843. ShmDcCommonData->ConnectErrList[0].GunErrMessage = 0;
  1844. ShmDcCommonData->ConnectErrList[1].GunErrMessage = 0;
  1845. ShmDcCommonData->LcmFwVersion = 0;
  1846. ShmDcCommonData->minOutputCur = 0;
  1847. ShmDcCommonData->evBoardResetFlag = NO;
  1848. ShmDcCommonData->enObtainAuthorizbyCCID = NO;
  1849. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  1850. ShmDcCommonData->_psuProtocol = _PSU_PROTOCOL_INFY;
  1851. ShmDcCommonData->_offlinePower = 0;
  1852. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE; //LW_tpc plug detect
  1853. ShmDcCommonData->TpcDetectFunction = NO; //LW_tpc plug detect
  1854. ShmDcCommonData->_isPsuErrorOccur = NO;
  1855. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1856. }
  1857. int Initialization()
  1858. {
  1859. // 初始化卡號驗證的 Flag
  1860. ClearAuthorizedFlag();
  1861. // 初始化插槍驗證的 Flag
  1862. ClearDetectPluginFlag();
  1863. // UART 2 for Rfid
  1864. rfidFd = InitialRfidPort();
  1865. int pinOut[2] = { 116, 115 }; //D13(Cha)、C13(CCS)
  1866. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1867. {
  1868. chargingInfo[count]->RemoteStartFlag = NO;
  1869. chargingInfo[count]->ReservedStartFlag = NO;
  1870. if (chargingInfo[count]->Type == _Type_Chademo)
  1871. {
  1872. gpio_set_value(pinOut[count], 0x00);
  1873. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1874. }
  1875. else if (chargingInfo[count]->Type == _Type_GB)
  1876. {
  1877. gpio_set_value(pinOut[count], 0x00);
  1878. ShmGBTData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1879. }
  1880. else if (chargingInfo[count]->Type == _Type_CCS)
  1881. {
  1882. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1883. gpio_set_value(pinOut[1], 0x00);
  1884. else
  1885. gpio_set_value(pinOut[count], 0x00);
  1886. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1887. }
  1888. strcpy((char *)ShmDcCommonData->_reserved_UserId[count], "");
  1889. }
  1890. byte acDirIndex = 0;
  1891. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1892. {
  1893. if (count == 1)
  1894. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  1895. ocpp_set_noErrorCode_cmd(count + acDirIndex, "NoError");
  1896. }
  1897. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  1898. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  1899. {
  1900. ac_chargingInfo[count]->RemoteStartFlag = NO;
  1901. if (ac_chargingInfo[count]->Type == _Type_AC)
  1902. {
  1903. ac_chargingInfo[count]->SelfTest_Comp = NO;
  1904. ocpp_set_noErrorCode_cmd(count + hasDc, "NoError");
  1905. }
  1906. }
  1907. PRINTF_FUNC("Information check done \n");
  1908. return PASS;
  1909. }
  1910. bool InitialSystemDefaultConfig()
  1911. {
  1912. bool result = true;
  1913. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1914. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  1915. InitGPIO();
  1916. InitEthernet();
  1917. GetMacAddress();
  1918. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1919. // sleep(3);
  1920. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1921. // sleep(1);
  1922. // system("/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up");
  1923. return result;
  1924. }
  1925. bool DisplaySelfTestFailReason()
  1926. {
  1927. bool result = false;
  1928. // RB、FB、407、EV 小板中有些板子無回應
  1929. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1930. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; result = true; }
  1931. if (ShmFanModuleData->SelfTest_Comp == NO)
  1932. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; result = true; }
  1933. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1934. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; result = true; }
  1935. // if (ShmLedModuleData->SelfTest_Comp == NO)
  1936. // { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES; }
  1937. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES)
  1938. { result = true; }
  1939. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1940. {
  1941. if (chargingInfo[index]->Type == _Type_Chademo)
  1942. {
  1943. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1944. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; result = true; }
  1945. }
  1946. else if (chargingInfo[index]->Type == _Type_GB)
  1947. {
  1948. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1949. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; result = true; }
  1950. }
  1951. else if (chargingInfo[index]->Type == _Type_CCS)
  1952. {
  1953. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1954. {
  1955. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1956. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; result = true; }
  1957. }
  1958. }
  1959. }
  1960. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1961. {
  1962. // 先借 GBT 顯示
  1963. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  1964. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; result = true; }
  1965. }
  1966. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1967. {
  1968. // AC Contact 未搭上
  1969. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1970. result = true;
  1971. }
  1972. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  1973. {
  1974. result = true;
  1975. }
  1976. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  1977. {
  1978. // PSU 通訊問題
  1979. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1980. result = true;
  1981. }
  1982. return result;
  1983. }
  1984. void SelfTestRun()
  1985. {
  1986. bool evInitFlag = false;
  1987. StartSystemTimeoutDet(Timeout_SelftestChk);
  1988. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1989. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  1990. GetTimeoutValue(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) <= 20)
  1991. {
  1992. ReviewCriticalAlarm();
  1993. ChkPrimaryStatus();
  1994. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL ||
  1995. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  1996. {
  1997. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1998. return;
  1999. }
  2000. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  2001. {
  2002. if (ShmPsuData->Work_Step == _NO_WORKING ||
  2003. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  2004. {
  2005. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2006. return;
  2007. }
  2008. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  2009. {
  2010. case _STEST_VERSION:
  2011. {
  2012. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
  2013. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  2014. {
  2015. //PRINTF_FUNC("RB pass \n");
  2016. ShmRelayModuleData->SelfTest_Comp = YES;
  2017. }
  2018. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  2019. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  2020. {
  2021. //PRINTF_FUNC("Fan pass \n");
  2022. ShmFanModuleData->SelfTest_Comp = YES;
  2023. }
  2024. if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
  2025. ShmPrimaryMcuData->version[0] != '\0')
  2026. {
  2027. //PRINTF_FUNC("407 pass \n");
  2028. ShmPrimaryMcuData->SelfTest_Comp = YES;
  2029. }
  2030. // EV 小板
  2031. if (!evInitFlag)
  2032. {
  2033. evInitFlag = YES;
  2034. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2035. {
  2036. if (chargingInfo[index]->Type == _Type_Chademo)
  2037. {
  2038. if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
  2039. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  2040. {
  2041. //PRINTF_FUNC("chademo pass \n");
  2042. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  2043. }
  2044. else
  2045. {
  2046. //PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  2047. evInitFlag = NO;
  2048. }
  2049. }
  2050. else if (chargingInfo[index]->Type == _Type_GB)
  2051. {
  2052. if (strlen((char *)ShmGBTData->evse[chargingInfo[index]->type_index].version) != 0 ||
  2053. ShmGBTData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  2054. {
  2055. //PRINTF_FUNC("GBT pass \n");
  2056. ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  2057. }
  2058. else
  2059. {
  2060. //PRINTF_FUNC("GBT fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  2061. evInitFlag = NO;
  2062. }
  2063. }
  2064. else if (chargingInfo[index]->Type == _Type_CCS)
  2065. {
  2066. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2067. {
  2068. if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
  2069. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  2070. {
  2071. //PRINTF_FUNC("ccs fw = %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  2072. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  2073. }
  2074. else
  2075. {
  2076. //PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  2077. evInitFlag = NO;
  2078. }
  2079. }
  2080. }
  2081. }
  2082. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  2083. {
  2084. if (ac_chargingInfo[index]->Type == _Type_AC)
  2085. {
  2086. if (strlen((char *)ac_chargingInfo[index]->version) != 0 ||
  2087. ac_chargingInfo[index]->version[0] != '\0')
  2088. {
  2089. ac_chargingInfo[index]->SelfTest_Comp = YES;
  2090. }
  2091. else
  2092. {
  2093. evInitFlag = NO;
  2094. }
  2095. }
  2096. }
  2097. }
  2098. if (ShmFanModuleData->SelfTest_Comp &&
  2099. ShmRelayModuleData->SelfTest_Comp &&
  2100. ShmPrimaryMcuData->SelfTest_Comp &&
  2101. evInitFlag)
  2102. {
  2103. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  2104. }
  2105. }
  2106. break;
  2107. case _STEST_AC_CONTACTOR:
  2108. {
  2109. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  2110. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  2111. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  2112. {
  2113. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  2114. PRINTF_FUNC("Communication board pass. \n");
  2115. }
  2116. }
  2117. break;
  2118. case _STEST_PSU_DETECT:
  2119. {
  2120. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  2121. {
  2122. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  2123. }
  2124. }
  2125. break;
  2126. case _STEST_PSU_CAP:
  2127. {
  2128. // 此測試是要確認當前總輸出能力
  2129. // 如果沒有 PSU 模組請 bypass
  2130. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  2131. {
  2132. sleep(1);
  2133. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  2134. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  2135. }
  2136. }
  2137. break;
  2138. }
  2139. }
  2140. else
  2141. break;
  2142. usleep(100000);
  2143. }
  2144. }
  2145. int SpawnTask()
  2146. {
  2147. sleep(2);
  2148. system("/root/Module_EventLogging &");
  2149. system("/root/Module_PrimaryComm &");
  2150. system("/root/Module_EvComm &");
  2151. system("/root/Module_LcmControl &");
  2152. system("/root/Module_InternalComm &");
  2153. system("/root/Module_PsuComm &");
  2154. system("/root/Module_SmartBox &");
  2155. system("/root/Module_ProduceUtils &");
  2156. ocpp_process_start();
  2157. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  2158. {
  2159. system("/root/Module_4g &");
  2160. }
  2161. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2162. {
  2163. system("/root/Module_Wifi &");
  2164. }
  2165. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2166. {
  2167. system("/root/Module_4g &");
  2168. system("/root/Module_Wifi &");
  2169. }
  2170. return PASS;
  2171. }
  2172. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2173. {
  2174. int result = PASS;
  2175. int fd,wrd;
  2176. unsigned int i,Chk;
  2177. unsigned char *ptr, *BufTmp;
  2178. Chk=0;
  2179. ptr=(unsigned char *)UsrData;
  2180. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2181. {
  2182. memset(BufTmp, 0, MtdBlockSize);
  2183. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2184. for(i=0; i<MtdBlockSize-4; i++)
  2185. Chk+=*(ptr+i);
  2186. memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
  2187. fd = open("/dev/mtdblock10", O_RDWR);
  2188. if (fd > 0)
  2189. {
  2190. wrd=write(fd, BufTmp, MtdBlockSize);
  2191. close(fd);
  2192. if(wrd >= MtdBlockSize)
  2193. {
  2194. fd = open("/dev/mtdblock11", O_RDWR);
  2195. if (fd > 0)
  2196. {
  2197. wrd=write(fd, BufTmp, MtdBlockSize);
  2198. close(fd);
  2199. if(wrd < MtdBlockSize)
  2200. {
  2201. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  2202. result = FAIL;
  2203. }
  2204. }
  2205. else
  2206. {
  2207. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  2208. result = FAIL;
  2209. }
  2210. }
  2211. else
  2212. {
  2213. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  2214. result = FAIL;
  2215. }
  2216. }
  2217. else
  2218. {
  2219. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  2220. result = FAIL;
  2221. }
  2222. }
  2223. else
  2224. {
  2225. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  2226. result = FAIL;
  2227. }
  2228. if(BufTmp != NULL)
  2229. free(BufTmp);
  2230. return result;
  2231. }
  2232. //===============================================
  2233. // Common Detect Chk - Stop Charging ?
  2234. //===============================================
  2235. bool isEvBoardStopChargeFlag(byte gunIndex)
  2236. {
  2237. return chargingInfo[gunIndex]->StopChargeFlag;
  2238. }
  2239. bool isEvBoardNormalStopChargeFlag(byte gunIndex)
  2240. {
  2241. return chargingInfo[gunIndex]->NormalStopChargeFlag;
  2242. }
  2243. //===============================================
  2244. // 插槍鎖槍狀況
  2245. //===============================================
  2246. void ClearDetectPluginFlag()
  2247. {
  2248. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2249. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2250. {
  2251. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  2252. chargingInfo[gun_index]->RemoteStartFlag = NO;
  2253. if (chargingInfo[gun_index]->ReservedStartFlag == YES)
  2254. chargingInfo[gun_index]->ReservedStartFlag = NO;
  2255. }
  2256. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  2257. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2258. }
  2259. void DetectPluginStart()
  2260. {
  2261. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  2262. }
  2263. bool isDetectPlugin()
  2264. {
  2265. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  2266. return YES;
  2267. return NO;
  2268. }
  2269. bool isEvGunLocked(byte gunIndex)
  2270. {
  2271. return chargingInfo[gunIndex]->GunLocked;
  2272. }
  2273. //===============================================
  2274. // Common Detect Chk - Chademo
  2275. //===============================================
  2276. bool isEvContactorWelding_chademo(byte gunIndex)
  2277. {
  2278. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  2279. }
  2280. bool isEvStopReq_chademo(byte gunIndex)
  2281. {
  2282. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  2283. }
  2284. byte isPrechargeStatus_chademo(byte gunIndex)
  2285. {
  2286. byte result = 0x00;
  2287. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2288. return result;
  2289. }
  2290. //===============================================
  2291. // Common Detect Chk - GB
  2292. //===============================================
  2293. byte isPrechargeStatus_gb(byte gunIndex)
  2294. {
  2295. byte result = 0x00;
  2296. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2297. return result;
  2298. }
  2299. //===============================================
  2300. // Common Detect Chk - CCS
  2301. //===============================================
  2302. byte isPrechargeStatus_ccs(byte gunIndex)
  2303. {
  2304. byte result = 0x00;
  2305. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2306. {
  2307. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2308. }
  2309. return result;
  2310. }
  2311. //===============================================
  2312. // Callback
  2313. //===============================================
  2314. void DisplayChargingInfo()
  2315. {
  2316. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  2317. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2318. {
  2319. if (chargingInfo[i]->SystemStatus != SYS_MODE_IDLE &&
  2320. chargingInfo[i]->SystemStatus != SYS_MODE_RESERVATION)
  2321. {
  2322. ChangeGunSelectByIndex(i);
  2323. usleep(50000);
  2324. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2325. return;
  2326. }
  2327. }
  2328. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2329. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  2330. ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_COMPLETE)
  2331. {
  2332. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2333. }
  2334. usleep(50000);
  2335. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2336. }
  2337. void _AutoReturnTimeout()
  2338. {
  2339. PRINTF_FUNC("*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  2340. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2341. {
  2342. ClearDetectPluginFlag();
  2343. }
  2344. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2345. {
  2346. bool needPluginDet = true;
  2347. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  2348. {
  2349. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2350. {
  2351. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  2352. {
  2353. needPluginDet = false;
  2354. break;
  2355. }
  2356. }
  2357. }
  2358. if (needPluginDet)
  2359. DetectPluginStart();
  2360. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHCOMP;
  2361. }
  2362. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  2363. {
  2364. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHFAIL;
  2365. }
  2366. usleep(50000);
  2367. stopChargingChkByCard = false;
  2368. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2369. }
  2370. void _SelfTestTimeout()
  2371. {
  2372. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  2373. {
  2374. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2375. {
  2376. ChargingAlarmProcess(gun_index);
  2377. }
  2378. }
  2379. ShmPsuData->Work_Step = _NO_WORKING;
  2380. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2381. PRINTF_FUNC("Self test timeout. \n");
  2382. }
  2383. void _AuthorizedTimeout()
  2384. {
  2385. //if(IsAuthorizingMode())
  2386. {
  2387. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  2388. isCardScan = false;
  2389. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2390. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTHFAIL;
  2391. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  2392. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2393. ClearAuthorizedFlag();
  2394. }
  2395. }
  2396. void _DetectPlugInTimeout()
  2397. {
  2398. PRINTF_FUNC("*********** _DetectPlugInTimeout (%d)*********** \n", _connectionTimeout);
  2399. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2400. ClearDetectPluginFlag();
  2401. usleep(50000);
  2402. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2403. }
  2404. //LW_tpc plug detect
  2405. void _DetectTpcPlugInTimeout()
  2406. {
  2407. PRINTF_FUNC("// _Detect CHAdeMO_TPC PlugInTimeout(60 sec) // \n");
  2408. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE;
  2409. }
  2410. void _DetectEvChargingEnableTimeout(byte gunIndex)
  2411. {
  2412. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2413. {
  2414. if(!isEvGunLocked(gunIndex))
  2415. {
  2416. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  2417. }
  2418. }
  2419. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2420. {
  2421. if(!isEvGunLocked(gunIndex))
  2422. {
  2423. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  2424. }
  2425. }
  2426. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  2427. {
  2428. if(!isEvGunLocked(gunIndex))
  2429. {
  2430. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  2431. }
  2432. }
  2433. ChargingTerminalProcess(gunIndex);
  2434. _AutoReturnTimeout();
  2435. }
  2436. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  2437. {
  2438. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  2439. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  2440. {
  2441. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  2442. _AutoReturnTimeout();
  2443. }
  2444. }
  2445. void _PrepareTimeout(byte gunIndex)
  2446. {
  2447. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  2448. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  2449. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  2450. _AutoReturnTimeout();
  2451. }
  2452. void _CcsPrechargeTimeout(byte gunIndex)
  2453. {
  2454. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  2455. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  2456. }
  2457. //===============================================
  2458. // 卡號驗證
  2459. //===============================================
  2460. void AuthorizingStart()
  2461. {
  2462. ocpp_set_authorizeReq_cmd(YES);
  2463. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  2464. }
  2465. void ClearAuthorizedFlag()
  2466. {
  2467. ocpp_set_authorizeConf_cmd(NO);
  2468. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  2469. }
  2470. bool isAuthorizedComplete()
  2471. {
  2472. return ocpp_get_authorize_conf();
  2473. }
  2474. bool IsAuthorizingMode()
  2475. {
  2476. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  2477. return false;
  2478. return true;
  2479. }
  2480. //===============================================
  2481. // 紀錄 Alarm Code
  2482. //===============================================
  2483. void RecordAlarmCode(byte gunIndex, char *code)
  2484. {
  2485. if (strcmp(code, "011018") == EQUAL) { ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = YES; return; }
  2486. else if (strcmp(code, "011019") == EQUAL) { ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = YES; return; }
  2487. else if (strcmp(code, "011020") == EQUAL) { ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = YES; return; }
  2488. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) == EQUAL)
  2489. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  2490. if (strcmp(code, "012234") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = YES;
  2491. else if (strcmp(code, "012235") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  2492. else if (strcmp(code, "012236") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = YES;
  2493. else if (strcmp(code, "012288") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = YES;
  2494. else if (strcmp(code, "012289") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = YES;
  2495. else if (strcmp(code, "012290") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = YES;
  2496. else if (strcmp(code, "012229") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = YES;
  2497. else if (strcmp(code, "012230") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = YES;
  2498. else if (strcmp(code, "012231") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = YES;
  2499. else if (strcmp(code, "012296") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = YES;
  2500. else if (strcmp(code, "012297") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = YES;
  2501. else if (strcmp(code, "012298") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = YES;
  2502. else if (strcmp(code, "011011") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = YES;
  2503. else if (strcmp(code, "011013") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = YES;
  2504. else if (strcmp(code, "011015") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = YES;
  2505. else if (strcmp(code, "011012") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = YES;
  2506. else if (strcmp(code, "011014") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = YES;
  2507. else if (strcmp(code, "011016") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = YES;
  2508. else if (strcmp(code, "012320") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = YES;
  2509. else if (strcmp(code, "012321") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = YES;
  2510. else if (strcmp(code, "012322") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = YES;
  2511. else if (strcmp(code, "011039") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault = YES;
  2512. else if (strcmp(code, "011040") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault = YES;
  2513. else if (strcmp(code, "023984") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.StopByEvseCondition = YES;
  2514. }
  2515. void ResetChargerAlarmCode(byte gunIndex, char *code)
  2516. {
  2517. if (strcmp(code, "012229") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP)
  2518. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  2519. else if (strcmp(code, "012230") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP)
  2520. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  2521. else if (strcmp(code, "012231") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP)
  2522. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  2523. else if (strcmp(code, "011011") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault)
  2524. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  2525. else if (strcmp(code, "011013") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault)
  2526. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  2527. else if (strcmp(code, "011015") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault)
  2528. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  2529. else if (strcmp(code, "011012") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault)
  2530. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  2531. else if (strcmp(code, "011014") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault)
  2532. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  2533. else if (strcmp(code, "011016") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault)
  2534. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  2535. else if (strcmp(code, "011018") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail)
  2536. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  2537. else if (strcmp(code, "011019") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail)
  2538. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  2539. else if (strcmp(code, "011020") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail)
  2540. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  2541. else if (strcmp(code, "012320") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP)
  2542. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = NO;
  2543. else if (strcmp(code, "012321") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP)
  2544. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = NO;
  2545. else if (strcmp(code, "012322") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP)
  2546. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = NO;
  2547. else if (strcmp(code, "011039") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault)
  2548. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayWeldingFault = NO;
  2549. else if (strcmp(code, "011040") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault)
  2550. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ParallelRelayDrivingFault = NO;
  2551. else if (strcmp(code, "023984") == EQUAL && ShmDcCommonData->ConnectErrList[gunIndex].GunBits.StopByEvseCondition)
  2552. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.StopByEvseCondition = NO;
  2553. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6) == EQUAL)
  2554. {
  2555. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2556. }
  2557. }
  2558. void ReleaseSysAlarmCode(byte gunIndex)
  2559. {
  2560. // 回 idle 後主要清除 GFD Trip、UVP、OVP、GFD Warning,PSU fail
  2561. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2562. {
  2563. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012251", 6) == EQUAL ||
  2564. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012252", 6) == EQUAL ||
  2565. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012324", 6) == EQUAL ||
  2566. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012325", 6) == EQUAL ||
  2567. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012267", 6) == EQUAL ||
  2568. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012346", 6) == EQUAL)
  2569. {
  2570. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2571. }
  2572. }
  2573. if (gunIndex == 0)
  2574. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_1 = NO;
  2575. else if (gunIndex == 1)
  2576. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_2 = NO;
  2577. ResetChargerAlarmCode(gunIndex, "023984");
  2578. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2579. {
  2580. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  2581. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  2582. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP = NO;
  2583. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  2584. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = NO;
  2585. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2586. {
  2587. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL ||
  2588. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL ||
  2589. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012217", 6) == EQUAL ||
  2590. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012296", 6) == EQUAL)
  2591. {
  2592. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2593. }
  2594. }
  2595. }
  2596. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  2597. {
  2598. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  2599. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  2600. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP = NO;
  2601. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  2602. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = NO;
  2603. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2604. {
  2605. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL ||
  2606. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL ||
  2607. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012219", 6) == EQUAL ||
  2608. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012297", 6) == EQUAL)
  2609. {
  2610. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2611. }
  2612. }
  2613. }
  2614. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2615. {
  2616. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  2617. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  2618. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP = NO;
  2619. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  2620. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = NO;
  2621. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2622. {
  2623. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL ||
  2624. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL ||
  2625. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012221", 6) == EQUAL ||
  2626. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012298", 6) == EQUAL)
  2627. {
  2628. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2629. }
  2630. }
  2631. }
  2632. ocpp_clear_errorCode_cmd(gunIndex);
  2633. }
  2634. void NormalStop(byte gunIndex)
  2635. {
  2636. PRINTF_FUNC("NormalStop (%d) \n", gunIndex);
  2637. }
  2638. //===============================================
  2639. // EmergencyStop and Charging Stop
  2640. //===============================================
  2641. void ChargingTerminalProcess(byte gunIndex)
  2642. {
  2643. SetChargerMode(gunIndex, SYS_MODE_TERMINATING);
  2644. }
  2645. void ChargingAlarmProcess(byte gunIndex)
  2646. {
  2647. CheckSystemErrorFunction(gunIndex);
  2648. ocpp_set_errCode_cmd(gunIndex);
  2649. SetChargerMode(gunIndex, SYS_MODE_ALARM);
  2650. }
  2651. void AcChargingTerminalProcess()
  2652. {
  2653. ac_chargingInfo[0]->SystemStatus = SYS_MODE_TERMINATING;
  2654. }
  2655. //void StopChargingProcessByString(byte level)
  2656. //{
  2657. // if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2658. // {
  2659. // ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2660. // }
  2661. //}
  2662. //void ReleaseChargingProcessByString(byte level)
  2663. //{
  2664. // if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2665. // ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2666. //}
  2667. // 一般錯誤停止充電處理函式
  2668. void BoardErrOccurByString(byte index, char *code)
  2669. {
  2670. //byte level = 1;
  2671. if ((chargingInfo[index]->SystemStatus > SYS_MODE_IDLE && chargingInfo[index]->SystemStatus < SYS_MODE_TERMINATING) ||
  2672. (chargingInfo[index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2673. {
  2674. PRINTF_FUNC("BoardErrOccurByString (%d) - (%s) \n", index, code);
  2675. if (strncmp(code, "023730", 6) == EQUAL &&
  2676. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2677. {
  2678. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2679. }
  2680. ChargingAlarmProcess(index);
  2681. }
  2682. //60StopChargingProcessByString(level);
  2683. }
  2684. // 急停狀況的停止充電處理函式
  2685. void EmcOccurThenStopCharging()
  2686. {
  2687. //byte level = 2;
  2688. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen...
  2689. // 其錯誤等級為 2
  2690. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2691. {
  2692. if ((chargingInfo[gun]->SystemStatus > SYS_MODE_IDLE && chargingInfo[gun]->SystemStatus < SYS_MODE_TERMINATING) ||
  2693. (chargingInfo[gun]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[gun]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2694. {
  2695. ChargingAlarmProcess(gun);
  2696. }
  2697. }
  2698. //StopChargingProcessByString(level);
  2699. }
  2700. void ReleaseBoardErrOccurByString(byte index, char *code)
  2701. {
  2702. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  2703. {
  2704. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2705. }
  2706. }
  2707. void ReleaseEmsOccureByString(byte index, char *code)
  2708. {
  2709. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2710. {
  2711. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2712. }
  2713. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2714. {
  2715. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2716. }
  2717. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2718. {
  2719. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2720. }
  2721. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2722. {
  2723. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2724. }
  2725. }
  2726. //===============================================
  2727. // 確認硬體 (按鈕) 狀態
  2728. //===============================================
  2729. bool leftBtnPush = false;
  2730. bool rightBtnPush = false;
  2731. byte _delayDetectSingleBtnCnt = 0;
  2732. bool twoBtnPress = 0;
  2733. bool btnDelayFlag = 0;
  2734. void ButtonTriggerEvent()
  2735. {
  2736. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS &&
  2737. ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS)
  2738. {
  2739. ShmSysConfigAndInfo->SysConfig.ShowInformation = YES;
  2740. twoBtnPress = TRUE;
  2741. }
  2742. else
  2743. {
  2744. ShmSysConfigAndInfo->SysConfig.ShowInformation = NO;
  2745. //Delay for detecting the single btn state after pressing two btn.
  2746. if (twoBtnPress == TRUE && _delayDetectSingleBtnCnt < 100)
  2747. _delayDetectSingleBtnCnt++;
  2748. else
  2749. {
  2750. if (twoBtnPress == TRUE)
  2751. twoBtnPress = FALSE;
  2752. _delayDetectSingleBtnCnt = 0;
  2753. }
  2754. }
  2755. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  2756. {
  2757. if(!leftBtnPush)
  2758. {
  2759. leftBtnPush = BTN_PRESS;
  2760. PRINTF_FUNC("left btn down............................... \n");
  2761. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2762. {
  2763. switch(ac_chargingInfo[0]->SystemStatus)
  2764. {
  2765. case SYS_MODE_IDLE:
  2766. case SYS_MODE_MAINTAIN:
  2767. case SYS_MODE_RESERVATION:
  2768. {
  2769. if(isDetectPlugin())
  2770. {
  2771. _DetectPlugInTimeout();
  2772. StopSystemTimeoutDet();
  2773. }
  2774. }
  2775. break;
  2776. case SYS_MODE_MODE_REASSIGN_CHECK:
  2777. case SYS_MODE_REASSIGN:
  2778. case SYS_MODE_PREPARING:
  2779. case SYS_MODE_PREPARE_FOR_EV:
  2780. case SYS_MODE_PREPARE_FOR_EVSE:
  2781. case SYS_MODE_CCS_PRECHARGE_STEP0:
  2782. case SYS_MODE_CCS_PRECHARGE_STEP1:
  2783. {
  2784. // 取消充電
  2785. AcChargingTerminalProcess();
  2786. }
  2787. break;
  2788. case SYS_MODE_CHARGING:
  2789. {
  2790. if (ShmSysConfigAndInfo->SysConfig.StopChargingByButton == YES ||
  2791. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  2792. ac_chargingInfo[0]->isRemoteStart == NO) ||
  2793. ac_chargingInfo[0]->isRemoteStart)
  2794. {
  2795. // 停止充電
  2796. AcChargingTerminalProcess();
  2797. }
  2798. }
  2799. break;
  2800. case SYS_MODE_COMPLETE:
  2801. {}
  2802. break;
  2803. }
  2804. }
  2805. else
  2806. {
  2807. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2808. {
  2809. case SYS_MODE_IDLE:
  2810. case SYS_MODE_MAINTAIN:
  2811. case SYS_MODE_RESERVATION:
  2812. {
  2813. if(isDetectPlugin())
  2814. {
  2815. _DetectPlugInTimeout();
  2816. StopSystemTimeoutDet();
  2817. btnDelayFlag = TRUE;
  2818. }
  2819. else
  2820. {
  2821. //LW_tpc plug detect
  2822. if (ShmDcCommonData->TpcDetectFunction == YES
  2823. && ShmSysConfigAndInfo->SysConfig.ShowInformation == NO && twoBtnPress == FALSE
  2824. && btnDelayFlag == FALSE)
  2825. {
  2826. if (ShmDcCommonData->_tpcPlugDetectState != _TPC_PLUG_DETECT_STATUS_WAIT)
  2827. {
  2828. PRINTF_FUNC("// Start Detect Plug in // \n");
  2829. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_WAIT;
  2830. GetTimespecFunc(&_tcpDetectStatus_time);
  2831. }
  2832. else
  2833. {
  2834. PRINTF_FUNC("// Stop Detect PlugIn for TPC. // \n");
  2835. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE;
  2836. StopGunInfoTimeoutDet(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2837. }
  2838. }
  2839. }
  2840. }
  2841. break;
  2842. case SYS_MODE_MODE_REASSIGN_CHECK:
  2843. case SYS_MODE_REASSIGN:
  2844. case SYS_MODE_PREPARING:
  2845. case SYS_MODE_PREPARE_FOR_EV:
  2846. case SYS_MODE_PREPARE_FOR_EVSE:
  2847. case SYS_MODE_CCS_PRECHARGE_STEP0:
  2848. case SYS_MODE_CCS_PRECHARGE_STEP1:
  2849. {
  2850. // 按鈕 - 取消充電
  2851. // if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2852. // AcChargingTerminalProcess();
  2853. // else
  2854. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2855. }
  2856. break;
  2857. case SYS_MODE_CHARGING:
  2858. {
  2859. if (ShmSysConfigAndInfo->SysConfig.StopChargingByButton == YES ||
  2860. ShmDcCommonData->enObtainAuthorizbyCCID ||
  2861. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  2862. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->isRemoteStart == NO) ||
  2863. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->isRemoteStart)
  2864. {
  2865. // 按鈕 - 停止充電
  2866. NormalStop(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2867. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2868. }
  2869. }
  2870. break;
  2871. case SYS_MODE_COMPLETE:
  2872. {
  2873. // 回 IDLE
  2874. //PRINTF_FUNC("right btn down.................SYS_MODE_COMPLETE \n");
  2875. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = SYS_MODE_IDLE;
  2876. }
  2877. break;
  2878. }
  2879. }
  2880. }
  2881. }
  2882. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  2883. {
  2884. if(leftBtnPush)
  2885. {
  2886. leftBtnPush = BTN_RELEASE;
  2887. PRINTF_FUNC("left btn up............................... \n");
  2888. //LW_tpc plug detect
  2889. switch (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2890. {
  2891. case SYS_MODE_IDLE:
  2892. btnDelayFlag = FALSE;
  2893. break;
  2894. }
  2895. }
  2896. }
  2897. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  2898. {
  2899. if(!rightBtnPush)
  2900. {
  2901. rightBtnPush = BTN_PRESS;
  2902. PRINTF_FUNC("right btn down .................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2903. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG &&
  2904. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  2905. return;
  2906. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2907. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  2908. ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 == ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  2909. {
  2910. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2911. }
  2912. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2913. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  2914. {
  2915. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2916. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2917. }
  2918. else if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2919. {
  2920. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  2921. {
  2922. if (chargingInfo[_index]->SystemStatus != SYS_MODE_BOOTING &&
  2923. chargingInfo[_index]->SystemStatus != SYS_MODE_IDLE &&
  2924. chargingInfo[_index]->SystemStatus != SYS_MODE_RESERVATION)
  2925. {
  2926. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  2927. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2928. return;
  2929. }
  2930. }
  2931. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2932. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2933. }
  2934. else
  2935. {
  2936. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2937. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2938. }
  2939. }
  2940. }
  2941. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  2942. {
  2943. if(rightBtnPush)
  2944. {
  2945. rightBtnPush = BTN_RELEASE;
  2946. PRINTF_FUNC("right btn up............................... \n");
  2947. }
  2948. }
  2949. }
  2950. void ChkPrimaryStatus()
  2951. {
  2952. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2953. {
  2954. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2955. EmcOccurThenStopCharging();
  2956. }
  2957. else
  2958. ReleaseEmsOccureByString(0, "012251");
  2959. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2960. {
  2961. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2962. EmcOccurThenStopCharging();
  2963. }
  2964. else
  2965. ReleaseEmsOccureByString(0, "012238");
  2966. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2967. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2968. else
  2969. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2970. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2971. {
  2972. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2973. EmcOccurThenStopCharging();
  2974. }
  2975. else
  2976. ReleaseEmsOccureByString(0, "012252");
  2977. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2978. {
  2979. EmcOccurThenStopCharging();
  2980. }
  2981. else
  2982. ReleaseEmsOccureByString(0, "012267");
  2983. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU)
  2984. {
  2985. EmcOccurThenStopCharging ();
  2986. }
  2987. else
  2988. ReleaseEmsOccureByString (0,"012346");
  2989. }
  2990. //===============================================
  2991. // 確認各小板偵測的錯誤狀況
  2992. //===============================================
  2993. void CheckSystemErrorFunction(byte index)
  2994. {
  2995. // System
  2996. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  2997. RecordAlarmCode(index, "012251");
  2998. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip)
  2999. RecordAlarmCode(index, "012238");
  3000. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  3001. RecordAlarmCode(index, "012252");
  3002. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  3003. RecordAlarmCode(index, "012267");
  3004. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU)
  3005. RecordAlarmCode(index, "012346");
  3006. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  3007. {
  3008. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  3009. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  3010. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  3011. {
  3012. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  3013. {
  3014. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  3015. }
  3016. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  3017. RecordAlarmCode(index, "012203");
  3018. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  3019. RecordAlarmCode(index, "012204");
  3020. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  3021. RecordAlarmCode(index, "012205");
  3022. chargingInfo[index]->StopChargeFlag = YES;
  3023. }
  3024. else
  3025. {
  3026. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  3027. {
  3028. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3029. }
  3030. }
  3031. }
  3032. else
  3033. {
  3034. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  3035. {
  3036. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3037. }
  3038. }
  3039. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  3040. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  3041. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  3042. {
  3043. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  3044. {
  3045. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  3046. }
  3047. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  3048. RecordAlarmCode(index, "012200");
  3049. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  3050. RecordAlarmCode(index, "012201");
  3051. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  3052. RecordAlarmCode(index, "012202");
  3053. chargingInfo[index]->StopChargeFlag = YES;
  3054. }
  3055. else
  3056. {
  3057. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  3058. {
  3059. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3060. }
  3061. }
  3062. }
  3063. //===============================================
  3064. // 確認 GPIO 狀態
  3065. //===============================================
  3066. void gpio_set_value(unsigned int gpio, unsigned int value)
  3067. {
  3068. int fd;
  3069. char buf[MAX_BUF];
  3070. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  3071. fd = open(buf, O_WRONLY);
  3072. if (fd < 0)
  3073. {
  3074. perror("gpio/set-value");
  3075. return;
  3076. }
  3077. if (value)
  3078. write(fd, "1", 2);
  3079. else
  3080. write(fd, "0", 2);
  3081. close(fd);
  3082. }
  3083. int gpio_get_value(unsigned int gpio, unsigned int *value)
  3084. {
  3085. int fd;
  3086. char buf[MAX_BUF];
  3087. char ch;
  3088. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  3089. fd = open(buf, O_RDONLY);
  3090. if (fd < 0) {
  3091. perror("gpio/get-value");
  3092. return fd;
  3093. }
  3094. read(fd, &ch, 1);
  3095. if (ch != '0') {
  3096. *value = 1;
  3097. } else {
  3098. *value = 0;
  3099. }
  3100. close(fd);
  3101. return 0;
  3102. }
  3103. void CheckGunTypeFromHw()
  3104. {
  3105. int pinIn[4] = { 22, 23, 44, 45 };
  3106. unsigned int gpioValue = 0;
  3107. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  3108. gpio_get_value(pinIn[i], &gpioValue);
  3109. {
  3110. switch (pinIn[i])
  3111. {
  3112. case 22:
  3113. bd1_1_status = gpioValue;
  3114. break;
  3115. case 23:
  3116. bd1_2_status = gpioValue;
  3117. break;
  3118. case 44:
  3119. bd0_1_status = gpioValue;
  3120. break;
  3121. case 45:
  3122. bd0_2_status = gpioValue;
  3123. break;
  3124. }
  3125. }
  3126. }
  3127. }
  3128. // Bypass useless functions
  3129. void CheckGpioInStatus()
  3130. {
  3131. int pinIn[2] = { 27, 47 };
  3132. unsigned int gpioValue = 0;
  3133. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  3134. {
  3135. gpio_get_value(pinIn[i], &gpioValue);
  3136. if (gpioValue == 0x01)
  3137. {
  3138. switch(pinIn[i])
  3139. {
  3140. // 小板緊急停止
  3141. case 47:
  3142. {
  3143. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3144. {
  3145. if (chargingInfo[i]->slotsIndex == 1)
  3146. {
  3147. // if (chargingInfo[i]->Type == _Type_Chademo)
  3148. // BoardErrOccurByString(i, "023730");
  3149. // else if (chargingInfo[i]->Type == _Type_CCS)
  3150. // BoardErrOccurByString(i, "013627");
  3151. break;
  3152. }
  3153. }
  3154. }
  3155. break;
  3156. case 27:
  3157. {
  3158. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3159. {
  3160. if (chargingInfo[i]->slotsIndex == 3)
  3161. {
  3162. // if (chargingInfo[i]->Type == _Type_Chademo)
  3163. // BoardErrOccurByString(i, "023730");
  3164. // else if (chargingInfo[i]->Type == _Type_CCS)
  3165. // BoardErrOccurByString(i, "013627");
  3166. break;
  3167. }
  3168. }
  3169. }
  3170. break;
  3171. }
  3172. }
  3173. else
  3174. {
  3175. switch (pinIn[i])
  3176. {
  3177. // 小板解除緊急停止
  3178. case 47:
  3179. {
  3180. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3181. {
  3182. if (chargingInfo[i]->slotsIndex == 1)
  3183. {
  3184. if (chargingInfo[i]->Type == _Type_Chademo)
  3185. ReleaseBoardErrOccurByString(i, "023730");
  3186. // else if (chargingInfo[i]->Type == _Type_CCS)
  3187. // ReleaseBoardErrOccurByString(i, "013627");
  3188. break;
  3189. }
  3190. }
  3191. }
  3192. break;
  3193. case 27:
  3194. {
  3195. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  3196. {
  3197. if (chargingInfo[i]->slotsIndex == 3)
  3198. {
  3199. if (chargingInfo[i]->Type == _Type_Chademo)
  3200. ReleaseBoardErrOccurByString(i, "023730");
  3201. // else if (chargingInfo[i]->Type == _Type_CCS)
  3202. // ReleaseBoardErrOccurByString(i, "013627");
  3203. break;
  3204. }
  3205. }
  3206. }
  3207. break;
  3208. }
  3209. }
  3210. }
  3211. }
  3212. //===============================================
  3213. // Main process
  3214. //===============================================
  3215. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  3216. {
  3217. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  3218. {
  3219. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  3220. {
  3221. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  3222. return true;
  3223. }
  3224. }
  3225. for (byte index = 0; index < CCS_QUANTITY; index++)
  3226. {
  3227. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  3228. {
  3229. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  3230. return true;
  3231. }
  3232. }
  3233. for (byte index = 0; index < GB_QUANTITY; index++)
  3234. {
  3235. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  3236. {
  3237. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  3238. return true;
  3239. }
  3240. }
  3241. return false;
  3242. }
  3243. // 檢查 Byte 中某個 Bit 的值
  3244. // _byte : 欲改變的 byte
  3245. // _bit : 該 byte 的第幾個 bit
  3246. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  3247. {
  3248. return ( _byte & mask_table[_bit] ) != 0x00;
  3249. }
  3250. // 設定 Byte 中某個 Bit的值
  3251. // _byte : 欲改變的 byte
  3252. // _bit : 該 byte 的第幾個 bit
  3253. // value : 修改的值為 0 or 1
  3254. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  3255. {
  3256. if(value == 1)
  3257. *_byte |= (1 << _bit);
  3258. else if (value == 0)
  3259. *_byte ^= (1 << _bit);
  3260. }
  3261. void GetCcidAuthPossible(bool *idleReq, byte *idleIndex)
  3262. {
  3263. for (byte gun_index = 0;
  3264. gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  3265. gun_index ++)
  3266. {
  3267. if (chargingInfo [gun_index]->SystemStatus == SYS_MODE_PREPARE_FOR_EV &&
  3268. ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  3269. {
  3270. *idleIndex = gun_index;
  3271. *idleReq = true;
  3272. break;
  3273. }
  3274. }
  3275. }
  3276. void UserScanFunction()
  3277. {
  3278. bool idleReq = false;
  3279. unsigned char idleIndex = 255;
  3280. //unsigned char stopReq = 255;
  3281. bool hasGunUsing = false;
  3282. bool isReservedCard = false;
  3283. // 當前非驗證的狀態
  3284. if(!IsAuthorizingMode())
  3285. {
  3286. // 先判斷現在是否可以提供刷卡
  3287. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  3288. // 2. 停止充電
  3289. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX ||
  3290. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_EMC)
  3291. {
  3292. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3293. return;
  3294. }
  3295. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  3296. {
  3297. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  3298. ac_chargingInfo[0]->SystemStatus != SYS_MODE_ALARM)
  3299. {
  3300. // 如果選 AC 槍
  3301. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  3302. ac_chargingInfo[0]->SystemStatus < SYS_MODE_ALARM)
  3303. {
  3304. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  3305. {
  3306. char value[32];
  3307. PRINTF_FUNC("ac stop charging \n");
  3308. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  3309. ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  3310. memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId, ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  3311. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  3312. {
  3313. AcChargingTerminalProcess();
  3314. }
  3315. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3316. }
  3317. }
  3318. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE ||
  3319. ac_chargingInfo[0]->SystemStatus == SYS_MODE_RESERVATION)
  3320. {
  3321. idleReq = true;
  3322. }
  3323. }
  3324. else
  3325. {
  3326. if (IsGunUsing(ShmSysConfigAndInfo->SysInfo.CurGunSelected))
  3327. {
  3328. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_CHARGING)
  3329. {
  3330. char value[32];
  3331. PRINTF_FUNC("stop charging \n");
  3332. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  3333. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  3334. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  3335. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  3336. // 同一張卡直接停掉
  3337. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  3338. {
  3339. NormalStop(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3340. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3341. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3342. }
  3343. else
  3344. {
  3345. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  3346. {
  3347. GetCcidAuthPossible(&idleReq, &idleIndex);
  3348. }
  3349. if (!idleReq)
  3350. {
  3351. PRINTF_FUNC ( "_LCM_AUTHORIZ_FAIL \n" );
  3352. stopChargingChkByCard = true;
  3353. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  3354. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3355. }
  3356. }
  3357. }
  3358. else
  3359. {
  3360. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  3361. {
  3362. GetCcidAuthPossible(&idleReq, &idleIndex);
  3363. }
  3364. }
  3365. }
  3366. // else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE ||
  3367. // chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  3368. // {
  3369. // idleIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  3370. // idleReq = true;
  3371. // }
  3372. else
  3373. {
  3374. if (ShmDcCommonData->enObtainAuthorizbyCCID &&
  3375. (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZING ||
  3376. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZ_COMP ||
  3377. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_AUTHORIZ_FAIL))
  3378. return;
  3379. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3380. {
  3381. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  3382. {
  3383. idleIndex = gun_index;
  3384. idleReq = true;
  3385. }
  3386. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3387. {
  3388. idleReq = true;
  3389. }
  3390. else
  3391. {
  3392. hasGunUsing = IsGunUsing(gun_index);
  3393. }
  3394. }
  3395. // 壁掛不給充 - 已經有至少一把槍在充電
  3396. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES &&
  3397. hasGunUsing)
  3398. {
  3399. PRINTF_FUNC("DW Serial not support dual charging. \n");
  3400. idleReq = false;
  3401. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3402. }
  3403. }
  3404. }
  3405. if (idleReq)
  3406. {
  3407. // if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  3408. // stopReq != 255 &&
  3409. // ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  3410. // {
  3411. // idleReq = false;
  3412. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3413. // }
  3414. // else
  3415. {
  3416. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gun_index++)
  3417. {
  3418. if (ac_chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3419. {
  3420. if (strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3421. (char *) ShmDcCommonData->_reserved_ac_UserId [gun_index] ) == EQUAL)
  3422. {
  3423. isReservedCard = true;
  3424. // 驗證通過
  3425. ac_chargingInfo[gun_index]->ReservedStartFlag = YES;
  3426. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3427. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  3428. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3429. break;
  3430. }
  3431. }
  3432. }
  3433. if (!isReservedCard)
  3434. {
  3435. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3436. {
  3437. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3438. {
  3439. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId,
  3440. (char *)ShmDcCommonData->_reserved_UserId[gun_index]) == EQUAL)
  3441. {
  3442. isReservedCard = true;
  3443. // 驗證通過
  3444. chargingInfo[gun_index]->ReservedStartFlag = YES;
  3445. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3446. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3447. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3448. break;
  3449. }
  3450. }
  3451. }
  3452. }
  3453. if (!isReservedCard &&
  3454. ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  3455. (idleIndex != 255 && (chargingInfo[idleIndex]->SystemStatus == SYS_MODE_IDLE || chargingInfo[idleIndex]->SystemStatus == SYS_MODE_PREPARE_FOR_EV))))
  3456. {
  3457. if (ShmDcCommonData->_CcidAuthProcessStep != _CCID_AUTH)
  3458. {
  3459. PRINTF_FUNC("Start Authorizing...UserId = %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3460. // LCM => Authorizing
  3461. if (ShmDcCommonData->enObtainAuthorizbyCCID &&
  3462. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != DEFAULT_AC_INDEX)
  3463. {
  3464. ShmSysConfigAndInfo->SysInfo.CurGunSelected = idleIndex;
  3465. }
  3466. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  3467. ShmDcCommonData->_CcidAuthProcessStep = _CCID_AUTH;
  3468. // 進入確認卡號狀態
  3469. AuthorizingStart();
  3470. }
  3471. }
  3472. else
  3473. {
  3474. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3475. }
  3476. }
  3477. }
  3478. else
  3479. {
  3480. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3481. }
  3482. }
  3483. }
  3484. }
  3485. unsigned char isModeChange(unsigned char gun_index)
  3486. {
  3487. unsigned char result = NO;
  3488. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  3489. {
  3490. result = YES;
  3491. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  3492. }
  3493. return result;
  3494. }
  3495. void ScannerCardProcess()
  3496. {
  3497. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL &&
  3498. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3499. {
  3500. isCardScan = true;
  3501. // 處理刷卡及驗證卡號的動作
  3502. UserScanFunction();
  3503. }
  3504. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3505. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3506. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  3507. {
  3508. StartSystemTimeoutDet(Timeout_Authorizing);
  3509. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3510. {
  3511. // 白名單驗證
  3512. for (int i = 0; i < 10; i++)
  3513. {
  3514. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  3515. {
  3516. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  3517. {
  3518. PRINTF_FUNC("White Card Pass (%s)... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3519. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3520. ClearAuthorizedFlag();
  3521. break;
  3522. }
  3523. }
  3524. }
  3525. }
  3526. // 確認驗證卡號完成沒
  3527. if (isAuthorizedComplete() || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3528. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3529. {
  3530. StopSystemTimeoutDet();
  3531. // 判斷後台回覆狀態
  3532. if(ocpp_chk_authrization_cmd() || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3533. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3534. {
  3535. // LCM => Authorize complete
  3536. PRINTF_FUNC("_LCM_AUTHORIZ_COMP \n");
  3537. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3538. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  3539. }
  3540. else
  3541. {
  3542. // LCM => Authorize fail
  3543. PRINTF_FUNC("_LCM_AUTHORIZ_FAIL \n");
  3544. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3545. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3546. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  3547. }
  3548. ClearAuthorizedFlag();
  3549. }
  3550. }
  3551. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  3552. {
  3553. StartSystemTimeoutDet(Timeout_VerifyFail);
  3554. isCardScan = false;
  3555. }
  3556. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  3557. {
  3558. StartSystemTimeoutDet(Timeout_VerifyComp);
  3559. }
  3560. else if(ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  3561. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  3562. {
  3563. StartSystemTimeoutDet(Timeout_WaitPlug);
  3564. }
  3565. else
  3566. {
  3567. isCardScan = false;
  3568. }
  3569. }
  3570. bool AddGunInfoByConnector(byte typeValue, byte slots)
  3571. {
  3572. bool result = true;
  3573. switch (typeValue)
  3574. {
  3575. case '0': // none
  3576. break;
  3577. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  3578. break;
  3579. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  3580. break;
  3581. case '3': // IEC 62196-2 Type 2 Plug
  3582. case '4': // IEC 62196-2 Type 2 Socket
  3583. if (AC_QUANTITY > _ac_Index)
  3584. {
  3585. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  3586. // AC 固定 index
  3587. ac_chargingInfo[_acgunIndex]->Index = DEFAULT_AC_INDEX;
  3588. ac_chargingInfo[_acgunIndex]->ReservationId = NO_RESERVE;
  3589. ac_chargingInfo[_acgunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3590. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  3591. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  3592. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3593. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3594. _ac_Index++;
  3595. _acgunIndex++;
  3596. }
  3597. else
  3598. result = false;
  3599. break;
  3600. case '5': // GB/T AC Plug
  3601. break;
  3602. case '6': // GB/T AC Socket
  3603. break;
  3604. case 'J': // CHAdeMO - 60A
  3605. case 'K': // CHAdeMO - 200A / 500V
  3606. case 'L': // CHAdeMO - 80A
  3607. case 'S': // CHAdeMO - 125A (boost : 200A)
  3608. {
  3609. if (CHAdeMO_QUANTITY > _chademoIndex)
  3610. {
  3611. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  3612. chargingInfo[_gunIndex]->Index = _gunIndex;
  3613. chargingInfo[_gunIndex]->ReservationId = NO_RESERVE;
  3614. chargingInfo[_gunIndex]->slotsIndex = slots;
  3615. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3616. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  3617. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  3618. chargingInfo[_gunIndex]->IsAvailable = YES;
  3619. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3620. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3621. chargingInfo[_gunIndex]->ModelType = typeValue;
  3622. ShmDcCommonData->TpcDetectFunction = YES; //LW_tpc plug detect
  3623. _chademoIndex++;
  3624. _gunIndex++;
  3625. }
  3626. else
  3627. result = false;
  3628. }
  3629. break;
  3630. case 'U': // Natural cooling CCS1
  3631. case 'E': // Natural cooling CCS2
  3632. case 'T': // Rema CCS1 - 300A
  3633. case 'D': // Rema CCS2 - 300A
  3634. case 'M': // CCS2 - 80A
  3635. case 'N': // CCS1 - 80A
  3636. case 'R': // CCS2 - 250A
  3637. case 'V' : // Liquid cooling CCS1 - 500A
  3638. case 'F' : // Liquid cooling CCS2 - 500A
  3639. case 'Y' : // CCS1 - 150A
  3640. case 'Z' : // CCS2 - 150A
  3641. {
  3642. if (CCS_QUANTITY > _ccsIndex)
  3643. {
  3644. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  3645. chargingInfo[_gunIndex]->Index = _gunIndex;
  3646. chargingInfo[_gunIndex]->ReservationId = NO_RESERVE;
  3647. chargingInfo[_gunIndex]->slotsIndex = slots;
  3648. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3649. chargingInfo[_gunIndex]->Type = _Type_CCS;
  3650. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  3651. chargingInfo[_gunIndex]->IsAvailable = YES;
  3652. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3653. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3654. chargingInfo[_gunIndex]->ModelType = typeValue;
  3655. // 現階段預設為走 DIN70121
  3656. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  3657. if (typeValue == 'U' || typeValue == 'T' || typeValue == 'N' || typeValue == 'V' || typeValue == 'Y')
  3658. ShmDcCommonData->CcsTypeSaved[_gunIndex] = _CCS_TYPE_CCS1;
  3659. else if (typeValue == 'E' || typeValue == 'D' || typeValue == 'M' || typeValue == 'R' || typeValue == 'F' || typeValue == 'Z')
  3660. ShmDcCommonData->CcsTypeSaved[_gunIndex] = _CCS_TYPE_CCS2;
  3661. _ccsIndex++;
  3662. _gunIndex++;
  3663. }
  3664. else
  3665. result = false;
  3666. }
  3667. break;
  3668. case 'G': // GBT DC
  3669. case 'B':
  3670. {
  3671. if (GB_QUANTITY > _gb_Index)
  3672. {
  3673. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  3674. chargingInfo[_gunIndex]->Index = _gunIndex;
  3675. chargingInfo[_gunIndex]->ReservationId = NO_RESERVE;
  3676. chargingInfo[_gunIndex]->slotsIndex = slots;
  3677. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3678. chargingInfo[_gunIndex]->Type = _Type_GB;
  3679. chargingInfo[_gunIndex]->type_index = _gb_Index;
  3680. chargingInfo[_gunIndex]->IsAvailable = YES;
  3681. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3682. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3683. chargingInfo[_gunIndex]->ModelType = typeValue;
  3684. _gb_Index++;
  3685. _gunIndex++;
  3686. }
  3687. else
  3688. result = false;
  3689. }
  3690. break;
  3691. }
  3692. return result;
  3693. }
  3694. bool CheckConnectorTypeStatus()
  3695. {
  3696. bool result = true;
  3697. // 硬體 0 1 => CCS,1 0 => Chademo,1 1 => GB
  3698. //CheckFwSlotSta.tusLog();
  3699. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  3700. {
  3701. byte slots = 1;
  3702. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  3703. {
  3704. if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  3705. {
  3706. return false;
  3707. }
  3708. slots++;
  3709. }
  3710. // AC index 接在 DC 後面
  3711. // if (_ac_Index > 0)
  3712. // ac_chargingInfo[0]->Index += _gunIndex;
  3713. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3714. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  3715. PRINTF_FUNC("Charger Count => DC-Count = %d, AC-Count = %d \n",
  3716. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  3717. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  3718. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  3719. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  3720. result = false;
  3721. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3722. {
  3723. bool isFind = false;
  3724. if (chargingInfo[0]->Type == _Type_Chademo)
  3725. {
  3726. if ((bd0_1_status == 0 && bd0_2_status == 1) ||
  3727. (bd1_1_status == 0 && bd1_2_status == 1))
  3728. {
  3729. isFind = true;
  3730. }
  3731. }
  3732. else if (chargingInfo[0]->Type == _Type_CCS)
  3733. {
  3734. if ((bd0_1_status == 1 && bd0_2_status == 0) ||
  3735. (bd1_1_status == 1 && bd1_2_status == 0))
  3736. {
  3737. isFind = true;
  3738. }
  3739. }
  3740. else if (chargingInfo[0]->Type == _Type_GB)
  3741. {
  3742. if ((bd0_1_status == 1 && bd0_2_status == 1) ||
  3743. (bd1_1_status == 1 && bd1_2_status == 1))
  3744. {
  3745. isFind = true;
  3746. }
  3747. }
  3748. if (isFind)
  3749. {
  3750. chargingInfo[0]->Evboard_id = 0x01;
  3751. CheckHwSlotStatusLog(0);
  3752. }
  3753. else
  3754. result = false;
  3755. }
  3756. else
  3757. {
  3758. char type = NO_DEFINE;
  3759. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  3760. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  3761. {
  3762. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  3763. {
  3764. PRINTF_FUNC("HW Conn %d (Chademo) \n", gunIndex);
  3765. // 與硬體相同 type : Chademo
  3766. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3767. {
  3768. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3769. }
  3770. }
  3771. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  3772. {
  3773. PRINTF_FUNC("HW Conn %d (CCS) \n", gunIndex);
  3774. // 與硬體相同 type : CCS
  3775. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  3776. {
  3777. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3778. }
  3779. }
  3780. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  3781. {
  3782. PRINTF_FUNC("HW Conn %d (GB) \n", gunIndex);
  3783. // 與硬體相同 type : GB
  3784. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3785. {
  3786. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3787. }
  3788. }
  3789. else if (gunIndex == 0)
  3790. {
  3791. PRINTF_FUNC("HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n",
  3792. gunIndex, bd0_1_status, bd0_2_status);
  3793. }
  3794. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  3795. {
  3796. PRINTF_FUNC("HW Conn %d (Chademo) \n", gunIndex);
  3797. // 與硬體相同 type : Chademo
  3798. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3799. {
  3800. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3801. }
  3802. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3803. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3804. }
  3805. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  3806. {
  3807. PRINTF_FUNC("HW Conn %d (CCS) \n", gunIndex);
  3808. // 與硬體相同 type : CCS
  3809. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  3810. {
  3811. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3812. }
  3813. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3814. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3815. }
  3816. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  3817. {
  3818. PRINTF_FUNC("HW Conn %d (GB) \n", gunIndex);
  3819. // 與硬體相同 type : GB
  3820. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3821. {
  3822. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3823. }
  3824. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3825. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3826. }
  3827. else if (gunIndex == 1)
  3828. {
  3829. PRINTF_FUNC("HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n",
  3830. gunIndex, bd1_1_status, bd1_2_status);
  3831. }
  3832. CheckHwSlotStatusLog(gunIndex);
  3833. if (type == NO_DEFINE)
  3834. type = chargingInfo[gunIndex]->Type;
  3835. else
  3836. {
  3837. // 雙槍都是同一個 type
  3838. if (type == chargingInfo[gunIndex]->Type)
  3839. ShmDcCommonData->SysGunAreSameType = YES;
  3840. }
  3841. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  3842. result = false;
  3843. }
  3844. }
  3845. }
  3846. else
  3847. {
  3848. // Module Name 不正確 - 告警
  3849. result = false;
  3850. }
  3851. return result;
  3852. }
  3853. void KillTask()
  3854. {
  3855. ChangeLcmByIndex(_LCM_FIX);
  3856. system("killall Module_EventLogging");
  3857. system("killall Module_PrimaryComm");
  3858. system("killall Module_EvComm");
  3859. system("killall Module_InternalComm");
  3860. system("killall Module_PsuComm");
  3861. system("killall Module_Module_SmartBox");
  3862. system("killall Module_4g &");
  3863. system("killall Module_Wifi &");
  3864. }
  3865. void KillTaskExceptDetectTask()
  3866. {
  3867. ChangeLcmByIndex(_LCM_FIX);
  3868. sleep(3);
  3869. system("killall Module_EvComm");
  3870. //system("killall Module_InternalComm");
  3871. system("killall Module_PsuComm");
  3872. system("killall Module_Module_SmartBox");
  3873. system("killall Module_4g &");
  3874. system("killall Module_Wifi &");
  3875. }
  3876. void KillAllTask()
  3877. {
  3878. ChangeLcmByIndex(_LCM_FIX);
  3879. system("killall Module_EventLogging");
  3880. system("killall Module_PrimaryComm");
  3881. system("killall Module_EvComm");
  3882. system("killall Module_LcmControl");
  3883. system("killall Module_InternalComm");
  3884. system("killall Module_PsuComm");
  3885. system("killall Module_Module_SmartBox");
  3886. system("killall OcppBackend &");
  3887. system("killall Module_4g &");
  3888. system("killall Module_Wifi &");
  3889. system("killall Module_DcMeter &");
  3890. }
  3891. void LogUpgradeType(unsigned int type)
  3892. {
  3893. switch (type)
  3894. {
  3895. case _UPDATE_TYPE_CSU_UBOOT: PRINTF_FUNC("CSU bootloader (uboot) \n"); break;
  3896. case _UPDATE_TYPE_CSU_DTB: PRINTF_FUNC("CSU kernel configuration (dtb) \n"); break;
  3897. case _UPDATE_TYPE_CSU_ZIMAGE: PRINTF_FUNC("CSU kernel image (zImage) \n"); break;
  3898. case _UPDATE_TYPE_CSU_RFILES: PRINTF_FUNC("CSU root file system (ramdisk.gz) \n"); break;
  3899. case _UPDATE_TYPE_CSU_CONFIG: PRINTF_FUNC("CSU user configuration (bin) \n"); break;
  3900. case _UPDATE_TYPE_CCS_UBOOT: PRINTF_FUNC("CCS board bootloader (uboot) \n"); break;
  3901. case _UPDATE_TYPE_CCS_DTB: PRINTF_FUNC("CCS board kernel configuration (dtb) \n"); break;
  3902. case _UPDATE_TYPE_CCS_ZIMAGE: PRINTF_FUNC("CCS board kernel image (zImage) \n"); break;
  3903. case _UPDATE_TYPE_CCS_RFILES: PRINTF_FUNC("CCS board file system (ramdisk.gz) \n"); break;
  3904. case _UPDATE_TYPE_DCM_ST407: PRINTF_FUNC("DCM 407 primary controller \n"); break;
  3905. case _UPDATE_TYPE_RELAY_CTR: PRINTF_FUNC("Relay control board \n"); break;
  3906. case _UPDATE_TYPE_FAN_CTR: PRINTF_FUNC("Fan control board \n"); break;
  3907. case _UPDATE_TYPE_AC_WLMOUNT_CTR: PRINTF_FUNC("AC wall-mount (low-end) controller \n"); break;
  3908. case _UPDATE_TYPE_LED_CTR: PRINTF_FUNC("LED control board \n"); break;
  3909. case _UPDATE_TYPE_CHADEMO: PRINTF_FUNC("CAHdeMO board \n"); break;
  3910. case _UPDATE_TYPE_GBT: PRINTF_FUNC("GBT board \n"); break;
  3911. }
  3912. }
  3913. int CheckUpdateProcess(void)
  3914. {
  3915. //bool isPass = true;
  3916. uint8_t retSucc = 0;
  3917. uint8_t retFail = 0;
  3918. byte index = 0;
  3919. byte target = 0;
  3920. char new_str[256];
  3921. unsigned char *ptr = NULL;
  3922. int fd = 0;
  3923. int CanFd = 0;
  3924. int uartFd = 0;
  3925. unsigned int Type = 0;
  3926. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  3927. DIR *d;
  3928. struct dirent *dir;
  3929. byte result = PASS;
  3930. char Buf[256];
  3931. d = opendir("/mnt/");
  3932. if (d)
  3933. {
  3934. while ((dir = readdir(d)) != NULL)
  3935. {
  3936. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
  3937. {
  3938. continue;
  3939. }
  3940. memset(new_str, 0x00, sizeof(new_str));
  3941. strcat(new_str, "/mnt/");
  3942. strcat(new_str, dir->d_name);
  3943. PRINTF_FUNC("%s \n", new_str);
  3944. fd = open(new_str, O_RDONLY);
  3945. if (fd < 0)
  3946. {
  3947. close(fd);
  3948. result = FAIL;
  3949. sleep(1);
  3950. continue;
  3951. }
  3952. ptr = malloc(MaxLen); //-48 is take out the header
  3953. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  3954. //get the image length
  3955. ImageLen = read(fd, ptr, MaxLen);
  3956. bool isModelNameFail = false;
  3957. // DS-----J0E-2PH
  3958. if((ShmSysConfigAndInfo->SysConfig.ModelName[0] != ptr[0]) ||
  3959. (ShmSysConfigAndInfo->SysConfig.ModelName[1] != ptr[1]) ||
  3960. (ShmSysConfigAndInfo->SysConfig.ModelName[7] != ptr[7]) ||
  3961. (ShmSysConfigAndInfo->SysConfig.ModelName[8] != ptr[8]) ||
  3962. (ShmSysConfigAndInfo->SysConfig.ModelName[9] != ptr[9]) ||
  3963. (ShmSysConfigAndInfo->SysConfig.ModelName[11] != ptr[11]) ||
  3964. (ShmSysConfigAndInfo->SysConfig.ModelName[12] != ptr[12]) ||
  3965. (ShmSysConfigAndInfo->SysConfig.ModelName[13] != ptr[13]))
  3966. {
  3967. result = MODELNAME_FAIL;
  3968. isModelNameFail = true;
  3969. }
  3970. if (isModelNameFail)
  3971. {
  3972. close(fd);
  3973. continue;
  3974. }
  3975. PRINTF_FUNC("model name check pass. \n");
  3976. if (ImageLen > 20) {
  3977. Type = (((unsigned int)ptr[16]) << 24 |
  3978. ((unsigned int)ptr[17]) << 16 |
  3979. ((unsigned int)ptr[18]) << 8 |
  3980. ((unsigned int)ptr[19]));
  3981. LogUpgradeType(Type);
  3982. PRINTF_FUNC("Upgrade Status : PASS : %d, FAIL : %d \n", retSucc, retFail);
  3983. free(ptr);
  3984. switch (Type)
  3985. {
  3986. case _UPDATE_TYPE_CSU_UBOOT:
  3987. case _UPDATE_TYPE_CSU_DTB:
  3988. case _UPDATE_TYPE_CSU_ZIMAGE:
  3989. case _UPDATE_TYPE_CSU_RFILES:
  3990. case _UPDATE_TYPE_CSU_CONFIG:
  3991. system("echo 3 > /proc/sys/vm/drop_caches");
  3992. sleep(2);
  3993. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3994. retSucc++;
  3995. else
  3996. {
  3997. PRINTF_FUNC("Upgrade CSU Failed (%d) \n", Type);
  3998. retFail++;
  3999. }
  4000. memset(Buf, 0, sizeof(Buf));
  4001. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4002. system(Buf);
  4003. break;
  4004. case _UPDATE_TYPE_CCS_UBOOT:
  4005. case _UPDATE_TYPE_CCS_DTB:
  4006. case _UPDATE_TYPE_CCS_ZIMAGE:
  4007. case _UPDATE_TYPE_CCS_RFILES:
  4008. CanFd = InitCanBus();
  4009. if (CanFd > 0)
  4010. {
  4011. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  4012. {
  4013. if (chargingInfo[index]->Type == _Type_CCS)
  4014. {
  4015. byte targetID = chargingInfo[index]->Evboard_id;
  4016. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1 &&
  4017. ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0)
  4018. {
  4019. targetID += 1;
  4020. }
  4021. system("echo 3 > /proc/sys/vm/drop_caches");
  4022. sleep(2);
  4023. PRINTF_FUNC("Upgrade CCS Processing..target id = %d \n", targetID);
  4024. if (Upgrade_CCS(CanFd, Type, targetID, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL)
  4025. {
  4026. PRINTF_FUNC("Upgrade CCS Failed \n");
  4027. retFail++;
  4028. }
  4029. else
  4030. retSucc++;
  4031. }
  4032. }
  4033. close(CanFd);
  4034. }
  4035. memset(Buf, 0, sizeof(Buf));
  4036. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4037. system(Buf);
  4038. break;
  4039. case _UPDATE_TYPE_DCM_ST407:
  4040. case _UPDATE_TYPE_RELAY_CTR:
  4041. case _UPDATE_TYPE_FAN_CTR:
  4042. case _UPDATE_TYPE_AC_WLMOUNT_CTR:
  4043. case _UPDATE_TYPE_LED_CTR:
  4044. target = 0x00;
  4045. if (Type == _UPDATE_TYPE_DCM_ST407) {
  4046. target = UPGRADE_PRI;
  4047. } else if (Type == _UPDATE_TYPE_RELAY_CTR) {
  4048. target = UPGRADE_RB;
  4049. } else if (Type == _UPDATE_TYPE_FAN_CTR) {
  4050. target = UPGRADE_FAN;
  4051. } else if (Type == _UPDATE_TYPE_AC_WLMOUNT_CTR) {
  4052. target = UPGRADE_AC;
  4053. } else if (Type == _UPDATE_TYPE_LED_CTR) {
  4054. target = UPGRADE_LED;
  4055. }
  4056. uartFd = InitComPort(target);
  4057. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  4058. {
  4059. retSucc++;
  4060. }
  4061. else
  4062. {
  4063. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  4064. retFail++;
  4065. }
  4066. if (uartFd > 0)
  4067. {
  4068. close(uartFd);
  4069. }
  4070. memset(Buf, 0, sizeof(Buf));
  4071. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4072. system(Buf);
  4073. break;
  4074. case _UPDATE_TYPE_CHADEMO:
  4075. case _UPDATE_TYPE_GBT:
  4076. CanFd = InitCanBus();
  4077. if (CanFd > 0)
  4078. {
  4079. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  4080. {
  4081. if ((Type == _UPDATE_TYPE_CHADEMO && chargingInfo[index]->Type == _Type_Chademo) ||
  4082. (Type == _UPDATE_TYPE_GBT && chargingInfo[index]->Type == _Type_GB))
  4083. {
  4084. PRINTF_FUNC("Upgrade GBT / Chademo Processing..target id = %d \n", chargingInfo[index]->Evboard_id);
  4085. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  4086. retSucc++;
  4087. else
  4088. {
  4089. if (chargingInfo[index]->Type == _Type_Chademo)
  4090. PRINTF_FUNC("Upgrade Chademo Failed \n");
  4091. else
  4092. PRINTF_FUNC("Upgrade GBT Failed \n");
  4093. retFail++;
  4094. }
  4095. }
  4096. }
  4097. close(CanFd);
  4098. }
  4099. memset(Buf, 0, sizeof(Buf));
  4100. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  4101. system(Buf);
  4102. break;
  4103. }
  4104. }
  4105. close(fd);
  4106. }
  4107. }
  4108. free(dir);
  4109. closedir(d);
  4110. PRINTF_FUNC("Upgrade Result : PASS : %d, FAIL : %d \n", retSucc, retFail);
  4111. sleep(1);
  4112. system("rm -rvf /mnt/* ");
  4113. if (retFail != 0)
  4114. {
  4115. result = FAIL;
  4116. }
  4117. return result;
  4118. }
  4119. void CreateRfidFork()
  4120. {
  4121. pid_t rfidRecPid;
  4122. rfidRecPid = fork();
  4123. if (rfidRecPid == 0)
  4124. {
  4125. int module_type = MODULE_EWT;
  4126. while(true)
  4127. {
  4128. // 刷卡判斷
  4129. RFID rfid;
  4130. if (!ShmSysConfigAndInfo->SysConfig.isRFID)
  4131. {}
  4132. else if(getRequestCardSN(rfidFd, module_type, &rfid))
  4133. {
  4134. //PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4135. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  4136. {
  4137. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  4138. {
  4139. switch (rfid.snType)
  4140. {
  4141. case RFID_SN_TYPE_6BYTE:
  4142. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4143. "%02X%02X%02X%02X%02X%02X",
  4144. rfid.currentCard[0], rfid.currentCard[1],
  4145. rfid.currentCard[2], rfid.currentCard[3],
  4146. rfid.currentCard[4], rfid.currentCard[5]);
  4147. break;
  4148. case RFID_SN_TYPE_7BYTE:
  4149. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4150. "%02X%02X%02X%02X%02X%02X%02X",
  4151. rfid.currentCard[0], rfid.currentCard[1],
  4152. rfid.currentCard[2], rfid.currentCard[3],
  4153. rfid.currentCard[4], rfid.currentCard[5],
  4154. rfid.currentCard[6]);
  4155. break;
  4156. case RFID_SN_TYPE_10BYTE:
  4157. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4158. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  4159. rfid.currentCard[0], rfid.currentCard[1],
  4160. rfid.currentCard[2], rfid.currentCard[3],
  4161. rfid.currentCard[4], rfid.currentCard[5],
  4162. rfid.currentCard[6], rfid.currentCard[7],
  4163. rfid.currentCard[8], rfid.currentCard[9]);
  4164. break;
  4165. case RFID_SN_TYPE_4BYTE:
  4166. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4167. "%02X%02X%02X%02X",
  4168. rfid.currentCard[0], rfid.currentCard[1],
  4169. rfid.currentCard[2], rfid.currentCard[3]);
  4170. break;
  4171. }
  4172. }
  4173. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  4174. {
  4175. switch (rfid.snType)
  4176. {
  4177. case RFID_SN_TYPE_6BYTE:
  4178. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4179. "%02X%02X%02X%02X%02X%02X",
  4180. rfid.currentCard[5], rfid.currentCard[4],
  4181. rfid.currentCard[3], rfid.currentCard[2],
  4182. rfid.currentCard[1], rfid.currentCard[0]);
  4183. break;
  4184. case RFID_SN_TYPE_7BYTE:
  4185. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4186. "%02X%02X%02X%02X%02X%02X%02X",
  4187. rfid.currentCard[6], rfid.currentCard[5],
  4188. rfid.currentCard[4], rfid.currentCard[3],
  4189. rfid.currentCard[2], rfid.currentCard[1],
  4190. rfid.currentCard[0]);
  4191. break;
  4192. case RFID_SN_TYPE_10BYTE:
  4193. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4194. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  4195. rfid.currentCard[9], rfid.currentCard[8],
  4196. rfid.currentCard[7], rfid.currentCard[6],
  4197. rfid.currentCard[5], rfid.currentCard[4],
  4198. rfid.currentCard[3], rfid.currentCard[2],
  4199. rfid.currentCard[1], rfid.currentCard[0]);
  4200. break;
  4201. case RFID_SN_TYPE_4BYTE:
  4202. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  4203. "%02X%02X%02X%02X",
  4204. rfid.currentCard[3], rfid.currentCard[2],
  4205. rfid.currentCard[1], rfid.currentCard[0]);
  4206. break;
  4207. }
  4208. }
  4209. PRINTF_FUNC("RFID : Get card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4210. }
  4211. if (rfid.cardType == ISO14443A)
  4212. sethaltCard ( rfidFd, module_type);
  4213. else if (rfid.cardType == IS014443B)
  4214. { }
  4215. else if (rfid.cardType == FELICA)
  4216. { }
  4217. }
  4218. sleep(1);
  4219. }
  4220. }
  4221. }
  4222. void CreateCheckSystemTaskFork()
  4223. {
  4224. pid_t taskPid;
  4225. taskPid = fork();
  4226. if (taskPid == 0)
  4227. {
  4228. bool stopToDet = false;
  4229. while(true)
  4230. {
  4231. if (!stopToDet)
  4232. {
  4233. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4234. {
  4235. if (chargingInfo[_index]->SystemStatus == SYS_MODE_UPDATE ||
  4236. chargingInfo[_index]->Type == 0x09)
  4237. {
  4238. stopToDet = true;
  4239. continue;
  4240. }
  4241. }
  4242. if (!stopToDet)
  4243. CheckSystemTaskAlive();
  4244. }
  4245. sleep(5);
  4246. }
  4247. }
  4248. }
  4249. void StartSystemTimeoutDet(unsigned char flag)
  4250. {
  4251. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  4252. {
  4253. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4254. }
  4255. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  4256. }
  4257. void StopSystemTimeoutDet()
  4258. {
  4259. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4260. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  4261. }
  4262. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  4263. {
  4264. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  4265. {
  4266. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  4267. {
  4268. GetTimespecFunc(&chargingInfo[gunIndex]->ConnectorTimeout);
  4269. }
  4270. chargingInfo[gunIndex]->TimeoutFlag = flag;
  4271. }
  4272. }
  4273. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  4274. {
  4275. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  4276. {
  4277. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  4278. }
  4279. }
  4280. void CheckConnectionTimeout()
  4281. {
  4282. if(system("pidof -s OcppBackend > /dev/null") != 0)
  4283. {
  4284. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4285. }
  4286. else
  4287. {
  4288. _connectionTimeout = _ocpp_get_connect_timeout();
  4289. if(_connectionTimeout <= 0)
  4290. {
  4291. _connectionTimeout = CONN_PLUG_TIME_OUT;
  4292. }
  4293. }
  4294. }
  4295. void GetOfflinePowerValue()
  4296. {
  4297. int value = atoi((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[OffLineMaxChargingPower].ItemData);//Unit: W
  4298. if (ShmDcCommonData->_offlinePower != value)
  4299. {
  4300. ShmDcCommonData->_offlinePower = value;
  4301. PRINTF_FUNC("OCPP offline .. Read OfflinePower = %d \n", ShmDcCommonData->_offlinePower);
  4302. }
  4303. }
  4304. void CreateTimeoutFork()
  4305. {
  4306. pid_t timeoutPid;
  4307. timeoutPid = fork();
  4308. if (timeoutPid > 0)
  4309. {
  4310. GetTimespecFunc(&_cmdSubPriority_time);
  4311. CheckConnectionTimeout();
  4312. int _timebuf = 0;
  4313. while(true)
  4314. {
  4315. _timebuf = GetTimeoutValue(&_cmdSubPriority_time);
  4316. if (_timebuf < 0)
  4317. {
  4318. GetTimespecFunc(&_cmdSubPriority_time);
  4319. }
  4320. else
  4321. {
  4322. if (_timebuf >= 5)
  4323. {
  4324. CheckConnectionTimeout();
  4325. //LW_OfflinePower
  4326. GetOfflinePowerValue();
  4327. GetTimespecFunc(&_cmdSubPriority_time);
  4328. }
  4329. }
  4330. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  4331. // 系統
  4332. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_None)
  4333. {
  4334. _timebuf = GetTimeoutValue(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4335. if (_timebuf < 0)
  4336. {
  4337. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  4338. }
  4339. else
  4340. {
  4341. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  4342. {
  4343. case Timeout_SelftestChk:
  4344. if (_timebuf >= SELFTEST_TIMEOUT)
  4345. {
  4346. _SelfTestTimeout();
  4347. StopSystemTimeoutDet();
  4348. }
  4349. break;
  4350. case Timeout_Authorizing:
  4351. if (_timebuf >= AUTHORIZE_TIMEOUT)
  4352. {
  4353. _AuthorizedTimeout();
  4354. StopSystemTimeoutDet();
  4355. }
  4356. break;
  4357. case Timeout_VerifyFail:
  4358. if (_timebuf >= AUTHORIZE_FAIL_TIMEOUT)
  4359. {
  4360. _AutoReturnTimeout();
  4361. StopSystemTimeoutDet();
  4362. }
  4363. break;
  4364. case Timeout_VerifyComp:
  4365. if (_timebuf >= AUTHORIZE_COMP_TIMEOUT)
  4366. {
  4367. _AutoReturnTimeout();
  4368. StopSystemTimeoutDet();
  4369. }
  4370. break;
  4371. case Timeout_WaitPlug:
  4372. if (_timebuf >= _connectionTimeout)
  4373. {
  4374. _DetectPlugInTimeout();
  4375. StopSystemTimeoutDet();
  4376. }
  4377. break;
  4378. case Timeout_ReturnToChargingGunDet:
  4379. {
  4380. if (_timebuf >= RETURN_TO_CHARGING_PAGE)
  4381. {
  4382. DisplayChargingInfo();
  4383. StopSystemTimeoutDet();
  4384. }
  4385. }
  4386. break;
  4387. // case Timeout_AuthorizingForStop:
  4388. // {
  4389. // if (_timebuf / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT)
  4390. // {
  4391. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  4392. // ClearAuthorizedFlag();
  4393. // StopSystemTimeoutDet();
  4394. // }
  4395. // }
  4396. // break;
  4397. }
  4398. }
  4399. }
  4400. // 各槍
  4401. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4402. {
  4403. if (chargingInfo[gun_index]->TimeoutFlag != Timeout_None)
  4404. {
  4405. _timebuf = GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout);
  4406. if (_timebuf < 0)
  4407. {
  4408. GetTimespecFunc(&chargingInfo[gun_index]->ConnectorTimeout);
  4409. }
  4410. else
  4411. {
  4412. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  4413. switch(chargingInfo[gun_index]->TimeoutFlag)
  4414. {
  4415. case Timeout_Preparing:
  4416. {
  4417. if (_timebuf >= GUN_PREPARE_TIMEOUT)
  4418. {
  4419. _PrepareTimeout(gun_index);
  4420. StopGunInfoTimeoutDet(gun_index);
  4421. }
  4422. }
  4423. break;
  4424. case Timeout_EvChargingDet:
  4425. {
  4426. if (_timebuf >= GUN_EV_WAIT_TIMEOUT)
  4427. {
  4428. _DetectEvChargingEnableTimeout(gun_index);
  4429. StopGunInfoTimeoutDet(gun_index);
  4430. }
  4431. }
  4432. break;
  4433. case Timeout_EvseChargingDet:
  4434. {
  4435. if (chargingInfo[gun_index]->Type == _Type_GB)
  4436. {
  4437. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT * 3)
  4438. {
  4439. _DetectEvseChargingEnableTimeout(gun_index);
  4440. StopGunInfoTimeoutDet(gun_index);
  4441. }
  4442. }
  4443. else
  4444. {
  4445. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT)
  4446. {
  4447. _DetectEvseChargingEnableTimeout(gun_index);
  4448. StopGunInfoTimeoutDet(gun_index);
  4449. }
  4450. }
  4451. }
  4452. break;
  4453. case Timeout_EvseCompleteDet:
  4454. {
  4455. if (_timebuf >= GUN_COMP_WAIT_TIMEOUT)
  4456. {
  4457. StopGunInfoTimeoutDet(gun_index);
  4458. }
  4459. }
  4460. break;
  4461. case Timeout_ForCcsPrechargeDet:
  4462. {
  4463. if (_timebuf >= GUN_PRECHARGING_TIMEOUT)
  4464. {
  4465. _CcsPrechargeTimeout(gun_index);
  4466. StopGunInfoTimeoutDet(gun_index);
  4467. }
  4468. }
  4469. break;
  4470. }
  4471. }
  4472. }
  4473. }
  4474. sleep(1);
  4475. }
  4476. }
  4477. }
  4478. void GetSystemTime()
  4479. {
  4480. struct timeb csuTime;
  4481. struct tm *tmCSU;
  4482. ftime(&csuTime);
  4483. tmCSU = localtime(&csuTime.time);
  4484. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  4485. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4486. tmCSU->tm_sec);
  4487. // byte date[14];
  4488. //
  4489. //
  4490. // //sprintf(&date, "%d", );
  4491. //
  4492. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  4493. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  4494. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  4495. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  4496. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  4497. //
  4498. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  4499. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  4500. //
  4501. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  4502. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  4503. //
  4504. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  4505. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  4506. //
  4507. // date[10] = (tmCSU->tm_min) / 10 % 10;
  4508. // date[11] = (tmCSU->tm_min) / 1 % 10;
  4509. //
  4510. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  4511. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  4512. // 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],
  4513. // date[4], date[5], date[6], date[7],
  4514. // date[8], date[9], date[10], date[11],
  4515. // date[12], date[13]);
  4516. }
  4517. void CheckFactoryConfigFunction()
  4518. {
  4519. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4520. {
  4521. char Buf[256];
  4522. sleep(5);
  4523. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  4524. ShmSysConfigAndInfo->SysConfig.ModelName,
  4525. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  4526. system(Buf);
  4527. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4528. system("sync");
  4529. sleep(5);
  4530. system("reboot -f");
  4531. sleep(5);
  4532. system("reboot -f");
  4533. }
  4534. }
  4535. void CheckFwUpdateFunction()
  4536. {
  4537. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  4538. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  4539. {
  4540. DEBUG_INFO_MSG("ftp : update start. \n");
  4541. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4542. {
  4543. SetChargerMode(gun_index, SYS_MODE_UPDATE);
  4544. }
  4545. CloseWatchDog();
  4546. sleep(2);
  4547. KillTask();
  4548. byte updateResult = CheckUpdateProcess();
  4549. if (updateResult == PASS)
  4550. DEBUG_INFO_MSG("ftp : update complete. \n");
  4551. else if (updateResult == MODELNAME_FAIL)
  4552. {
  4553. DEBUG_INFO_MSG("ftp : model name is none match. \n");
  4554. KillAllTask();
  4555. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4556. sleep(5);
  4557. system("/usr/bin/run_evse_restart.sh");
  4558. return;
  4559. }
  4560. else
  4561. DEBUG_INFO_MSG("ftp : update fail. \n");
  4562. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4563. sleep(5);
  4564. system("reboot -f");
  4565. }
  4566. else
  4567. {
  4568. ocpp_chk_update_cmd();
  4569. }
  4570. }
  4571. int TariffParsing(char *StringItem, char *TariffCode)
  4572. {
  4573. char *ptrSave, *ptrToken;
  4574. char strSource[128];
  4575. int fee = 0;
  4576. memcpy(strSource, StringItem, 128);
  4577. ptrToken = strtok_r(strSource, ";", &ptrSave);
  4578. while(ptrToken != NULL)
  4579. {
  4580. char *strTariff = strstr(ptrToken, TariffCode);
  4581. if(strTariff != NULL)
  4582. {
  4583. char *strFee = strstr(strTariff, "$");
  4584. if(strFee != NULL)
  4585. {
  4586. strFee++;
  4587. float fFee = atof(strFee);
  4588. fee = (int)(fFee * 100);
  4589. break;
  4590. }
  4591. }
  4592. ptrToken = strtok_r(NULL, ";", &ptrSave);
  4593. }
  4594. return fee;
  4595. }
  4596. void DefaultPriceHandlerOcpp16(char *StrDefaultPrice)
  4597. {
  4598. unsigned int connectionFee = 0, currentRate = 0, occupancyFee = 0;
  4599. PRINTF_FUNC("DefaultPriceHandler - StrDefaultPrice = %s \n", StrDefaultPrice);
  4600. connectionFee = TariffParsing(StrDefaultPrice, "Connection Fee");
  4601. currentRate = TariffParsing(StrDefaultPrice, "Current Rate");
  4602. occupancyFee = TariffParsing(StrDefaultPrice, "Occupancy Fee");
  4603. PRINTF_FUNC("*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100));
  4604. PRINTF_FUNC("*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100));
  4605. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100));
  4606. ShmDcCommonData->balanceInfo.defaultPrice = currentRate;
  4607. }
  4608. void UserPriceHandlerOcpp16(char *UserId, char *UserPrice)
  4609. {
  4610. unsigned int connectionFee = 0, currentRate = 0, occupancyFee = 0;
  4611. int accountBalance = 0;
  4612. byte acGunIndex = 0;
  4613. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  4614. {
  4615. if (gunIndex == 1)
  4616. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4617. else
  4618. acGunIndex = 0;
  4619. if((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4620. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) &&
  4621. strcmp((char *)chargingInfo[gunIndex]->StartUserId, UserId) == EQUAL)
  4622. {
  4623. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Connection Fee");
  4624. currentRate = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Current Rate");
  4625. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Occupancy Fee");
  4626. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Account Balance");
  4627. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].UserPrice != currentRate ||
  4628. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4629. {
  4630. PRINTF_FUNC("*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100));
  4631. PRINTF_FUNC("*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100));
  4632. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100));
  4633. PRINTF_FUNC("*** Account Balance: %d.%02d ***", (accountBalance / 100), (accountBalance % 100));
  4634. PRINTF_FUNC("*** Connector Id %d User Price %d.%02d, Account Balance %d.%02d ***",
  4635. (gunIndex), (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100));
  4636. }
  4637. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].UserPrice = currentRate;
  4638. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance = accountBalance;
  4639. }
  4640. }
  4641. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex++)
  4642. {
  4643. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4644. if(strcmp((char *)ac_chargingInfo[gunIndex]->StartUserId, UserId) == EQUAL)
  4645. {
  4646. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].UserPrice != currentRate ||
  4647. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance != accountBalance)
  4648. {
  4649. PRINTF_FUNC("*** AC Connector User Price %d.%02d, Account Balance %d.%02d ***",
  4650. (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100));
  4651. }
  4652. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].UserPrice = currentRate;
  4653. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance = accountBalance;
  4654. }
  4655. }
  4656. }
  4657. void RunningFinalCostHandlerOcpp16()
  4658. {
  4659. unsigned int connectionFee = 0, sessionFee = 0, occupancyFee = 0, totalCost = 0;
  4660. int accountBalance = 0;
  4661. byte acGunIndex = 0;
  4662. bool isFind = false;
  4663. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  4664. {
  4665. isFind = false;
  4666. if (gunIndex == 1)
  4667. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4668. else
  4669. acGunIndex = 0;
  4670. // PRINTF_FUNC("*** gunIndex = %d, description = %s *** \n", gunIndex + acGunIndex,
  4671. // ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description);
  4672. // PRINTF_FUNC("*** gunIndex = %d, f description = %s *** \n", gunIndex + acGunIndex,
  4673. // ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description);
  4674. if (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4675. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4676. {
  4677. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description) > 0)
  4678. {
  4679. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Connection Fee");
  4680. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Session Fee");
  4681. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Occupancy Fee");
  4682. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Total Cost");
  4683. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Account Balance");
  4684. PRINTF_FUNC("*** Connector Id: %d Running Cost *** \n", gunIndex);
  4685. isFind = true;
  4686. memset(&ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex], 0x00, sizeof(struct StrcutRunningCost));
  4687. }
  4688. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description) > 0)
  4689. {
  4690. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Connection Fee");
  4691. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Session Fee");
  4692. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Occupancy Fee");
  4693. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Total Cost");
  4694. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Account Balance");
  4695. PRINTF_FUNC("********** Connector Id: %d Final Cost ********** \n", gunIndex);
  4696. isFind = true;
  4697. memset(&ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex], 0x00, sizeof(struct StrcutFinalCost));
  4698. }
  4699. if (isFind)
  4700. {
  4701. PRINTF_FUNC("*** Connection Fee: %d.%02d *** \n", (connectionFee / 100), (connectionFee % 100));
  4702. PRINTF_FUNC("*** Session Fee: %d.%02d *** \n", (sessionFee / 100), (sessionFee % 100));
  4703. PRINTF_FUNC("*** Occupancy Fee: %d.%02d *** \n", (occupancyFee / 100), (occupancyFee % 100));
  4704. PRINTF_FUNC("*** Total Cost: %d.%02d *** \n", (totalCost / 100), (totalCost % 100));
  4705. PRINTF_FUNC("*** Account Balance: %d.%02d *** \n", (accountBalance / 100), (accountBalance % 100));
  4706. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost != totalCost ||
  4707. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4708. {
  4709. PRINTF_FUNC("*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d *** \n",
  4710. (gunIndex), (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100));
  4711. }
  4712. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost = totalCost;
  4713. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance = accountBalance;
  4714. }
  4715. }
  4716. }
  4717. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex++)
  4718. {
  4719. isFind = false;
  4720. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4721. if (ac_chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_PREPARING &&
  4722. ac_chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4723. {
  4724. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description) > 0)
  4725. {
  4726. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Connection Fee");
  4727. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Session Fee");
  4728. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Occupancy Fee");
  4729. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Total Cost");
  4730. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Account Balance");
  4731. PRINTF_FUNC("*** AC Connector : Running Cost ***\n");
  4732. isFind = true;
  4733. memset(&ShmOCPP16Data->Cost.RunningCost[acGunIndex], 0x00, sizeof(struct StrcutRunningCost));
  4734. }
  4735. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description) > 0)
  4736. {
  4737. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Connection Fee");
  4738. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Session Fee");
  4739. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Occupancy Fee");
  4740. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Total Cost");
  4741. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Account Balance");
  4742. PRINTF_FUNC("*** AC Connector : Final Cost ***\n");
  4743. isFind = true;
  4744. memset(&ShmOCPP16Data->Cost.FinalCost[acGunIndex], 0x00, sizeof(struct StrcutFinalCost));
  4745. }
  4746. if (isFind)
  4747. {
  4748. PRINTF_FUNC("*** Connection Fee: %d.%02d ***\n", (connectionFee / 100), (connectionFee % 100));
  4749. PRINTF_FUNC("*** Session Fee: %d.%02d ***\n", (sessionFee / 100), (sessionFee % 100));
  4750. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***\n", (occupancyFee / 100), (occupancyFee % 100));
  4751. PRINTF_FUNC("*** Total Cost: %d.%02d ***\n", (totalCost / 100), (totalCost % 100));
  4752. PRINTF_FUNC("*** Account Balance: %d.%02d ***\n", (accountBalance / 100), (accountBalance % 100));
  4753. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost != totalCost ||
  4754. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4755. {
  4756. PRINTF_FUNC("*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d ***\n",
  4757. (gunIndex), (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100));
  4758. }
  4759. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].TotalCost = totalCost;
  4760. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance = accountBalance;
  4761. }
  4762. }
  4763. }
  4764. }
  4765. //===============================================
  4766. // ocpp key
  4767. //===============================================
  4768. bool ocpp_authorizeRemoteTxReqChk_key()
  4769. {
  4770. bool result = false;
  4771. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4772. {
  4773. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
  4774. result = true;
  4775. }
  4776. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4777. {
  4778. }
  4779. return result;
  4780. }
  4781. //===============================================
  4782. // Check reservation date is expired
  4783. //===============================================
  4784. int opcc_chk_reserve_expired(byte gun_index)
  4785. {
  4786. int result = NO;
  4787. byte acDirIndex = 0;
  4788. if (gun_index == 1)
  4789. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4790. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4791. {
  4792. if(chargingInfo[gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  4793. PRINTF_FUNC("%s \n", ShmOCPP16Data->ReserveNow[gun_index + acDirIndex].ExpiryDate);
  4794. if (CheckTimeOut(ShmOCPP16Data->ReserveNow[gun_index + acDirIndex].ExpiryDate))
  4795. result = YES;
  4796. }
  4797. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4798. {
  4799. if(chargingInfo[gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  4800. PRINTF_FUNC("%s \n", ShmOCPP20Data->ReserveNow[gun_index + acDirIndex].expiryDateTime);
  4801. if (CheckTimeOut(ShmOCPP20Data->ReserveNow[gun_index + acDirIndex].expiryDateTime))
  4802. result = YES;
  4803. }
  4804. return result;
  4805. }
  4806. //===============================================
  4807. // AC OCPP routine
  4808. //===============================================
  4809. void ocpp_ac_chk_availability_cmd()
  4810. {
  4811. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4812. byte type = 0;
  4813. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  4814. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4815. {
  4816. if (ShmOCPP16Data->CsMsg.bits[hasDc].ChangeAvailabilityReq == YES)
  4817. {
  4818. PRINTF_FUNC("***************ChkOcppStatus : AC OcppChangeAvailability******************** \n");
  4819. DEBUG_ERROR_MSG("***************ChkOcppStatus : AC OcppChangeAvailability******************** \n");
  4820. ShmOCPP16Data->CsMsg.bits[hasDc].ChangeAvailabilityReq = NO;
  4821. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[hasDc].Type, "Operative") == EQUAL)
  4822. {
  4823. if (isDb_ready)
  4824. DB_Update_Operactive(localDb, hasDc, true);
  4825. type = 1;
  4826. }
  4827. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[hasDc].Type, "Inoperative") == EQUAL)
  4828. {
  4829. if (isDb_ready)
  4830. DB_Update_Operactive(localDb, hasDc, false);
  4831. type = 2;
  4832. }
  4833. }
  4834. }
  4835. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4836. {
  4837. if (ShmOCPP20Data->CsMsg.bits[hasDc].ChangeAvailabilityReq == YES)
  4838. {
  4839. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4840. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4841. ShmOCPP20Data->CsMsg.bits[hasDc].ChangeAvailabilityReq = NO;
  4842. if(strcmp((char *)ShmOCPP20Data->ChangeAvailability[hasDc].operationalStatus, "Operative") == EQUAL)
  4843. {
  4844. if (isDb_ready)
  4845. DB_Update_Operactive(localDb, hasDc, true);
  4846. type = 1;
  4847. }
  4848. else if (strcmp((char *)ShmOCPP20Data->ChangeAvailability[hasDc].operationalStatus, "Inoperative") == EQUAL)
  4849. {
  4850. if (isDb_ready)
  4851. DB_Update_Operactive(localDb, hasDc, false);
  4852. type = 2;
  4853. }
  4854. }
  4855. }
  4856. if (type == 1)
  4857. {
  4858. ac_chargingInfo[0]->IsAvailable = YES;
  4859. }
  4860. else if (type == 2)
  4861. {
  4862. ac_chargingInfo[0]->IsAvailable = NO;
  4863. }
  4864. }
  4865. void ocpp_ac_chk_unlock_cmd()
  4866. {
  4867. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4868. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  4869. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4870. {
  4871. if (ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorReq == YES)
  4872. {
  4873. ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorReq = NO;
  4874. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4875. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  4876. {
  4877. // Unlocked - 充電中,需停止充電
  4878. strcpy((char *)ShmOCPP16Data->StopTransaction[hasDc].StopReason, "UnlockCommand");
  4879. ac_chargingInfo[0]->StopChargeFlag = YES;
  4880. }
  4881. strcpy((char *)ShmOCPP16Data->UnlockConnector[hasDc].ResponseStatus, "Unlocked");
  4882. ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorConf = YES;
  4883. }
  4884. }
  4885. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4886. {
  4887. if (ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorReq == YES)
  4888. {
  4889. ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorReq = NO;
  4890. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4891. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  4892. {
  4893. // Unlocked - 充電中,需停止充電
  4894. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].transactionInfo.stoppedReason, "UnlockCommand");
  4895. ac_chargingInfo[0]->StopChargeFlag = YES;
  4896. }
  4897. strcpy((char*)ShmOCPP20Data->UnlockConnector[hasDc].Response_status, "Unlocked");
  4898. ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorConf = YES;
  4899. }
  4900. }
  4901. }
  4902. void ocpp_ac_chk_profileConf_cmd()
  4903. {
  4904. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4905. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4906. {
  4907. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf == YES)
  4908. {
  4909. ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf = NO;
  4910. }
  4911. }
  4912. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4913. {
  4914. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf == YES)
  4915. {
  4916. ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf = NO;
  4917. }
  4918. }
  4919. }
  4920. void ocpp_ac_chargingProfile_process()
  4921. {
  4922. int _time = 0;
  4923. int _startCount = NO_DEFINE;
  4924. int _maxCount = 0;
  4925. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4926. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4927. {
  4928. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingProfileKind, "Absolute") == EQUAL &&
  4929. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingProfileId == YES)
  4930. {
  4931. _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.StartSchedule);
  4932. if (_time > -1)
  4933. {
  4934. _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod);
  4935. _startCount = NO_DEFINE;
  4936. for (byte _count = 0; _count < _maxCount; _count++)
  4937. {
  4938. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4939. if (_time >= ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  4940. {
  4941. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  4942. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  4943. {
  4944. _startCount = _count;
  4945. }
  4946. }
  4947. }
  4948. if (_startCount < _maxCount)
  4949. {
  4950. //PRINTF_FUNC("AC - Gun_%d, Get Profile - Profile Index = %d \n", hasDc, _startCount);
  4951. //PRINTF_FUNC("Profile Limit = %.2f \n", ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  4952. if (ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit >= 1 &&
  4953. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit <= 5)
  4954. {
  4955. ac_chargingInfo[0]->ChargingProfileCurrent = AC_MINIMUM_DUTY;
  4956. }
  4957. else if (ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit == 0)
  4958. {
  4959. ac_chargingInfo[0]->ChargingProfileCurrent = AC_STOP_DUTY;
  4960. }
  4961. else
  4962. {
  4963. ac_chargingInfo[0]->ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit;
  4964. }
  4965. }
  4966. else
  4967. {
  4968. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4969. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4970. }
  4971. }
  4972. else
  4973. {
  4974. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4975. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4976. }
  4977. }
  4978. else
  4979. {
  4980. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4981. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4982. }
  4983. }
  4984. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4985. {
  4986. if (strcmp((char *)ShmOCPP20Data->SmartChargingProfile[hasDc].chargingProfileKind, "Absolute") == EQUAL &&
  4987. ShmOCPP20Data->SmartChargingProfile[hasDc].id == YES)
  4988. {
  4989. _time = GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].startSchedule);
  4990. _maxCount = ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod);
  4991. _startCount = NO_DEFINE;
  4992. for (byte _count = 0; _count < _maxCount; _count++)
  4993. {
  4994. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4995. if (_time >= ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].startPeriod)
  4996. {
  4997. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].limit >= MIN_OUTPUT_CUR) ||
  4998. ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].limit > MIN_OUTPUT_CUR)
  4999. {
  5000. _startCount = _count;
  5001. }
  5002. }
  5003. }
  5004. if (_startCount < _maxCount)
  5005. {
  5006. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", 1, _startCount);
  5007. //PRINTF_FUNC("Profile Limit = %.2f \n", ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit);
  5008. if (ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit >= 1 &&
  5009. ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit <= 5)
  5010. {
  5011. ac_chargingInfo [0]->ChargingProfileCurrent = AC_MINIMUM_DUTY;
  5012. }
  5013. else if (ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit == 0)
  5014. {
  5015. ac_chargingInfo [0]->ChargingProfileCurrent = AC_STOP_DUTY;
  5016. }
  5017. else
  5018. {
  5019. ac_chargingInfo [0]->ChargingProfileCurrent = ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit;
  5020. }
  5021. }
  5022. else
  5023. {
  5024. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5025. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5026. }
  5027. }
  5028. else
  5029. {
  5030. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5031. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5032. }
  5033. }
  5034. else
  5035. {
  5036. ac_chargingInfo[0]->ChargingProfilePower = -1;
  5037. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  5038. }
  5039. if (ac_chargingInfo[0]->ChargingProfileCurrent >= 0 &&
  5040. _AcChargingProfileCurBuffer != ac_chargingInfo[0]->ChargingProfileCurrent)
  5041. {
  5042. _AcChargingProfileCurBuffer = ac_chargingInfo[0]->ChargingProfileCurrent;
  5043. PRINTF_FUNC ( "AC Profile (duty) : ChargingProfileCurrent = %.2f \n", ac_chargingInfo[0]->ChargingProfileCurrent);
  5044. }
  5045. }
  5046. void ocpp_ac_lift_profileReq_cmd(byte gunIndex)
  5047. {
  5048. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5049. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5050. {
  5051. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf == NO)
  5052. {
  5053. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileReq == NO)
  5054. ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileReq = YES;
  5055. }
  5056. }
  5057. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5058. {
  5059. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf == NO)
  5060. {
  5061. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileReq == NO)
  5062. ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileReq = YES;
  5063. }
  5064. }
  5065. }
  5066. int opcc_ac_chk_reserve_expired()
  5067. {
  5068. int result = NO;
  5069. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5070. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5071. {
  5072. if(ac_chargingInfo [0]->SystemStatus != SYS_MODE_RESERVATION)
  5073. PRINTF_FUNC("%s \n", ShmOCPP16Data->ReserveNow[hasDc].ExpiryDate);
  5074. if (CheckTimeOut(ShmOCPP16Data->ReserveNow[hasDc].ExpiryDate))
  5075. result = YES;
  5076. }
  5077. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5078. {
  5079. if(ac_chargingInfo [0]->SystemStatus != SYS_MODE_RESERVATION)
  5080. PRINTF_FUNC("%s \n", ShmOCPP20Data->ReserveNow[hasDc].expiryDateTime);
  5081. if (CheckTimeOut(ShmOCPP20Data->ReserveNow[hasDc].expiryDateTime))
  5082. result = YES;
  5083. }
  5084. return result;
  5085. }
  5086. void ocpp_ac_chk_reserved_cmd()
  5087. {
  5088. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  5089. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5090. {
  5091. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE &&
  5092. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowReq == YES)
  5093. {
  5094. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5095. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowReq = NO;
  5096. if ( ! opcc_ac_chk_reserve_expired ())
  5097. {
  5098. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP16Data->ReserveNow [hasDc].ReservationId );
  5099. PRINTF_FUNC ( "IdTag = %s \n", ShmOCPP16Data->ReserveNow [hasDc].IdTag );
  5100. PRINTF_FUNC ( "*************ChkOcppStatus : (AC) OcppReservedStatus End****************** \n" );
  5101. ac_chargingInfo [0]->ReservationId = ShmOCPP16Data->ReserveNow [hasDc].ReservationId;
  5102. ac_chargingInfo [0]->SystemStatus = SYS_MODE_RESERVATION;
  5103. strcpy ( (char *) ShmDcCommonData->_reserved_ac_UserId[0],
  5104. (char *) ShmOCPP16Data->ReserveNow [hasDc].IdTag );
  5105. }
  5106. ShmOCPP16Data->CsMsg.bits [hasDc].ReserveNowConf = YES;
  5107. }
  5108. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_RESERVATION &&
  5109. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationReq == YES)
  5110. {
  5111. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationReq = NO;
  5112. if (ShmOCPP16Data->ReserveNow [hasDc].ReservationId == ac_chargingInfo [0]->ReservationId)
  5113. {
  5114. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5115. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP16Data->ReserveNow [hasDc].ReservationId );
  5116. PRINTF_FUNC ( "***********ChkOcppStatus : (AC) Cancel OcppReservedStatus End**************** \n" );
  5117. ac_chargingInfo [0]->ReservationId = NO_RESERVE;
  5118. ac_chargingInfo [0]->SystemStatus = SYS_MODE_IDLE;
  5119. strcpy ( (char *) ShmDcCommonData->_reserved_ac_UserId[0], "" );
  5120. ShmOCPP16Data->CsMsg.bits [hasDc].CancelReservationConf = YES;
  5121. }
  5122. }
  5123. }
  5124. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5125. {
  5126. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_IDLE &&
  5127. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowReq == YES)
  5128. {
  5129. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5130. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowReq = NO;
  5131. if ( ! opcc_ac_chk_reserve_expired ())
  5132. {
  5133. PRINTF_FUNC ( "id = %d \n", ShmOCPP20Data->ReserveNow [hasDc].id );
  5134. PRINTF_FUNC ( "idToken = %s \n", ShmOCPP20Data->ReserveNow [hasDc].idToken.idToken );
  5135. PRINTF_FUNC ( "*************ChkOcppStatus : (AC) OcppReservedStatus End****************** \n" );
  5136. ac_chargingInfo [0]->ReservationId = ShmOCPP20Data->ReserveNow [hasDc].id;
  5137. ac_chargingInfo [0]->SystemStatus = SYS_MODE_RESERVATION;
  5138. strncpy ( (char *) ShmDcCommonData->_reserved_UserId,
  5139. (char *) ShmOCPP20Data->ReserveNow [hasDc].idToken.idToken, 30 );
  5140. }
  5141. ShmOCPP20Data->CsMsg.bits [hasDc].ReserveNowConf = YES;
  5142. }
  5143. if (ac_chargingInfo [0]->SystemStatus == SYS_MODE_RESERVATION &&
  5144. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationReq == YES)
  5145. {
  5146. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationReq = NO;
  5147. if (ShmOCPP20Data->ReserveNow [hasDc].id == ac_chargingInfo [0]->ReservationId)
  5148. {
  5149. PRINTF_FUNC ( "***************ChkOcppStatus : (AC) OcppReservedStatus******************** \n" );
  5150. PRINTF_FUNC ( "ReservationId = %d \n", ShmOCPP20Data->ReserveNow [hasDc].id );
  5151. PRINTF_FUNC ( "***********ChkOcppStatus : (AC) Cancel OcppReservedStatus End**************** \n" );
  5152. ac_chargingInfo [0]->ReservationId = NO_RESERVE;
  5153. ac_chargingInfo [0]->SystemStatus = SYS_MODE_IDLE;
  5154. strcpy ( (char *) ShmDcCommonData->_reserved_UserId, "" );
  5155. ShmOCPP20Data->CsMsg.bits [hasDc].CancelReservationConf = YES;
  5156. }
  5157. }
  5158. }
  5159. }
  5160. //===============================================
  5161. // OCPP routine
  5162. //===============================================
  5163. void ocpp_process_start()
  5164. {
  5165. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  5166. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  5167. {
  5168. if(system("pidof -s OcppBackend > /dev/null") != 0)
  5169. {
  5170. DEBUG_ERROR_MSG("OcppBackend not running, restart it.\r\n");
  5171. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5172. system("/root/OcppBackend &");
  5173. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5174. system("/root/OcppBackend20 &");
  5175. }
  5176. else
  5177. {
  5178. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5179. {
  5180. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  5181. {
  5182. DEBUG_ERROR_MSG("OcppBackend watch dog timeout task restart.\n");
  5183. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  5184. system("pkill OcppBackend");
  5185. }
  5186. }
  5187. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5188. {
  5189. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  5190. {
  5191. DEBUG_ERROR_MSG("OcppBackend20 watch dog timeout task restart.\n");
  5192. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  5193. system("pkill OcppBackend20");
  5194. }
  5195. }
  5196. }
  5197. }
  5198. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  5199. {
  5200. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  5201. {
  5202. DEBUG_ERROR_MSG("OcppBackendPH not running, restart it.\r\n");
  5203. system("/root/OcppBackendPH &");
  5204. }
  5205. }
  5206. }
  5207. void ocpp_auto_response_BootNotification()
  5208. {
  5209. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5210. {
  5211. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  5212. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  5213. }
  5214. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5215. {
  5216. if (ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES)
  5217. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = NO;
  5218. }
  5219. }
  5220. bool ocpp_is_resPass_StartTransactionConf(byte gun_index)
  5221. {
  5222. bool result = false;
  5223. byte acDirIndex = 0;
  5224. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5225. if (gun_index == 1)
  5226. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5227. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5228. {
  5229. result = ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf;
  5230. ShmDcCommonData->startTransactionID[gun_index] = ShmOCPP16Data->StartTransaction[gun_index + acDirIndex].ResponseTransactionId;
  5231. }
  5232. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5233. {
  5234. result = ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf;
  5235. }
  5236. return result;
  5237. }
  5238. void ocpp_auto_response_StartTransationConf(byte gun_index)
  5239. {
  5240. byte acDirIndex = 0;
  5241. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5242. if (gun_index == 1)
  5243. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5244. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5245. {
  5246. if (ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf)
  5247. ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf = NO;
  5248. }
  5249. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5250. {
  5251. if (ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf)
  5252. ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf = NO;
  5253. }
  5254. }
  5255. void ocpp_auto_response_StopTransationConf(byte gun_index)
  5256. {
  5257. byte acDirIndex = 0;
  5258. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5259. if (gun_index == 1)
  5260. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5261. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5262. {
  5263. if(ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf)
  5264. ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf = NO;
  5265. }
  5266. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5267. {
  5268. }
  5269. }
  5270. void ocpp_auto_response_StopTransationConfPH(byte gun_index)
  5271. {
  5272. byte acDirIndex = 0;
  5273. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5274. if (gun_index == 1)
  5275. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5276. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  5277. {
  5278. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5279. {
  5280. if(ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf)
  5281. ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StopTransactionConf = NO;
  5282. }
  5283. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5284. {
  5285. }
  5286. }
  5287. }
  5288. void ocpp_copy_stopReason2maintainServer(byte gun_index)
  5289. {
  5290. byte acDirIndex = 0;
  5291. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5292. if (gun_index == 1)
  5293. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5294. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  5295. {
  5296. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index + acDirIndex].StopReason, ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason));
  5297. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index + acDirIndex].IdTag, ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].IdTag));
  5298. }
  5299. }
  5300. void ocpp_auto_response_StartTransationConfPH(byte gun_index)
  5301. {
  5302. byte acDirIndex = 0;
  5303. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5304. if (gun_index == 1)
  5305. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5306. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  5307. {
  5308. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5309. {
  5310. if (ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf)
  5311. ShmOCPP16DataPH->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf = NO;
  5312. }
  5313. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5314. {
  5315. }
  5316. }
  5317. }
  5318. void ocpp_auto_response_ReserveConf(byte gun_index)
  5319. {
  5320. byte acDirIndex = 0;
  5321. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5322. if (gun_index == 1)
  5323. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5324. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5325. ShmOCPP16Data->CsMsg.bits[gun_index + acDirIndex].ReserveNowConf = YES;
  5326. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5327. ShmOCPP20Data->CsMsg.bits[gun_index + acDirIndex].ReserveNowConf = YES;
  5328. }
  5329. bool ocpp_chk_authrization_cmd()
  5330. {
  5331. char buf2[16] = "";
  5332. memset(buf2, 0, ARRAY_SIZE(buf2));
  5333. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5334. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  5335. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5336. sprintf(buf2, "%s", ShmOCPP20Data->Authorize.Response_idTokenInfo.status);
  5337. // 因為無法得知實際的長度,所以只能用搜尋的方式
  5338. if(strcmp(buf2, "Accepted") == EQUAL)
  5339. return true;
  5340. return false;
  5341. }
  5342. bool opcc_sub_get_reset_req()
  5343. {
  5344. bool result = false;
  5345. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5346. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  5347. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5348. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  5349. return result;
  5350. }
  5351. void ocpp_sub_run_reset(bool canReset)
  5352. {
  5353. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5354. {
  5355. if(canReset && strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  5356. {
  5357. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  5358. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  5359. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  5360. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  5361. sleep(3);
  5362. system("reboot -f");
  5363. }
  5364. else if (canReset && strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  5365. {
  5366. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  5367. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  5368. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  5369. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  5370. sleep(3);
  5371. KillAllTask();
  5372. CloseWatchDog();
  5373. system("/usr/bin/run_evse_restart.sh");
  5374. }
  5375. }
  5376. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5377. {
  5378. if(canReset && strcmp((char *)ShmOCPP20Data->Reset.type, "Immediate") == EQUAL)
  5379. {
  5380. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  5381. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  5382. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  5383. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  5384. sleep(3);
  5385. system("reboot -f");
  5386. }
  5387. else if (canReset && strcmp((char *)ShmOCPP20Data->Reset.type, "OnIdle") == EQUAL)
  5388. {
  5389. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  5390. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  5391. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  5392. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  5393. sleep(3);
  5394. KillAllTask();
  5395. CloseWatchDog();
  5396. system("/usr/bin/run_evse_restart.sh");
  5397. }
  5398. }
  5399. }
  5400. void ocpp_chk_reset_cmd(byte isforce)
  5401. {
  5402. if (opcc_sub_get_reset_req())
  5403. {
  5404. bool canReset = true;
  5405. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  5406. {
  5407. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5408. {
  5409. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5410. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  5411. {
  5412. // Reset - AC 先停止充電
  5413. ac_chargingInfo[0]->StopChargeFlag = YES;
  5414. GetTimespecFunc(&_resetChkTime);
  5415. }
  5416. }
  5417. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5418. {
  5419. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_REASSIGN &&
  5420. chargingInfo[_index]->SystemStatus <= SYS_MODE_TERMINATING)
  5421. {
  5422. canReset = false;
  5423. // Reset 系統先停止充電
  5424. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5425. {
  5426. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  5427. ocpp_set_stopReason_by_cmd(_index, "HardReset");
  5428. else
  5429. ocpp_set_stopReason_by_cmd(_index, "SoftReset");
  5430. }
  5431. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5432. {
  5433. if(strcmp((char *)ShmOCPP20Data->Reset.type, "Immediate") == EQUAL)
  5434. ocpp_set_stopReason_by_cmd(_index, "HardReset");
  5435. else
  5436. ocpp_set_stopReason_by_cmd(_index, "SoftReset");
  5437. }
  5438. ChargingTerminalProcess(_index);
  5439. GetTimespecFunc(&_resetChkTime);
  5440. }
  5441. }
  5442. }
  5443. if (canReset)
  5444. {
  5445. if (GetTimeoutValue(&_resetChkTime) < 0)
  5446. GetTimespecFunc(&_resetChkTime);
  5447. if (GetTimeoutValue(&_resetChkTime) >= 5)
  5448. {
  5449. ShmDcCommonData->evBoardResetFlag = YES;
  5450. // Reset -> change status to maintain.
  5451. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5452. {
  5453. SetChargerMode(_index, SYS_MODE_MAINTAIN);
  5454. }
  5455. ChangeLcmByIndex(_LCM_FIX);
  5456. }
  5457. else
  5458. canReset = false;
  5459. }
  5460. else
  5461. GetTimespecFunc(&_resetChkTime);
  5462. if (isforce == YES)
  5463. canReset = true;
  5464. ocpp_sub_run_reset(canReset);
  5465. }
  5466. }
  5467. void ocpp_chk_reserved_cmd(byte gunIndex)
  5468. {
  5469. byte acDirIndex = 0;
  5470. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5471. if (gunIndex == 1)
  5472. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5473. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5474. {
  5475. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE &&
  5476. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq == YES)
  5477. {
  5478. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5479. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq = NO;
  5480. if (!opcc_chk_reserve_expired(gunIndex))
  5481. {
  5482. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId);
  5483. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].IdTag);
  5484. PRINTF_FUNC("*************ChkOcppStatus : OcppReservedStatus End****************** \n");
  5485. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  5486. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  5487. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex],
  5488. (char *)ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].IdTag);
  5489. }
  5490. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowConf = YES;
  5491. }
  5492. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION &&
  5493. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq == YES)
  5494. {
  5495. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq = NO;
  5496. if (ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId == chargingInfo[gunIndex]->ReservationId)
  5497. {
  5498. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5499. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId);
  5500. PRINTF_FUNC("***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n");
  5501. chargingInfo[gunIndex]->ReservationId = NO_RESERVE;
  5502. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_IDLE;
  5503. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex], "");
  5504. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationConf = YES;
  5505. }
  5506. }
  5507. }
  5508. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5509. {
  5510. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE &&
  5511. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq == YES)
  5512. {
  5513. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5514. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq = NO;
  5515. if (!opcc_chk_reserve_expired(gunIndex))
  5516. {
  5517. PRINTF_FUNC("id = %d \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id);
  5518. PRINTF_FUNC("idToken = %s \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].idToken.idToken);
  5519. PRINTF_FUNC("*************ChkOcppStatus : OcppReservedStatus End****************** \n");
  5520. chargingInfo[gunIndex]->ReservationId = ShmOCPP20Data->ReserveNow[gunIndex].id;
  5521. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  5522. strncpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex],
  5523. (char *)ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].idToken.idToken, 30);
  5524. }
  5525. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowConf = YES;
  5526. }
  5527. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION &&
  5528. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq == YES)
  5529. {
  5530. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq = NO;
  5531. if (ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id == chargingInfo[gunIndex]->ReservationId)
  5532. {
  5533. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  5534. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id);
  5535. PRINTF_FUNC("***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n");
  5536. chargingInfo[gunIndex]->ReservationId = NO_RESERVE;
  5537. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_IDLE;
  5538. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex], "");
  5539. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationConf = YES;
  5540. }
  5541. }
  5542. }
  5543. }
  5544. void ocpp_chk_availability_cmd(byte gunIndex)
  5545. {
  5546. byte type = 0; // 0 : none, 1 : operative, 2 inoperative
  5547. byte acDirIndex = 0;
  5548. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5549. if (gunIndex == 1)
  5550. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5551. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5552. {
  5553. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5554. {
  5555. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5556. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5557. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5558. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex + acDirIndex].Type, "Operative") == EQUAL)
  5559. {
  5560. if (isDb_ready)
  5561. DB_Update_Operactive(localDb, gunIndex + acDirIndex, true);
  5562. type = 1;
  5563. }
  5564. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex + acDirIndex].Type, "Inoperative") == EQUAL)
  5565. {
  5566. if (isDb_ready)
  5567. DB_Update_Operactive(localDb, gunIndex + acDirIndex, false);
  5568. type = 2;
  5569. }
  5570. }
  5571. }
  5572. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5573. {
  5574. if (ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5575. {
  5576. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5577. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5578. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5579. if(strcmp((char *)ShmOCPP20Data->ChangeAvailability[gunIndex + acDirIndex].operationalStatus, "Operative") == EQUAL)
  5580. {
  5581. if (isDb_ready)
  5582. DB_Update_Operactive(localDb, gunIndex + acDirIndex, true);
  5583. type = 1;
  5584. }
  5585. else if (strcmp((char *)ShmOCPP20Data->ChangeAvailability[gunIndex + acDirIndex].operationalStatus, "Inoperative") == EQUAL)
  5586. {
  5587. if (isDb_ready)
  5588. DB_Update_Operactive(localDb, gunIndex + acDirIndex, false);
  5589. type = 2;
  5590. }
  5591. }
  5592. }
  5593. if (type == 1)
  5594. {
  5595. chargingInfo[gunIndex]->IsAvailable = YES;
  5596. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5597. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION ||
  5598. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5599. {
  5600. SetChargerMode(gunIndex, SYS_MODE_IDLE);
  5601. }
  5602. }
  5603. else if (type == 2)
  5604. {
  5605. chargingInfo[gunIndex]->IsAvailable = NO;
  5606. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5607. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION ||
  5608. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5609. {
  5610. SetChargerMode(gunIndex, SYS_MODE_MAINTAIN);
  5611. }
  5612. }
  5613. }
  5614. void ocpp_chk_unlock_cmd(byte gunIndex)
  5615. {
  5616. byte acDirIndex = 0;
  5617. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5618. if (gunIndex == 1)
  5619. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5620. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5621. {
  5622. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5623. {
  5624. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5625. if ((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5626. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) ||
  5627. (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  5628. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  5629. {
  5630. // Unlocked - 充電中,需停止充電
  5631. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "UnlockCommand");
  5632. ChargingTerminalProcess(gunIndex);
  5633. }
  5634. if (chargingInfo[gunIndex]->Type == _Type_Chademo ||
  5635. chargingInfo[gunIndex]->Type == _Type_GB)
  5636. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  5637. else
  5638. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  5639. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorConf = YES;
  5640. }
  5641. }
  5642. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5643. {
  5644. if (ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5645. {
  5646. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5647. if ((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5648. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) ||
  5649. (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  5650. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  5651. {
  5652. // Unlocked - 充電中,需停止充電
  5653. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "UnlockCommand");
  5654. ChargingTerminalProcess(gunIndex);
  5655. }
  5656. if (chargingInfo[gunIndex]->Type == _Type_Chademo ||
  5657. chargingInfo[gunIndex]->Type == _Type_GB)
  5658. strcpy((char*)ShmOCPP20Data->UnlockConnector[gunIndex + acDirIndex].Response_status, "Unlocked");
  5659. else
  5660. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "Unlocked");
  5661. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorConf = YES;
  5662. }
  5663. }
  5664. }
  5665. bool ocpp_chk_remoteStop_cmd(byte gunIndex)
  5666. {
  5667. bool result = false;
  5668. byte acDirIndex = 0;
  5669. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5670. if (gunIndex == 1)
  5671. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5672. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5673. result = ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStopTransactionReq;
  5674. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5675. result = ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStopTransactionReq;
  5676. if (result)
  5677. {
  5678. DEBUG_INFO_MSG("Remote Stop by OCPP (%d) \n", gunIndex + acDirIndex);
  5679. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5680. {
  5681. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "Remote");
  5682. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStopTransactionReq = NO;
  5683. }
  5684. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5685. {
  5686. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "Remote");
  5687. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStopTransactionReq = NO;
  5688. }
  5689. }
  5690. return result;
  5691. }
  5692. bool ocpp_sub_get_remote_start_transaction_req(byte gun_index)
  5693. {
  5694. bool result = false;
  5695. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5696. {
  5697. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  5698. }
  5699. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5700. {
  5701. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
  5702. }
  5703. if (result && ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5704. {
  5705. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag,
  5706. (char *)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag);
  5707. }
  5708. return result;
  5709. }
  5710. void ocpp_sub_set_remote_start_transaction_req(byte gunIndex, bool result)
  5711. {
  5712. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5713. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStartTransactionReq = result;
  5714. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5715. ShmOCPP20Data->CsMsg.bits[gunIndex].RequestStartTransactionReq = result;
  5716. }
  5717. void ocpp_chk_remoteStart_cmd()
  5718. {
  5719. if(!isDetectPlugin())
  5720. {
  5721. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  5722. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5723. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++)
  5724. {
  5725. if (ocpp_sub_get_remote_start_transaction_req(acDirIndex))
  5726. {
  5727. if (ac_chargingInfo[ac_index]->SystemStatus == SYS_MODE_IDLE ||
  5728. ac_chargingInfo[ac_index]->SystemStatus == SYS_MODE_RESERVATION)
  5729. {
  5730. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  5731. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5732. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  5733. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  5734. //ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  5735. DetectPluginStart();
  5736. return;
  5737. }
  5738. ocpp_sub_set_remote_start_transaction_req(acDirIndex, NO);
  5739. }
  5740. }
  5741. byte threeGunIndex = 0;
  5742. byte dcIndex = 0;
  5743. bool isGunUsingStatus = false;
  5744. byte scheduleIndex = 0;
  5745. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5746. {
  5747. // 如果有 AC 槍,且 DC 槍也有兩把
  5748. if (acDirIndex == 1 && _index == 1)
  5749. threeGunIndex = 1;
  5750. if (ocpp_sub_get_remote_start_transaction_req(_index + threeGunIndex))
  5751. dcIndex = _index;
  5752. if (chargingInfo[_index]->SystemStatus != SYS_MODE_IDLE)
  5753. {
  5754. isGunUsingStatus = true;
  5755. }
  5756. if (chargingInfo[_index]->schedule.isTriggerStart)
  5757. scheduleIndex = _index;
  5758. }
  5759. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  5760. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  5761. {
  5762. if (dcIndex == 0)
  5763. threeGunIndex = 0;
  5764. //ocpp_sub_set_remote_start_transaction_req(dcIndex + threeGunIndex, NO);
  5765. return;
  5766. }
  5767. if (dcIndex == 0)
  5768. threeGunIndex = 0;
  5769. if (ocpp_sub_get_remote_start_transaction_req(dcIndex + threeGunIndex))
  5770. {
  5771. if (chargingInfo[dcIndex]->SystemStatus == SYS_MODE_IDLE ||
  5772. chargingInfo[dcIndex]->SystemStatus == SYS_MODE_RESERVATION)
  5773. {
  5774. if (ocpp_authorizeRemoteTxReqChk_key())
  5775. {
  5776. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId,
  5777. (char *)ShmOCPP16Data->RemoteStartTransaction[dcIndex + threeGunIndex].IdTag);
  5778. }
  5779. else
  5780. {
  5781. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  5782. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5783. ChangeGunSelectByIndex(dcIndex);
  5784. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  5785. DetectPluginStart();
  5786. }
  5787. }
  5788. ocpp_sub_set_remote_start_transaction_req(dcIndex + threeGunIndex, NO);
  5789. }
  5790. else if (chargingInfo[scheduleIndex]->schedule.isTriggerStart)
  5791. {
  5792. if (chargingInfo[scheduleIndex]->SystemStatus == SYS_MODE_IDLE ||
  5793. chargingInfo[scheduleIndex]->SystemStatus == SYS_MODE_RESERVATION)
  5794. {
  5795. chargingInfo[scheduleIndex]->RemoteStartFlag = YES;
  5796. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5797. DetectPluginStart();
  5798. }
  5799. chargingInfo[scheduleIndex]->schedule.isTriggerStart = NO;
  5800. }
  5801. }
  5802. }
  5803. bool ocpp_remote_start_wait_cmd()
  5804. {
  5805. bool result = false;
  5806. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5807. {
  5808. result = ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  5809. }
  5810. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5811. {
  5812. result = ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  5813. }
  5814. return result;
  5815. }
  5816. void ocpp_chk_update_cmd()
  5817. {
  5818. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5819. {
  5820. if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  5821. {
  5822. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  5823. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  5824. {
  5825. DEBUG_INFO_MSG("Backend : update start. \n");
  5826. sleep(2);
  5827. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  5828. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  5829. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5830. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5831. {
  5832. SetChargerMode(_index, SYS_MODE_UPDATE);
  5833. }
  5834. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5835. {
  5836. ac_chargingInfo[_index]->SystemStatus = SYS_MODE_UPDATE;
  5837. }
  5838. CloseWatchDog();
  5839. sleep(2);
  5840. KillTask();
  5841. byte updateResult = CheckUpdateProcess();
  5842. if (updateResult == PASS)
  5843. {
  5844. DEBUG_INFO_MSG("Backend : update complete. \n");
  5845. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5846. }
  5847. else if (updateResult == MODELNAME_FAIL)
  5848. {
  5849. DEBUG_INFO_MSG("Backend : model name is none match. \n");
  5850. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5851. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5852. sleep(5);
  5853. KillAllTask();
  5854. system("/usr/bin/run_evse_restart.sh");
  5855. return;
  5856. }
  5857. else
  5858. {
  5859. DEBUG_INFO_MSG("Backend : update fail. \n");
  5860. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5861. }
  5862. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5863. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5864. sleep(5);
  5865. system("reboot -f");
  5866. }
  5867. }
  5868. }
  5869. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5870. {
  5871. if(ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES)
  5872. {
  5873. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = NO;
  5874. if (strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded") == EQUAL)
  5875. {
  5876. DEBUG_INFO_MSG("Backend : update start. \n");
  5877. sleep(2);
  5878. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "");
  5879. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installing");
  5880. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5881. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5882. {
  5883. SetChargerMode(_index, SYS_MODE_UPDATE);
  5884. }
  5885. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5886. {
  5887. ac_chargingInfo[_index]->SystemStatus = SYS_MODE_UPDATE;
  5888. }
  5889. CloseWatchDog();
  5890. sleep(2);
  5891. KillTask();
  5892. byte updateResult = CheckUpdateProcess();
  5893. if (updateResult == PASS)
  5894. {
  5895. DEBUG_INFO_MSG("Backend : update complete. \n");
  5896. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5897. }
  5898. else if (updateResult == MODELNAME_FAIL)
  5899. {
  5900. DEBUG_INFO_MSG("Backend : model name is none match. \n");
  5901. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5902. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5903. sleep(5);
  5904. KillAllTask();
  5905. system("/usr/bin/run_evse_restart.sh");
  5906. return;
  5907. }
  5908. else
  5909. {
  5910. DEBUG_INFO_MSG("Backend : update fail. \n");
  5911. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5912. }
  5913. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5914. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5915. sleep(5);
  5916. system("reboot -f");
  5917. }
  5918. }
  5919. }
  5920. }
  5921. bool ocpp_chk_invalid_id_cmd(byte gunIndex)
  5922. {
  5923. byte acDirIndex = 0;
  5924. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5925. if (gunIndex == 1)
  5926. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5927. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5928. {
  5929. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  5930. {
  5931. if (strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Blocked") == EQUAL ||
  5932. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Expired") == EQUAL ||
  5933. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Invalid") == EQUAL)
  5934. return true;
  5935. }
  5936. }
  5937. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5938. {
  5939. if (strstr((char *) ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE"))
  5940. {
  5941. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Blocked") == EQUAL ||
  5942. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Expired") == EQUAL ||
  5943. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Invalid") == EQUAL ||
  5944. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NoCredit") == EQUAL ||
  5945. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAllowedTypeEVSE") == EQUAL ||
  5946. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAtThisLocation") == EQUAL ||
  5947. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAtThisTime") == EQUAL ||
  5948. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Unknown") == EQUAL)
  5949. return true;
  5950. }
  5951. }
  5952. return false;
  5953. }
  5954. bool ocpp_chk_profileConf_cmd(byte gunIndex)
  5955. {
  5956. byte acDirIndex = 0;
  5957. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5958. if (gunIndex == 1)
  5959. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5960. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5961. {
  5962. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == YES)
  5963. {
  5964. ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf = NO;
  5965. return true;
  5966. }
  5967. }
  5968. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5969. {
  5970. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == YES)
  5971. {
  5972. ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf = NO;
  5973. return true;
  5974. }
  5975. }
  5976. return false;
  5977. }
  5978. void ocpp_chk_notificationMessage_cmd(byte gunIndex)
  5979. {
  5980. byte acDirIndex = 0;
  5981. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5982. if (gunIndex == 1)
  5983. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5984. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5985. {
  5986. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].TriggerMessageReq == YES)
  5987. {
  5988. if (chargingInfo[gunIndex]->Type == _Type_Chademo &&
  5989. (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5990. chargingInfo[gunIndex]->SystemStatus ==SYS_MODE_RESERVATION ||
  5991. chargingInfo[gunIndex]->SystemStatus ==SYS_MODE_MAINTAIN))
  5992. {
  5993. if (ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag == NO)
  5994. {
  5995. PRINTF_FUNC("Trigger msg (StatusNotification) = %d (chademo) \n", gunIndex);
  5996. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = 0;
  5997. GetTimespecFunc(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  5998. ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag = YES;
  5999. }
  6000. }
  6001. else if (chargingInfo[gunIndex]->Type == _Type_CCS ||
  6002. chargingInfo[gunIndex]->Type == _Type_GB ||
  6003. chargingInfo[gunIndex]->Type == _Type_AC)
  6004. {
  6005. PRINTF_FUNC("Trigger msg (StatusNotification) = %d (ccs) \n", gunIndex);
  6006. }
  6007. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].TriggerMessageReq = NO;
  6008. }
  6009. }
  6010. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6011. {
  6012. }
  6013. if (ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag)
  6014. {
  6015. int _timeBuf = GetTimeoutValue(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  6016. if (_timeBuf < 0)
  6017. {
  6018. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = 0;
  6019. GetTimespecFunc(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  6020. }
  6021. if (_timeBuf > 30 ||
  6022. (_timeBuf > 2 &&
  6023. chargingInfo[gunIndex]->ConnectorPlugIn))
  6024. {
  6025. PRINTF_FUNC("Trigger msg - timeout or Plugin. \n");
  6026. ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag = NO;
  6027. }
  6028. else
  6029. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = (_timeBuf / uSEC_VAL);
  6030. }
  6031. }
  6032. void ocpp_lift_profileReq_cmd(byte gunIndex)
  6033. {
  6034. byte acDirIndex = 0;
  6035. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6036. if (gunIndex == 1)
  6037. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6038. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6039. {
  6040. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == NO)
  6041. {
  6042. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq == NO)
  6043. ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq = YES;
  6044. }
  6045. }
  6046. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6047. {
  6048. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == NO)
  6049. {
  6050. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq == NO)
  6051. ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq = YES;
  6052. }
  6053. }
  6054. }
  6055. void ocpp_set_startTransation_cmd(byte gunIndex)
  6056. {
  6057. byte _OcppGunIndex = gunIndex;
  6058. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6059. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6060. _OcppGunIndex = 2;
  6061. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6062. {
  6063. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6064. strcpy((char *)chargingInfo[gunIndex]->StartUserId, (char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  6065. else
  6066. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6067. if (strcmp((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, "") == EQUAL)
  6068. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6069. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  6070. PRINTF_FUNC("Gun = %d, IdTag = %s \n", _OcppGunIndex, chargingInfo[gunIndex]->StartUserId);
  6071. }
  6072. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6073. {
  6074. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6075. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6076. else
  6077. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)chargingInfo[gunIndex]->StartUserId);
  6078. ShmOCPP20Data->CpMsg.bits[_OcppGunIndex].TransactionEventReq = YES;
  6079. PRINTF_FUNC("Gun = %d, IdTag = %s \n", _OcppGunIndex, ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6080. }
  6081. }
  6082. void ocpp_set_startTransationPH_cmd(byte gunIndex)
  6083. {
  6084. byte _OcppGunIndex = gunIndex;
  6085. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6086. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6087. _OcppGunIndex = 2;
  6088. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  6089. {
  6090. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6091. {
  6092. if (strcmp((char *)ShmOCPP16DataPH->StartTransaction[_OcppGunIndex].IdTag, "") == EQUAL)
  6093. strcpy((char *)ShmOCPP16DataPH->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6094. ShmOCPP16DataPH->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  6095. }
  6096. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6097. {
  6098. }
  6099. }
  6100. }
  6101. void ocpp_set_stopTransation_cmd(byte gunIndex)
  6102. {
  6103. byte _OcppGunIndex = gunIndex;
  6104. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6105. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6106. _OcppGunIndex = 2;
  6107. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6108. {
  6109. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6110. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  6111. else
  6112. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6113. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  6114. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  6115. }
  6116. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6117. {
  6118. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6119. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6120. else
  6121. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)chargingInfo[gunIndex]->StartUserId);
  6122. PRINTF_FUNC("IdTag = %s \n", ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  6123. ShmOCPP20Data->CpMsg.bits[_OcppGunIndex].TransactionEventReq = YES;
  6124. }
  6125. }
  6126. void ocpp_set_stopTransationPH_cmd(byte gunIndex)
  6127. {
  6128. byte _OcppGunIndex = gunIndex;
  6129. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6130. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6131. _OcppGunIndex = 2;
  6132. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  6133. {
  6134. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6135. {
  6136. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  6137. strcpy((char *)ShmOCPP16DataPH->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16DataPH->StopTransaction[_OcppGunIndex].IdTag);
  6138. else
  6139. strcpy((char *)ShmOCPP16DataPH->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  6140. ShmOCPP16DataPH->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  6141. }
  6142. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6143. {
  6144. }
  6145. }
  6146. }
  6147. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString)
  6148. {
  6149. // 參數 gunIndex 已包含 AC 槍
  6150. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6151. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, errString);
  6152. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6153. {
  6154. }
  6155. }
  6156. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason)
  6157. {
  6158. byte acDirIndex = 0;
  6159. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6160. if (gunIndex == 1)
  6161. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6162. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6163. {
  6164. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "") == EQUAL)
  6165. {
  6166. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, reason);
  6167. }
  6168. }
  6169. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6170. {
  6171. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "") == EQUAL)
  6172. {
  6173. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, reason);
  6174. }
  6175. }
  6176. }
  6177. void ocpp_set_authorizeReq_cmd(byte value)
  6178. {
  6179. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6180. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = value;
  6181. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6182. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = value;
  6183. }
  6184. void ocpp_set_authorizeConf_cmd(byte value)
  6185. {
  6186. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6187. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = value;
  6188. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6189. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = value;
  6190. }
  6191. void ocpp_set_errCode_cmd(byte gunIndex)
  6192. {
  6193. byte acDirIndex = 0;
  6194. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6195. if (gunIndex == 1)
  6196. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6197. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6198. {
  6199. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  6200. {
  6201. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "InternalError");
  6202. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, (char *)chargingInfo[gunIndex]->ConnectorAlarmCode);
  6203. }
  6204. else if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  6205. {
  6206. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "OtherError");
  6207. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, (char *)chargingInfo[gunIndex]->EvConnAlarmCode);
  6208. }
  6209. else
  6210. {
  6211. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError");
  6212. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "");
  6213. }
  6214. }
  6215. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6216. {
  6217. }
  6218. }
  6219. void ocpp_clear_errorCode_cmd(byte gunIndex)
  6220. {
  6221. byte acDirIndex = 0;
  6222. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6223. if (gunIndex == 1)
  6224. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6225. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6226. {
  6227. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError") != EQUAL)
  6228. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError");
  6229. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "") != EQUAL)
  6230. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "");
  6231. }
  6232. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6233. {
  6234. }
  6235. }
  6236. void ocpp_default_errorCode_cmd(byte gunIndex)
  6237. {
  6238. byte acDirIndex = 0;
  6239. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6240. if (gunIndex == 1)
  6241. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6242. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6243. {
  6244. if (strcmp ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "" ) == EQUAL)
  6245. strcpy ( (char *) ShmOCPP16Data->StatusNotification [gunIndex + acDirIndex].ErrorCode, "NoError" );
  6246. }
  6247. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6248. {
  6249. }
  6250. }
  6251. void ocpp_clear_idTag_cmd(byte gun_index)
  6252. {
  6253. byte acDirIndex = 0;
  6254. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6255. if (gun_index == 1)
  6256. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6257. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6258. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index + acDirIndex].ResponseIdTagInfo.Status, "");
  6259. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6260. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].Response_idTokenInfo.status, "");
  6261. }
  6262. void ocpp_clear_stopReason(byte gun_index)
  6263. {
  6264. byte acDirIndex = 0;
  6265. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6266. if (gun_index == 1)
  6267. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6268. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6269. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason, "");
  6270. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6271. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].transactionInfo.stoppedReason, "");
  6272. }
  6273. void ocpp_clear_stopTansaction_Idtag(byte gun_index)
  6274. {
  6275. byte acDirIndex = 0;
  6276. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6277. if (gun_index == 1)
  6278. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6279. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6280. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].IdTag, "");
  6281. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6282. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].idToken.idToken, "");
  6283. }
  6284. unsigned short _ocpp_get_connect_timeout()
  6285. {
  6286. uint16_t result = (TIMEOUT_SPEC_HANDSHAKING / 1000);
  6287. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6288. {
  6289. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  6290. result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  6291. }
  6292. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6293. {
  6294. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
  6295. result = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  6296. }
  6297. return result;
  6298. }
  6299. bool ocpp_get_authorize_conf()
  6300. {
  6301. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6302. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  6303. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6304. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  6305. return false;
  6306. }
  6307. bool ocpp_get_startTransation_req(byte gunIndex)
  6308. {
  6309. bool result = false;
  6310. byte acDirIndex = 0;
  6311. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6312. if (gunIndex == 1)
  6313. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6314. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6315. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6316. {
  6317. result = ShmOCPP16Data->CpMsg.bits[gunIndex + acDirIndex].StartTransactionReq;
  6318. }
  6319. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6320. {
  6321. result = ShmOCPP20Data->CpMsg.bits[gunIndex + acDirIndex].TransactionEventReq;
  6322. }
  6323. return result;
  6324. }
  6325. void ocpp_get_freeVendor(byte gunIndex)
  6326. {
  6327. byte _OcppGunIndex = gunIndex;
  6328. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  6329. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  6330. _OcppGunIndex = 2;
  6331. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6332. {
  6333. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, "");
  6334. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag,
  6335. (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
  6336. }
  6337. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6338. {
  6339. if (strlen((char *)ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value) > 0)
  6340. {
  6341. }
  6342. else
  6343. {
  6344. }
  6345. }
  6346. }
  6347. void ocpp_parsing_DT_defaultPrice_cmd()
  6348. {
  6349. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6350. {
  6351. if(strcmp(MyDefaultPriceString, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData) != EQUAL)
  6352. {
  6353. DefaultPriceHandlerOcpp16((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  6354. memcpy(MyDefaultPriceString, ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, 128);
  6355. }
  6356. }
  6357. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6358. {
  6359. //ShmOCPP20Data->CostUpdated
  6360. }
  6361. }
  6362. void ocpp_parsing_DT_userPrice_cmd()
  6363. {
  6364. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6365. {
  6366. if (strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken) > 0 &&
  6367. strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.price) > 0)
  6368. {
  6369. UserPriceHandlerOcpp16((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken,
  6370. (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  6371. //memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  6372. }
  6373. }
  6374. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6375. {
  6376. //ShmOCPP20Data->CostUpdated
  6377. }
  6378. }
  6379. void ocpp_parsing_DT_running_final_cmd()
  6380. {
  6381. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6382. {
  6383. RunningFinalCostHandlerOcpp16();
  6384. }
  6385. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6386. {
  6387. //ShmOCPP20Data->CostUpdated
  6388. }
  6389. }
  6390. void ocpp_chargingProfile_process(byte _index)
  6391. {
  6392. int _time = 0;
  6393. int _startCount = NO_DEFINE;
  6394. int _maxCount = 0;
  6395. byte acDirIndex = 0;
  6396. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  6397. if (_index == 1)
  6398. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  6399. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6400. {
  6401. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingProfileKind, "Absolute") == EQUAL &&
  6402. ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingProfileId == YES)
  6403. {
  6404. _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.StartSchedule);
  6405. if (_time > -1)
  6406. {
  6407. _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod);
  6408. _startCount = NO_DEFINE;
  6409. for (byte _count = 0; _count < _maxCount; _count++)
  6410. {
  6411. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  6412. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  6413. {
  6414. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  6415. ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  6416. {
  6417. _startCount = _count;
  6418. }
  6419. }
  6420. }
  6421. if (_startCount < _maxCount)
  6422. {
  6423. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount);
  6424. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  6425. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  6426. chargingInfo[_index]->PresentChargingVoltage > 0)
  6427. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  6428. else
  6429. chargingInfo[_index]->ChargingProfileCurrent = 0;
  6430. }
  6431. else
  6432. {
  6433. chargingInfo[_index]->ChargingProfilePower = -1;
  6434. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6435. }
  6436. }
  6437. else
  6438. {
  6439. chargingInfo[_index]->ChargingProfilePower = -1;
  6440. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6441. }
  6442. }
  6443. else
  6444. {
  6445. chargingInfo[_index]->ChargingProfilePower = -1;
  6446. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6447. }
  6448. if (strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == EQUAL &&
  6449. ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES)
  6450. {
  6451. _time = GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  6452. if (_time > -1)
  6453. {
  6454. _maxCount = ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  6455. _startCount = NO_DEFINE;
  6456. for (byte _count = 0; _count < _maxCount; _count++)
  6457. {
  6458. if (_time >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  6459. {
  6460. if ((_count == 0 && ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  6461. ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  6462. {
  6463. _startCount = _count;
  6464. }
  6465. }
  6466. }
  6467. //PRINTF_FUNC("_startCount = %d, Max Profile Limit = %f \n", _startCount, ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  6468. if (_startCount < _maxCount)
  6469. {
  6470. float maxChargingProfilePow = ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  6471. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_BOTH)
  6472. maxChargingProfilePow /= 2;
  6473. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = maxChargingProfilePow;
  6474. }
  6475. else
  6476. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6477. }
  6478. }
  6479. else
  6480. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6481. }
  6482. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6483. {
  6484. if (strcmp((char *)ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingProfileKind, "Absolute") == EQUAL &&
  6485. ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].id == YES)
  6486. {
  6487. _time = GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].startSchedule);
  6488. _maxCount = ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod);
  6489. _startCount = NO_DEFINE;
  6490. for (byte _count = 0; _count < _maxCount; _count++)
  6491. {
  6492. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  6493. if (_time >= ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].startPeriod)
  6494. {
  6495. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].limit >= MIN_OUTPUT_CUR) ||
  6496. ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].limit > MIN_OUTPUT_CUR)
  6497. {
  6498. _startCount = _count;
  6499. }
  6500. }
  6501. }
  6502. if (_startCount < _maxCount)
  6503. {
  6504. //PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount);
  6505. //PRINTF_FUNC("Profile Limit = %f \n", ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit);
  6506. chargingInfo[_index]->ChargingProfilePower = ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit * AC_OUTPUT_VOL;
  6507. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 && chargingInfo[_index]->PresentChargingVoltage > 0)
  6508. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  6509. else
  6510. chargingInfo[_index]->ChargingProfileCurrent = 0;
  6511. }
  6512. else
  6513. {
  6514. chargingInfo[_index]->ChargingProfilePower = -1;
  6515. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6516. }
  6517. }
  6518. else
  6519. {
  6520. chargingInfo[_index]->ChargingProfilePower = -1;
  6521. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6522. }
  6523. }
  6524. else
  6525. {
  6526. chargingInfo[_index]->ChargingProfilePower = -1;
  6527. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6528. }
  6529. if (chargingInfo[_index]->ChargingProfilePower >= 0 &&
  6530. _ChargingProfilePwBuffer[_index] != chargingInfo[_index]->ChargingProfilePower)
  6531. {
  6532. _ChargingProfilePwBuffer[_index] = chargingInfo[_index]->ChargingProfilePower;
  6533. PRINTF_FUNC("Profile : ChargingProfilePower = %.2f \n", chargingInfo[_index]->ChargingProfilePower);
  6534. PRINTF_FUNC("Profile : ChargingProfileCurrent = %.2f \n", chargingInfo[_index]->ChargingProfileCurrent);
  6535. }
  6536. }
  6537. void CheckOcppProfileConfirm(byte gun_index)
  6538. {
  6539. if ((chargingInfo [gun_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo [gun_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  6540. (chargingInfo [gun_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo [gun_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  6541. {
  6542. ocpp_chk_profileConf_cmd ( gun_index );
  6543. }
  6544. }
  6545. void CheckAcOcppProfileConfirm()
  6546. {
  6547. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING &&
  6548. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  6549. {
  6550. ocpp_ac_chk_profileConf_cmd();
  6551. }
  6552. }
  6553. //===============================================
  6554. // wifi schedule
  6555. //===============================================
  6556. bool WifiScheduleStop(byte gunIndex)
  6557. {
  6558. bool result = false;
  6559. result = chargingInfo[gunIndex]->schedule.isTriggerStop;
  6560. chargingInfo[gunIndex]->schedule.isTriggerStop = NO;
  6561. return result;
  6562. }
  6563. //===============================================
  6564. // SQLite3 related routine
  6565. //===============================================
  6566. int NetworkDB_Open(sqlite3 *db)
  6567. {
  6568. int result = PASS;
  6569. char* errMsg = NULL;
  6570. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  6571. "idx integer primary key AUTOINCREMENT, "
  6572. "occurDatetime text NOT NULL, "
  6573. "isInternet text NOT NULL, "
  6574. "isOcppConnected text NOT NULL, "
  6575. "isEth0Internet text NOT NULL, "
  6576. "isMlan0Internet text NOT NULL, "
  6577. "isPpp0Internet text NOT NULL, "
  6578. "rssiMlan0 text NOT NULL, "
  6579. "rssiPpp0 text NOT NULL"
  6580. ");";
  6581. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  6582. if(sqlite3_open(NETWORK_DB_FILE, &db))
  6583. {
  6584. result = FAIL;
  6585. PRINTF_FUNC( "Can't open database (Net) : %s\n", sqlite3_errmsg(db));
  6586. sqlite3_close(db);
  6587. }
  6588. else
  6589. {
  6590. PRINTF_FUNC( "Local network status record database open successfully.\n");
  6591. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6592. {
  6593. result = FAIL;
  6594. PRINTF_FUNC( "Create local network status record table error message: %s\n", errMsg);
  6595. }
  6596. else
  6597. {
  6598. PRINTF_FUNC( "Opened local network status record table successfully\n");
  6599. }
  6600. sqlite3_close(db);
  6601. }
  6602. return result;
  6603. }
  6604. int DB_Open(sqlite3 *db)
  6605. {
  6606. int result = PASS;
  6607. char* errMsg = NULL;
  6608. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  6609. "idx integer primary key AUTOINCREMENT, "
  6610. "reservationId text, "
  6611. "transactionId text, "
  6612. "startMethod text, "
  6613. "userId text, "
  6614. "dateTimeStart text, "
  6615. "dateTimeStop text,"
  6616. "socStart text, "
  6617. "socStop text, "
  6618. "chargeEnergy text, "
  6619. "stopReason text,"
  6620. "vincode text"
  6621. ");";
  6622. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  6623. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6624. "`item` TEXT NOT NULL, "
  6625. "`connector` INTEGER NOT NULL, "
  6626. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  6627. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  6628. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6629. "`occurDatetime` TEXT NOT NULL, "
  6630. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  6631. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  6632. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6633. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  6634. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  6635. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6636. "`connector` INTEGER NOT NULL, "
  6637. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  6638. // char * createTestSql = "CREATE TABLE IF NOT EXISTS `for_test` ( "
  6639. // "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6640. // "`connector` INTEGER NOT NULL, "
  6641. // "`testID` TEXT, "
  6642. // "`checkStatus` INTEGER NOT NULL "
  6643. // ");";
  6644. if(sqlite3_open(DB_FILE, &db))
  6645. {
  6646. result = FAIL;
  6647. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6648. sqlite3_close(db);
  6649. }
  6650. else
  6651. {
  6652. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  6653. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6654. {
  6655. result = FAIL;
  6656. PRINTF_FUNC( "Create local charging record table error message: %s\n", errMsg);
  6657. }
  6658. else
  6659. {
  6660. PRINTF_FUNC( "Opened local charging record table successfully\n");
  6661. }
  6662. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  6663. {
  6664. result = FAIL;
  6665. PRINTF_FUNC( "Create local config table error message: %s\n", errMsg);
  6666. }
  6667. else
  6668. {
  6669. PRINTF_FUNC( "Opened local config table successfully\n");
  6670. }
  6671. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6672. {
  6673. result = FAIL;
  6674. PRINTF_FUNC( "Create local record table error message: %s\n", errMsg);
  6675. }
  6676. else
  6677. {
  6678. PRINTF_FUNC( "Opened local record table successfully\n");
  6679. }
  6680. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  6681. {
  6682. result = FAIL;
  6683. PRINTF_FUNC( "Create reboot record table error message: %s\n", errMsg);
  6684. }
  6685. else
  6686. {
  6687. PRINTF_FUNC( "Opened reboot record table successfully\n");
  6688. }
  6689. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  6690. {
  6691. result = FAIL;
  6692. PRINTF_FUNC( "Create power consumption record table error message: %s\n", errMsg);
  6693. }
  6694. else
  6695. {
  6696. PRINTF_FUNC( "Opened power consumption record table successfully\n");
  6697. }
  6698. // if (sqlite3_exec(db, createTestSql, 0, 0, &errMsg) != SQLITE_OK)
  6699. // {
  6700. // result = FAIL;
  6701. // printf( "Create test table error message: %s\n", errMsg);
  6702. // }
  6703. // else
  6704. // {
  6705. // printf( "Opened test table successfully\n");
  6706. // }
  6707. sqlite3_close(db);
  6708. }
  6709. return result;
  6710. }
  6711. int DB_Insert_Record(sqlite3 *db, int gun_index)
  6712. {
  6713. int result = PASS;
  6714. char* errMsg = NULL;
  6715. char insertSql[1024];
  6716. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6717. {
  6718. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, vincode) "
  6719. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  6720. chargingInfo[gun_index]->ReservationId,
  6721. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  6722. chargingInfo[gun_index]->StartMethod,
  6723. chargingInfo[gun_index]->StartUserId,
  6724. chargingInfo[gun_index]->StartDateTime,
  6725. chargingInfo[gun_index]->StopDateTime,
  6726. chargingInfo[gun_index]->EvBatteryStartSoc,
  6727. chargingInfo[gun_index]->EvBatterySoc,
  6728. chargingInfo[gun_index]->PresentChargedEnergy,
  6729. ShmOCPP16Data->StopTransaction[gun_index].StopReason,
  6730. chargingInfo[gun_index]->EVCCID);
  6731. }
  6732. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6733. {
  6734. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  6735. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  6736. chargingInfo[gun_index]->ReservationId,
  6737. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  6738. chargingInfo[gun_index]->StartMethod,
  6739. chargingInfo[gun_index]->StartUserId,
  6740. chargingInfo[gun_index]->StartDateTime,
  6741. chargingInfo[gun_index]->StopDateTime,
  6742. chargingInfo[gun_index]->EvBatteryStartSoc,
  6743. chargingInfo[gun_index]->EvBatterySoc,
  6744. chargingInfo[gun_index]->PresentChargedEnergy,
  6745. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  6746. }
  6747. if(sqlite3_open(DB_FILE, &db))
  6748. {
  6749. result = FAIL;
  6750. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6751. sqlite3_close(db);
  6752. }
  6753. else
  6754. {
  6755. PRINTF_FUNC( "Local charging record database open successfully.\n");
  6756. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6757. {
  6758. result = FAIL;
  6759. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  6760. }
  6761. else
  6762. {
  6763. PRINTF_FUNC( "Insert local charging record successfully\n");
  6764. }
  6765. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  6766. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6767. {
  6768. result = FAIL;
  6769. PRINTF_FUNC( "delete local charging error message: %s\n", errMsg);
  6770. }
  6771. else
  6772. {
  6773. PRINTF_FUNC( "delete local charging record successfully\n");
  6774. }
  6775. sqlite3_close(db);
  6776. }
  6777. return result;
  6778. }
  6779. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  6780. {
  6781. uint8_t result = false;
  6782. char* errMsg = NULL;
  6783. char sqlStr[1024];
  6784. srand(time(NULL));
  6785. if(sqlite3_open(DB_FILE, &db))
  6786. {
  6787. result = FAIL;
  6788. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6789. sqlite3_close(db);
  6790. }
  6791. else
  6792. {
  6793. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  6794. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  6795. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  6796. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6797. {
  6798. result = FAIL;
  6799. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  6800. }
  6801. else
  6802. {
  6803. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  6804. }
  6805. sqlite3_close(db);
  6806. }
  6807. return result;
  6808. }
  6809. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  6810. {
  6811. uint8_t result = true;
  6812. char* errMsg = NULL;
  6813. char sqlStr[1024];
  6814. char **rs;
  6815. int rows, cols;
  6816. sprintf(sqlStr, "select * from config where item='IsAvailable' and connector=%d;", gun_index);
  6817. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  6818. if(sqlite3_open(DB_FILE, &db))
  6819. {
  6820. result = FAIL;
  6821. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6822. sqlite3_close(db);
  6823. }
  6824. else
  6825. {
  6826. PRINTF_FUNC( "Local config query database open successfully.\r\n");
  6827. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  6828. if(rows>0)
  6829. {
  6830. for(int idxRow=1;idxRow<=rows;idxRow++)
  6831. {
  6832. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  6833. {
  6834. result = false;
  6835. }
  6836. PRINTF_FUNC("Query connector-%d isOperactive: %s\r\n", gun_index, rs[(idxRow*cols)+3]);
  6837. }
  6838. }
  6839. else
  6840. {
  6841. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.\r\n", gun_index);
  6842. }
  6843. sqlite3_free_table(rs);
  6844. sqlite3_close(db);
  6845. }
  6846. return result;
  6847. }
  6848. void DB_Update_PowerConsumption(sqlite3 *db, uint8_t gun_index, float value)
  6849. {
  6850. char* errMsg = NULL;
  6851. char sqlStr[1024];
  6852. srand(time(NULL));
  6853. if(sqlite3_open(DB_FILE, &db))
  6854. {
  6855. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6856. sqlite3_close(db);
  6857. }
  6858. else
  6859. {
  6860. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  6861. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6862. {
  6863. PRINTF_FUNC( "update power_consumption error message: %s\n", errMsg);
  6864. }
  6865. else
  6866. {
  6867. //PRINTF_FUNC("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  6868. }
  6869. sqlite3_close(db);
  6870. }
  6871. }
  6872. float DB_Get_PowerConsumption(sqlite3 *db, uint8_t gun_index)
  6873. {
  6874. float result = 0;
  6875. char* errMsg = NULL;
  6876. char sqlStr[1024];
  6877. char **rs;
  6878. int rows, cols;
  6879. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  6880. if (sqlite3_open(DB_FILE, &db))
  6881. {
  6882. result = FAIL;
  6883. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  6884. sqlite3_close(db);
  6885. }
  6886. else
  6887. {
  6888. PRINTF_FUNC("Local config query database open successfully.\r\n");
  6889. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  6890. if (rows > 0)
  6891. {
  6892. for (int idxRow = 1; idxRow <= rows; idxRow++)
  6893. {
  6894. result = atof(rs[(idxRow * cols) + 2]);
  6895. }
  6896. }
  6897. else
  6898. {
  6899. PRINTF_FUNC( "Query connector-%d fail, set default value to consumption.\r\n", gun_index);
  6900. }
  6901. sqlite3_free_table(rs);
  6902. sqlite3_close(db);
  6903. }
  6904. return result;
  6905. }
  6906. int DB_Reboot_Record(sqlite3 *db)
  6907. {
  6908. int result = PASS;
  6909. char* errMsg = NULL;
  6910. char insertSql[256];
  6911. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  6912. if(sqlite3_open(DB_FILE, &db))
  6913. {
  6914. result = FAIL;
  6915. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6916. sqlite3_close(db);
  6917. }
  6918. else
  6919. {
  6920. PRINTF_FUNC( "Local charging record database open successfully.\n");
  6921. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6922. {
  6923. result = FAIL;
  6924. PRINTF_FUNC( "Insert reboot record error message: %s\n", errMsg);
  6925. }
  6926. else
  6927. {
  6928. PRINTF_FUNC( "Insert reboot record successfully\n");
  6929. }
  6930. sprintf(insertSql, "delete from reboot_record where idx < (select idx from reboot_record order by idx desc limit 1)-2000;");
  6931. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6932. {
  6933. result = FAIL;
  6934. PRINTF_FUNC( "delete reboot record error message: %s\n", errMsg);
  6935. }
  6936. else
  6937. {
  6938. PRINTF_FUNC( "delete reboot record successfully\n");
  6939. }
  6940. sqlite3_close(db);
  6941. }
  6942. return result;
  6943. }
  6944. int DB_Insert_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  6945. {
  6946. int result = PASS;
  6947. char* errMsg = NULL;
  6948. char insertSql[1024];
  6949. sprintf(insertSql, "insert into for_test (connector, testID, checkStatus) values(%d, %s, 0);",
  6950. gun_index, textID);
  6951. if(sqlite3_open(DB_FILE, &db))
  6952. {
  6953. result = FAIL;
  6954. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6955. sqlite3_close(db);
  6956. }
  6957. else
  6958. {
  6959. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6960. {
  6961. result = FAIL;
  6962. PRINTF_FUNC( "Insert sdlu test error message : %s\n", errMsg);
  6963. }
  6964. else
  6965. {
  6966. PRINTF_FUNC( "Insert sdlu test successfully\n");
  6967. }
  6968. sqlite3_close(db);
  6969. }
  6970. return result;
  6971. }
  6972. int DB_delete_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  6973. {
  6974. int result = PASS;
  6975. char* errMsg = NULL;
  6976. char insertSql[1024];
  6977. sprintf(insertSql, "delete from for_test where testID = %s", textID);
  6978. if (sqlite3_open(DB_FILE, &db))
  6979. {
  6980. result = FAIL;
  6981. PRINTF_FUNC("Can't open database: %s\n", sqlite3_errmsg(db));
  6982. sqlite3_close(db);
  6983. }
  6984. else
  6985. {
  6986. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6987. {
  6988. result = FAIL;
  6989. PRINTF_FUNC( "Test message 2: %s\n", errMsg);
  6990. }
  6991. else
  6992. {
  6993. PRINTF_FUNC( "Test message 2 successfully\n");
  6994. }
  6995. sqlite3_close(db);
  6996. }
  6997. return result;
  6998. }
  6999. int DB_sql_test(sqlite3 *db, uint8_t gun_index)
  7000. {
  7001. int result = PASS;
  7002. char* errMsg = NULL;
  7003. char insertSql[1024];
  7004. char valueStr[128];
  7005. char **rs;
  7006. int rows, cols;
  7007. sprintf(insertSql, "select testID from for_test where checkStatus=0;");
  7008. if (sqlite3_open(DB_FILE, &db))
  7009. {
  7010. result = FAIL;
  7011. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  7012. sqlite3_close(db);
  7013. }
  7014. else
  7015. {
  7016. PRINTF_FUNC("Local for_test query database open successfully.\r\n");
  7017. sqlite3_get_table(db, insertSql, &rs, &rows, &cols, &errMsg);
  7018. if (rows > 0)
  7019. {
  7020. for (int idxRow = 1; idxRow <= rows; idxRow++)
  7021. {
  7022. strcpy(valueStr, rs[(idxRow * cols)]);
  7023. printf("valueStr = %s \n", valueStr);
  7024. }
  7025. }
  7026. else
  7027. {
  7028. PRINTF_FUNC("select testID from for_test fail \r\n");
  7029. }
  7030. sqlite3_free_table(rs);
  7031. sqlite3_close(db);
  7032. }
  7033. return result;
  7034. }
  7035. //===============================================
  7036. // Config process
  7037. //===============================================
  7038. void AddPlugInTimes(byte gunIndex)
  7039. {
  7040. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  7041. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  7042. else if(chargingInfo[gunIndex]->Type == _Type_CCS)
  7043. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  7044. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  7045. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  7046. }
  7047. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  7048. {
  7049. char cmdBuf[32];
  7050. struct timeb csuTime;
  7051. struct tm *tmCSU;
  7052. ftime(&csuTime);
  7053. tmCSU = localtime(&csuTime.time);
  7054. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  7055. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  7056. tmCSU->tm_sec);
  7057. if (isStart)
  7058. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  7059. else
  7060. {
  7061. if (strcmp((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00") != EQUAL)
  7062. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  7063. }
  7064. }
  7065. void zipLogFiles()
  7066. {
  7067. const char* logPath = "/Storage/SystemLog";
  7068. // 獲取目錄
  7069. DIR* pDir = opendir(logPath);
  7070. if (pDir != NULL)
  7071. {
  7072. struct timeb csuTime;
  7073. struct tm *tmCSU;
  7074. ftime(&csuTime);
  7075. tmCSU = localtime(&csuTime.time);
  7076. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  7077. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  7078. // tmCSU->tm_sec);
  7079. // Read items inside the folder
  7080. struct dirent* pEntry = NULL;
  7081. while ((pEntry = readdir(pDir)) != NULL)
  7082. {
  7083. if (strcmp(pEntry->d_name, ".") != 0 &&
  7084. strcmp(pEntry->d_name, "..") != 0 &&
  7085. strncmp(pEntry->d_name, "[", 1) == 0 &&
  7086. strstr(pEntry->d_name, "tar") < 0)
  7087. {
  7088. char yearC[5];
  7089. unsigned short year = 0;
  7090. char monthC[3];
  7091. unsigned short month = 0;
  7092. yearC[4] = '\0';
  7093. strncpy(yearC, pEntry->d_name + 1, 4);
  7094. monthC[2] = '\0';
  7095. strncpy(monthC, pEntry->d_name + 6, 2);
  7096. year = atoi(yearC);
  7097. month = atoi(monthC);
  7098. if (year != 0)
  7099. {
  7100. if (year < tmCSU->tm_year + 1900 ||
  7101. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  7102. {
  7103. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  7104. char file[256];
  7105. memset(file, 0x00, sizeof(file));
  7106. strcat(file, "tar zcvf ");
  7107. strcat(file, logPath);
  7108. strncat(file, "/", 1);
  7109. strcat(file, pEntry->d_name);
  7110. strcat(file, ".tar");
  7111. strncat(file, " ", 1);
  7112. strcat(file, logPath);
  7113. strncat(file, "/", 1);
  7114. strcat(file, pEntry->d_name);
  7115. PRINTF_FUNC("zip = %s \n", file);
  7116. system(file);
  7117. }
  7118. }
  7119. }
  7120. }
  7121. }
  7122. // Close folder
  7123. closedir(pDir);
  7124. }
  7125. void ChangeGunSelectByIndex(byte sel)
  7126. {
  7127. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  7128. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  7129. }
  7130. void CheckIsAlternatvieByModelName()
  7131. {
  7132. if(ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'W')
  7133. {
  7134. // 壁掛
  7135. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  7136. }
  7137. else
  7138. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  7139. }
  7140. void StopProcessingLoop()
  7141. {
  7142. ocpp_process_start();
  7143. for (;;)
  7144. {
  7145. CheckTask();
  7146. CheckFactoryConfigFunction();
  7147. CheckFwUpdateFunction();
  7148. ocpp_chk_reset_cmd(YES);
  7149. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  7150. {
  7151. // 經過底下的 function 後,ShmSysConfigAndInfo->SysWarningInfo.Level 會再被更新一次
  7152. ChkPrimaryStatus();
  7153. ButtonTriggerEvent();
  7154. ReviewCriticalAlarm();
  7155. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_NORMAL &&
  7156. ShmDcCommonData->rebootCount < 1)
  7157. {
  7158. PRINTF_FUNC("Soft reboot for retry self-test (Primary). \n");
  7159. SetupRebootCount(1);
  7160. KillAllTask();
  7161. sleep(3);
  7162. system("/usr/bin/run_evse_restart.sh");
  7163. return;
  7164. }
  7165. }
  7166. sleep(1);
  7167. }
  7168. }
  7169. void CreateWatchdog()
  7170. {
  7171. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  7172. {
  7173. wtdFd = InitWatchDog();
  7174. if (wtdFd < 0)
  7175. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  7176. }
  7177. }
  7178. bool IsConnectorWholeIdle()
  7179. {
  7180. bool result = true;
  7181. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  7182. {
  7183. if (chargingInfo[count]->SystemStatus != SYS_MODE_IDLE &&
  7184. chargingInfo[count]->SystemStatus != SYS_MODE_RESERVATION)
  7185. {
  7186. result = false;
  7187. break;
  7188. }
  7189. }
  7190. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  7191. {
  7192. if (ac_chargingInfo[count]->SystemStatus != SYS_MODE_IDLE &&
  7193. ac_chargingInfo[count]->IsErrorOccur == NO)
  7194. {
  7195. result = false;
  7196. break;
  7197. }
  7198. }
  7199. return result;
  7200. }
  7201. void ClearAlarmCodeWhenAcOff()
  7202. {
  7203. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  7204. {
  7205. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  7206. }
  7207. }
  7208. //==========================================
  7209. // Check task processing
  7210. //==========================================
  7211. void CheckTask()
  7212. {
  7213. if(reset4gStep == RESET_4G_STEP_NONE &&
  7214. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' ||
  7215. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  7216. {
  7217. if(system("pidof -s Module_4g > /dev/null") != 0)
  7218. {
  7219. DEBUG_ERROR_MSG("Module_4g not running, restart it.\r\n");
  7220. system("/root/Module_4g &");
  7221. }
  7222. }
  7223. if(reset4gStep == RESET_4G_STEP_NONE &&
  7224. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' ||
  7225. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  7226. {
  7227. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  7228. {
  7229. DEBUG_ERROR_MSG("Module_Wifi not running, restart it.\r\n");
  7230. system("/root/Module_Wifi &");
  7231. }
  7232. }
  7233. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'P')
  7234. {
  7235. if(system("pidof -s Module_DcMeter > /dev/null") != 0)
  7236. {
  7237. DEBUG_ERROR_MSG("Module_DcMeter not running, restart it.\r\n");
  7238. system("/root/Module_DcMeter &");
  7239. }
  7240. }
  7241. ocpp_process_start();
  7242. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  7243. {
  7244. DEBUG_ERROR_MSG("Module_ProduceUtils not running, restart it.\r\n");
  7245. system ("/root/Module_ProduceUtils &");
  7246. }
  7247. }
  7248. void Check267Alarm()
  7249. {
  7250. bool canRun = true;
  7251. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7252. {
  7253. if((chargingInfo[_index]->SystemStatus > SYS_MODE_IDLE && chargingInfo[_index]->SystemStatus < SYS_MODE_TERMINATING) ||
  7254. (chargingInfo[_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7255. {
  7256. canRun = false;
  7257. break;
  7258. }
  7259. }
  7260. if (canRun)
  7261. {
  7262. if ( ! ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  7263. {
  7264. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  7265. }
  7266. }
  7267. }
  7268. void CheckSystemTaskAlive()
  7269. {
  7270. unsigned char lostId = CheckSystemTask(ShmSysConfigAndInfo->SysInfo.SystemPage);
  7271. if (lostId != 0)
  7272. {
  7273. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost == NO)
  7274. {
  7275. EmcOccurThenStopCharging();
  7276. sleep(2);
  7277. if (lostId == _SYSTEM_TASK_LOST_ITEM_MAIN) PRINTF_FUNC ( "System task lost (CSU). \n" );
  7278. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVCOMM)
  7279. {
  7280. PRINTF_FUNC ( "System task lost (EVComm). \n" );
  7281. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun ++)
  7282. {
  7283. chargingInfo [gun]->ConnectorPlugIn = NO;
  7284. }
  7285. }
  7286. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PSUCOMM) PRINTF_FUNC ( "System task lost (PSU Task). \n" );
  7287. else if (lostId == _SYSTEM_TASK_LOST_ITEM_SMARTBOX) PRINTF_FUNC ( "System task lost (SmartBox Task). \n" );
  7288. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVENTLOG) PRINTF_FUNC ( "System task lost (Event log). \n" );
  7289. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PRIMARYCOMM) PRINTF_FUNC ( "System task lost (Primary). \n" );
  7290. else if (lostId == _SYSTEM_TASK_LOST_ITEM_LCMCONTROL) PRINTF_FUNC ( "System task lost (LCM Comm). \n" );
  7291. else if (lostId == _SYSTEM_TASK_LOST_ITEM_INTERCOMM) PRINTF_FUNC("System task lost (Internal Comm). \n");
  7292. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = YES;
  7293. }
  7294. }
  7295. else
  7296. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = NO;
  7297. }
  7298. void InitialDHCP()
  7299. {
  7300. char tmpbuf[256];
  7301. memset(tmpbuf,0,256);
  7302. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  7303. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  7304. system(tmpbuf);
  7305. }
  7306. void Run4gResetProc()
  7307. {
  7308. if (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == _SYS_4G_MODE_ENABLE ||
  7309. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_STATION)
  7310. {
  7311. if (!ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  7312. !ShmSysConfigAndInfo->SysInfo.InternetConn)
  7313. {
  7314. if (reset4gStep == RESET_4G_STEP_NONE)
  7315. {
  7316. if ((ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES && _4gResetChkFlag >= 12) ||
  7317. _4gResetChkFlag >= 60)
  7318. {
  7319. PRINTF_FUNC("Reset 4G/Wifi module. \n");
  7320. reset4gStep = RESET_4G_STEP_DELETE_TASK;
  7321. }
  7322. else
  7323. _4gResetChkFlag++;
  7324. }
  7325. }
  7326. else
  7327. _4gResetChkFlag = 0;
  7328. switch(reset4gStep)
  7329. {
  7330. case RESET_4G_STEP_DELETE_TASK:
  7331. {
  7332. PRINTF_FUNC("Killall 4G / Wifi task. \n");
  7333. system("killall Module_4g &");
  7334. system("killall Module_Wifi &");
  7335. reset4gStep = RESET_4G_STEP_GPIO_HIGH;
  7336. }
  7337. break;
  7338. case RESET_4G_STEP_GPIO_HIGH:
  7339. {
  7340. PRINTF_FUNC("Disable 4G/Wifi. GPIO-104 -> High. \n");
  7341. system("echo 1 > /sys/class/gpio/gpio104/value");
  7342. reset4gStep = RESET_4G_STEP_GPIO_LOW_COMP;
  7343. }
  7344. break;
  7345. case RESET_4G_STEP_GPIO_LOW_COMP:
  7346. {
  7347. PRINTF_FUNC("Enable 4G/Wifi. GPIO-104 -> Low. \n");
  7348. system("echo 0 > /sys/class/gpio/gpio104/value");
  7349. reset4gStep = RESET_4G_STEP_NONE;
  7350. _4gResetChkFlag = 0;
  7351. }
  7352. break;
  7353. }
  7354. }
  7355. else
  7356. {
  7357. if (reset4gStep != RESET_4G_STEP_NONE)
  7358. system("echo 0 > /sys/class/gpio/gpio104/value");
  7359. reset4gStep = RESET_4G_STEP_NONE;
  7360. _4gResetChkFlag = 0;
  7361. }
  7362. }
  7363. //==========================================
  7364. // Check Smart Charging Profile
  7365. //==========================================
  7366. int GetStartScheduleTime(unsigned char *time)
  7367. {
  7368. int result = -1;
  7369. struct tm tmScheduleStart;
  7370. struct timeb tbScheduleStart;
  7371. 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))
  7372. {
  7373. tmScheduleStart.tm_year -= 1900;
  7374. tmScheduleStart.tm_mon -= 1;
  7375. tbScheduleStart.time = mktime(&tmScheduleStart);
  7376. tbScheduleStart.millitm = 0;
  7377. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  7378. }
  7379. return result;
  7380. }
  7381. //==========================================
  7382. // sub function
  7383. //==========================================
  7384. void CheckReturnToChargingConn()
  7385. {
  7386. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  7387. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  7388. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  7389. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  7390. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  7391. {
  7392. bool isReturnTimeout = false;
  7393. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  7394. {
  7395. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  7396. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7397. {
  7398. if ((chargingInfo[count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= SYS_MODE_COMPLETE) ||
  7399. (chargingInfo[count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7400. {
  7401. isReturnTimeout = false;
  7402. break;
  7403. }
  7404. }
  7405. else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7406. {
  7407. if ((chargingInfo[count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= SYS_MODE_COMPLETE) ||
  7408. (chargingInfo[count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7409. {
  7410. isReturnTimeout = true;
  7411. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  7412. }
  7413. }
  7414. }
  7415. // AC 槍
  7416. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  7417. {
  7418. // 沒有選中 AC,且 AC 在充電中
  7419. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  7420. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_COMPLETE))
  7421. {
  7422. // 當前 DC 充電槍在 idle 狀態
  7423. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE ||
  7424. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  7425. {
  7426. isReturnTimeout = true;
  7427. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  7428. }
  7429. }
  7430. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  7431. ((chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_COMPLETE) ||
  7432. (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)))
  7433. {
  7434. // 當前 DC 充電槍在 idle 狀態
  7435. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE ||
  7436. ac_chargingInfo[0]->SystemStatus == SYS_MODE_RESERVATION)
  7437. {
  7438. isReturnTimeout = true;
  7439. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  7440. }
  7441. }
  7442. }
  7443. if (!isReturnTimeout)
  7444. StopSystemTimeoutDet();
  7445. }
  7446. }
  7447. bool GetStartChargingByAlterMode(byte _gun)
  7448. {
  7449. bool result = true;
  7450. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2 &&
  7451. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  7452. {
  7453. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select++)
  7454. {
  7455. if (_select != _gun)
  7456. {
  7457. if (chargingInfo[_select]->SystemStatus != SYS_MODE_IDLE &&
  7458. chargingInfo[_select]->SystemStatus != SYS_MODE_RESERVATION)
  7459. {
  7460. result = false;
  7461. break;
  7462. }
  7463. }
  7464. }
  7465. }
  7466. return result;
  7467. }
  7468. void TheEndCharging(byte gun_index)
  7469. {
  7470. chargingInfo[gun_index]->isRemoteStart = NO;
  7471. StopGunInfoTimeoutDet(gun_index);
  7472. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  7473. ChangeStartOrStopDateTime(NO, gun_index);
  7474. DB_Insert_Record(localDb, gun_index);
  7475. }
  7476. void AdjustChargerCurrent()
  7477. {
  7478. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  7479. // 設定的電流~ 如超過可輸出的電流,則 bypass
  7480. if (ShmSysConfigAndInfo->SysConfig.RatingCurrent < ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent ||
  7481. ShmSysConfigAndInfo->SysConfig.RatingCurrent == 0)
  7482. {
  7483. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent = 0;
  7484. }
  7485. PRINTF_FUNC ("System : MaxChargingPower = %d (KW), MaxChargingCurrent = %d (A)",
  7486. ShmPsuData->SystemAvailablePower / 10,
  7487. ShmPsuData->SystemAvailableCurrent / 10
  7488. );
  7489. PRINTF_FUNC ("Web Config : ChargingPower = %d (KW), ChargingCurrent = %d (A)",
  7490. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  7491. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent
  7492. );
  7493. }
  7494. bool CheckTheSystemType()
  7495. {
  7496. bool result = false;
  7497. char EvsePower[2];
  7498. EvsePower[2] = '\0';
  7499. strncpy ( EvsePower, (char *) (ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2 );
  7500. ShmDcCommonData->halfGroupCount = 1;
  7501. if (strcmp ( EvsePower, "30" ) == EQUAL)
  7502. ShmDcCommonData->systemType = _SYSTEM_TYPE_SIMPLE;
  7503. else
  7504. {
  7505. // 60KW 以上的系統
  7506. if (ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity == 0 &&
  7507. ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity == 0)
  7508. ShmDcCommonData->systemType = _SYSTEM_TYPE_STANDARD;
  7509. else
  7510. {
  7511. ShmDcCommonData->halfGroupCount = 2;
  7512. ShmDcCommonData->systemType = _SYSTEM_TYPE_SMART;
  7513. }
  7514. if ((ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity != 0 && ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity == 0) ||
  7515. (ShmPsuData->PsuGroup[2].GroupPresentPsuQuantity == 0 && ShmPsuData->PsuGroup[3].GroupPresentPsuQuantity != 0))
  7516. {
  7517. result = true;
  7518. }
  7519. }
  7520. return result;
  7521. }
  7522. void CheckStatusOfMeter()
  7523. {
  7524. // 橋接如果搭上~ 則兩個電表都是使用狀態
  7525. // 如橋接沒有搭上~ 則視該火線 relay 狀態為電表使用狀態
  7526. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  7527. {
  7528. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++)
  7529. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  7530. }
  7531. else
  7532. {
  7533. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++)
  7534. {
  7535. if (chargingInfo[mIndex]->RelayKPK2Status == YES || chargingInfo[mIndex]->RelayK1K2Status)
  7536. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  7537. else
  7538. ShmCsuMeterData->_meter[mIndex].isCalculation = NO;
  7539. }
  7540. }
  7541. }
  7542. void ReviewCriticalAlarm()
  7543. {
  7544. if (ShmDcCommonData->GunRelayDrivingOccur[0] == YES ||
  7545. ShmDcCommonData->GunRelayDrivingOccur[1] == YES ||
  7546. ShmDcCommonData->GunRelayWeldingOccur[0] == YES ||
  7547. ShmDcCommonData->GunRelayWeldingOccur[1] == YES ||
  7548. ShmDcCommonData->ParaRelayWeldingOccur == YES ||
  7549. ShmDcCommonData->ParaRelayDrivingOccur == YES ||
  7550. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip ||
  7551. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip ||
  7552. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen ||
  7553. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  7554. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm ||
  7555. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU ||
  7556. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost)
  7557. {
  7558. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_CRITICAL;
  7559. }
  7560. else
  7561. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_NORMAL;
  7562. }
  7563. void CheckRelayWeldingOrDrivingFault(byte gun_index)
  7564. {
  7565. // relay welding fault then stop the charging process.
  7566. byte faultCode = RELAY_STATUS_ERROR_NONE;
  7567. if (gun_index == 0)
  7568. {
  7569. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7570. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7571. {
  7572. faultCode = RELAY_STATUS_ERROR_WELDING;
  7573. }
  7574. else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7575. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7576. {
  7577. faultCode = RELAY_STATUS_ERROR_DRIVING;
  7578. }
  7579. }
  7580. else if (gun_index == 1)
  7581. {
  7582. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7583. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7584. {
  7585. faultCode = RELAY_STATUS_ERROR_WELDING;
  7586. }
  7587. else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7588. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7589. {
  7590. faultCode = RELAY_STATUS_ERROR_DRIVING;
  7591. }
  7592. }
  7593. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2 &&
  7594. !ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  7595. {
  7596. // 橋接
  7597. if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7598. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  7599. ShmDcCommonData->RelayCheckStatus[RELAY_PRE_STATUS] == RELAY_STATUS_ERROR_WELDING)
  7600. {
  7601. faultCode = RELAY_STATUS_ERROR_PARA_WELDING;
  7602. }
  7603. else if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7604. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  7605. ShmDcCommonData->RelayCheckStatus[RELAY_PRE_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  7606. {
  7607. faultCode = RELAY_STATUS_ERROR_PARA_DRIVING;
  7608. }
  7609. }
  7610. if (faultCode == RELAY_STATUS_ERROR_WELDING)
  7611. {
  7612. // welding
  7613. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  7614. RecordAlarmCode(gun_index, "011011");
  7615. else if (chargingInfo[gun_index]->Type == _Type_GB)
  7616. RecordAlarmCode(gun_index, "011015");
  7617. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  7618. RecordAlarmCode(gun_index, "011013");
  7619. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = YES;
  7620. EmcOccurThenStopCharging();
  7621. }
  7622. else if (faultCode == RELAY_STATUS_ERROR_DRIVING)
  7623. {
  7624. // driving
  7625. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  7626. RecordAlarmCode(gun_index, "011012");
  7627. else if (chargingInfo[gun_index]->Type == _Type_GB)
  7628. RecordAlarmCode(gun_index, "011016");
  7629. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  7630. RecordAlarmCode(gun_index, "011014");
  7631. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = YES;
  7632. EmcOccurThenStopCharging();
  7633. }
  7634. else
  7635. {
  7636. if (chargingInfo[gun_index]->SystemStatus <= SYS_MODE_TERMINATING ||
  7637. chargingInfo[gun_index]->SystemStatus >= SYS_MODE_ALARM)
  7638. {
  7639. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = NO;
  7640. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = NO;
  7641. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  7642. {
  7643. ResetChargerAlarmCode(gun_index, "011012");
  7644. ResetChargerAlarmCode(gun_index, "011011");
  7645. }
  7646. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  7647. {
  7648. ResetChargerAlarmCode(gun_index, "011014");
  7649. ResetChargerAlarmCode(gun_index, "011013");
  7650. }
  7651. else if (chargingInfo[gun_index]->Type == _Type_GB)
  7652. {
  7653. ResetChargerAlarmCode(gun_index, "011016");
  7654. ResetChargerAlarmCode(gun_index, "011015");
  7655. }
  7656. }
  7657. }
  7658. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2)
  7659. {
  7660. if ((chargingInfo[0]->SystemStatus <= SYS_MODE_TERMINATING ||
  7661. chargingInfo[0]->SystemStatus >= SYS_MODE_ALARM) &&
  7662. (chargingInfo[1]->SystemStatus <= SYS_MODE_TERMINATING ||
  7663. chargingInfo[1]->SystemStatus >= SYS_MODE_ALARM))
  7664. {
  7665. if (faultCode == RELAY_STATUS_ERROR_PARA_WELDING)
  7666. {
  7667. // parallel welding
  7668. RecordAlarmCode ( gun_index, "011039" );
  7669. ShmDcCommonData->ParaRelayWeldingOccur = YES;
  7670. EmcOccurThenStopCharging ();
  7671. }
  7672. else if (faultCode == RELAY_STATUS_ERROR_PARA_DRIVING)
  7673. {
  7674. // parallel driving
  7675. RecordAlarmCode ( gun_index, "011040" );
  7676. ShmDcCommonData->ParaRelayDrivingOccur = YES;
  7677. EmcOccurThenStopCharging ();
  7678. }
  7679. else
  7680. {
  7681. ShmDcCommonData->ParaRelayWeldingOccur = NO;
  7682. ShmDcCommonData->ParaRelayDrivingOccur = NO;
  7683. ResetChargerAlarmCode ( gun_index, "011039" );
  7684. ResetChargerAlarmCode ( gun_index, "011040" );
  7685. }
  7686. }
  7687. }
  7688. }
  7689. bool PrecheckIsPass(byte gun_index)
  7690. {
  7691. bool result = true;
  7692. // relay welding or driving 是反向
  7693. if (result)
  7694. result = !ShmDcCommonData->GunRelayWeldingOccur[gun_index];
  7695. if (result)
  7696. result = !ShmDcCommonData->ParaRelayWeldingOccur;
  7697. return result;
  7698. }
  7699. void CollectError(byte gun_index)
  7700. {
  7701. errCollect.GunErrMessage |= ShmDcCommonData->ConnectErrList[gun_index].GunErrMessage;
  7702. }
  7703. void AlarmCheck(byte type)
  7704. {
  7705. // type : Cha = 0、CCS = 1、GBT = 2
  7706. if (type == _Type_Chademo)
  7707. {
  7708. // GFD Trip
  7709. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  7710. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  7711. else
  7712. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  7713. // UVP
  7714. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  7715. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  7716. else
  7717. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  7718. // OTP
  7719. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  7720. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = YES;
  7721. else
  7722. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = NO;
  7723. // OVP
  7724. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  7725. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = YES;
  7726. else
  7727. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = NO;
  7728. // GFD Warning
  7729. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  7730. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  7731. else
  7732. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  7733. // Relay Welding
  7734. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7735. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  7736. else
  7737. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = NO;
  7738. // Relay Driving
  7739. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7740. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  7741. else
  7742. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  7743. // Connect temp Sensor broken
  7744. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7745. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = YES;
  7746. else
  7747. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = NO;
  7748. // Output UCP
  7749. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 0))
  7750. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = YES;
  7751. else
  7752. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = NO;
  7753. // Parallel Relay Welding
  7754. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 3))
  7755. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  7756. else
  7757. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  7758. // Parallel Relay Driving
  7759. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 4))
  7760. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  7761. else
  7762. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  7763. // Stop By Evse Condition
  7764. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 5))
  7765. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = YES;
  7766. else
  7767. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = NO;
  7768. }
  7769. else if (type == _Type_CCS)
  7770. {
  7771. // GFD Trip
  7772. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  7773. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  7774. else
  7775. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  7776. // UVP
  7777. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  7778. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  7779. else
  7780. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  7781. // OTP
  7782. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  7783. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = YES;
  7784. else
  7785. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = NO;
  7786. // OVP
  7787. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  7788. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = YES;
  7789. else
  7790. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = NO;
  7791. // GFD Warning
  7792. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  7793. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  7794. else
  7795. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  7796. // Relay Welding
  7797. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7798. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  7799. else
  7800. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = NO;
  7801. // Relay Driving
  7802. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7803. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  7804. else
  7805. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  7806. // Connect temp Sensor broken
  7807. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7808. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = YES;
  7809. else
  7810. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = NO;
  7811. // Output UCP
  7812. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 1))
  7813. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = YES;
  7814. else
  7815. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = NO;
  7816. // Parallel Relay Welding
  7817. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 3))
  7818. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  7819. else
  7820. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  7821. // Parallel Relay Driving
  7822. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 4))
  7823. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  7824. else
  7825. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  7826. // Stop By Evse Condition
  7827. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 5))
  7828. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = YES;
  7829. else
  7830. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = NO;
  7831. }
  7832. else if (type == _Type_GB)
  7833. {
  7834. // GFD Trip
  7835. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  7836. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  7837. else
  7838. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  7839. // UVP
  7840. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  7841. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  7842. else
  7843. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  7844. // OTP
  7845. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  7846. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = YES;
  7847. else
  7848. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = NO;
  7849. // OVP
  7850. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  7851. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = YES;
  7852. else
  7853. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = NO;
  7854. // GFD Warning
  7855. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  7856. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  7857. else
  7858. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  7859. // Relay Welding
  7860. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7861. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  7862. else
  7863. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = NO;
  7864. // Relay Driving
  7865. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7866. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  7867. else
  7868. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  7869. // Connect temp Sensor broken
  7870. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7871. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = YES;
  7872. else
  7873. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = NO;
  7874. // Output UCP
  7875. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 2))
  7876. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = YES;
  7877. else
  7878. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = NO;
  7879. // Parallel Relay Welding
  7880. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 3))
  7881. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  7882. else
  7883. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  7884. // Parallel Relay Driving
  7885. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 4))
  7886. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  7887. else
  7888. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  7889. // Stop By Evse Condition
  7890. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 5))
  7891. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = YES;
  7892. else
  7893. ShmStatusCodeData->InfoCode.InfoEvents.bits.Stop_by_EVSE_condition = NO;
  7894. }
  7895. }
  7896. void SystemAlarmCheck()
  7897. {
  7898. byte system = 3;
  7899. // Parallel Welding
  7900. if (DetectBitValue(errCollect.GunErrMessage >> (8 * system), 3))
  7901. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = YES;
  7902. else
  7903. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayWelding = NO;
  7904. // Parallel Driving
  7905. if (DetectBitValue(errCollect.GunErrMessage >> (8 * system), 4))
  7906. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = YES;
  7907. else
  7908. ShmStatusCodeData->FaultCode.FaultEvents.bits.ParallelRelayDriving = NO;
  7909. }
  7910. void SavePresentChargedDuration(byte gun_index)
  7911. {
  7912. int _diffTime = 0;
  7913. ftime(&endChargingTime[gun_index]);
  7914. _diffTime = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  7915. // below 0 or over 5 sec is abnormal
  7916. if (_diffTime < 0 ||
  7917. _diffTime > chargingInfo[gun_index]->PresentChargedDuration + 5)
  7918. {
  7919. _presentChargingTimeBuf[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  7920. ftime(&startChargingTime[gun_index]);
  7921. PRINTF_FUNC("TimeZone changed (%d) \n", gun_index);
  7922. }
  7923. else
  7924. chargingInfo[gun_index]->PresentChargedDuration = _presentChargingTimeBuf[gun_index] + _diffTime;
  7925. }
  7926. void ClearEnergyTimePeriod(byte gun_index)
  7927. {
  7928. for (int i = 0; i < 24; i++)
  7929. {
  7930. ShmDcCommonData->energy_time_period[gun_index][i] = 0;
  7931. }
  7932. }
  7933. void CheckOcppCostAndPrice()
  7934. {
  7935. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling &&
  7936. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  7937. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  7938. {
  7939. ocpp_parsing_DT_defaultPrice_cmd();
  7940. ocpp_parsing_DT_userPrice_cmd();
  7941. ocpp_parsing_DT_running_final_cmd();
  7942. }
  7943. }
  7944. void CheckAuthrizeByEvccid()
  7945. {
  7946. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  7947. ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID)
  7948. ShmDcCommonData->enObtainAuthorizbyCCID = YES;
  7949. else
  7950. ShmDcCommonData->enObtainAuthorizbyCCID = NO;
  7951. }
  7952. void ConnectorOTP(byte _index)
  7953. {
  7954. bool isOTP = false;
  7955. if (chargingInfo[_index]->Type == _Type_Chademo)
  7956. {
  7957. if (ShmDcCommonData->ConnectErrList[_index].GunBits.ChaConnectOTP)
  7958. isOTP = true;
  7959. }
  7960. else if (chargingInfo[_index]->Type == _Type_CCS)
  7961. {
  7962. if (ShmDcCommonData->ConnectErrList[_index].GunBits.CCSConnectOTP)
  7963. isOTP = true;
  7964. }
  7965. else if (chargingInfo[_index]->Type == _Type_GB)
  7966. {
  7967. if (ShmDcCommonData->ConnectErrList[_index].GunBits.GBTConnectOTP)
  7968. isOTP = true;
  7969. }
  7970. if (isOTP)
  7971. {
  7972. if (gunTempAllowCount[_index] >= 1)
  7973. chargingInfo[_index]->StopChargeFlag = YES;
  7974. else
  7975. gunTempAllowCount[_index] += 1;
  7976. }
  7977. }
  7978. void CheckConnectorOTP(byte _index)
  7979. {
  7980. if (chargingInfo[_index]->Type == _Type_Chademo &&
  7981. chargingInfo[_index]->ModelType == 'K')
  7982. {
  7983. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  7984. {
  7985. ResetChargerAlarmCode(_index, "011018");
  7986. if (chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7987. RecordAlarmCode(_index, "012229");
  7988. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  7989. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7990. ResetChargerAlarmCode(_index, "012229");
  7991. }
  7992. else
  7993. {
  7994. // 沒接上 Sensor or 異常
  7995. RecordAlarmCode(_index, "011018");
  7996. ResetChargerAlarmCode(_index, "012229");
  7997. }
  7998. }
  7999. else if (chargingInfo[_index]->Type == _Type_CCS)
  8000. {
  8001. // CCS 不管甚麼輸出都會有槍溫偵測!!~
  8002. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  8003. {
  8004. ResetChargerAlarmCode(_index, "011019");
  8005. if(chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  8006. RecordAlarmCode(_index, "012230");
  8007. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  8008. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  8009. ResetChargerAlarmCode(_index, "012230");
  8010. }
  8011. else
  8012. {
  8013. // 沒接上 Sensor or 異常
  8014. RecordAlarmCode(_index, "011019");
  8015. ResetChargerAlarmCode(_index, "012230");
  8016. }
  8017. }
  8018. else if (chargingInfo[_index]->Type == _Type_GB &&
  8019. chargingInfo[_index]->ModelType == 'B')
  8020. {
  8021. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  8022. {
  8023. ResetChargerAlarmCode(_index, "011020");
  8024. if(chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  8025. RecordAlarmCode(_index, "012231");
  8026. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  8027. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  8028. ResetChargerAlarmCode(_index, "012231");
  8029. }
  8030. else
  8031. {
  8032. // 沒接上 Sensor or 異常
  8033. RecordAlarmCode(_index, "011020");
  8034. ResetChargerAlarmCode(_index, "012231");
  8035. }
  8036. }
  8037. ConnectorOTP(_index);
  8038. }
  8039. void ChkConnectorBoardStop(byte index)
  8040. {
  8041. if (isEvBoardStopChargeFlag(index) == YES)
  8042. {
  8043. // 板端要求停止 (錯誤)
  8044. ocpp_set_stopReason_by_cmd(index, "EVDisconnected");
  8045. ChargingAlarmProcess(index);
  8046. }
  8047. else if (isEvBoardNormalStopChargeFlag(index) == YES)
  8048. {
  8049. // 板端要求停止 (正常)
  8050. ocpp_set_stopReason_by_cmd(index, "EVDisconnected");
  8051. ChargingTerminalProcess(index);
  8052. }
  8053. }
  8054. void ChkNoCharging(byte index)
  8055. {
  8056. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  8057. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  8058. {
  8059. PRINTF_FUNC("Offline Policy : No Charing... \n");
  8060. ocpp_set_stopReason_by_cmd(index, "Other");
  8061. ChargingTerminalProcess(index);
  8062. }
  8063. }
  8064. void ChkPsuErrorStop(byte index)
  8065. {
  8066. if (ShmPsuData->PsuGroup[index].GroupErrorFlag.bits.PsuFailure == YES ||
  8067. (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf && ShmPsuData->PsuGroup[0].GroupErrorFlag.bits.PsuFailure))
  8068. {
  8069. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8070. {
  8071. if (chargingInfo[gun_index]->PresentChargingCurrent <= 5 &&
  8072. ((chargingInfo[gun_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  8073. (chargingInfo[gun_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[gun_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)))
  8074. {
  8075. ocpp_set_stopReason_by_cmd(gun_index, "Other");
  8076. ChargingAlarmProcess(gun_index);
  8077. }
  8078. }
  8079. }
  8080. }
  8081. void ChkBackendStop(byte index)
  8082. {
  8083. if (ocpp_chk_invalid_id_cmd(index))
  8084. {
  8085. // Start Transaction Stop
  8086. ocpp_set_stopReason_by_cmd(index, "DeAuthorized");
  8087. ChargingTerminalProcess(index);
  8088. }
  8089. else if (ocpp_chk_remoteStop_cmd(index) == YES ||
  8090. WifiScheduleStop(index))
  8091. {
  8092. // 後臺要求停止
  8093. ChargingTerminalProcess(index);
  8094. }
  8095. }
  8096. void ChkConfigurationStop(byte index)
  8097. {
  8098. if (CheckWebConfigChargingTimeout(index) ||
  8099. CheckWebConfigChargingEnergy(index) ||
  8100. CheckWebConfigChargingSOC(index))
  8101. {
  8102. // 網頁設定
  8103. PRINTF_FUNC("CheckBackendCharging stop charging (%d) \n", index);
  8104. RecordAlarmCode(index, "023984");
  8105. ChargingTerminalProcess(index);
  8106. return;
  8107. }
  8108. if (CheckOcppChargingTimeout(index) ||
  8109. CheckOcppChargingEnergy(index) ||
  8110. CheckOcppChargingSOC(index))
  8111. {
  8112. // OCPP 設定
  8113. PRINTF_FUNC("CheckOcppCharging stop charging (%d) \n", index);
  8114. RecordAlarmCode(index, "023984");
  8115. ChargingTerminalProcess(index);
  8116. }
  8117. }
  8118. void RecordGfdResult(byte index)
  8119. {
  8120. if (chargingInfo[index]->Type == _Type_Chademo)
  8121. {
  8122. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  8123. {
  8124. // GFD 錯誤停止
  8125. RecordAlarmCode(index, "012234");
  8126. ocpp_set_stopReason_by_cmd(index, "Other");
  8127. ChargingAlarmProcess(index);
  8128. }
  8129. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  8130. {
  8131. // GFD 警告
  8132. RecordAlarmCode(index, "012296");
  8133. }
  8134. }
  8135. else if (chargingInfo[index]->Type == _Type_GB)
  8136. {
  8137. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  8138. {
  8139. // GFD 錯誤停止
  8140. RecordAlarmCode(index, "012236");
  8141. ocpp_set_stopReason_by_cmd ( index, "Other" );
  8142. ChargingAlarmProcess ( index );
  8143. }
  8144. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  8145. {
  8146. // GFD 警告
  8147. RecordAlarmCode(index, "012298");
  8148. }
  8149. }
  8150. else if (chargingInfo[index]->Type == _Type_CCS)
  8151. {
  8152. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  8153. {
  8154. // GFD 錯誤停止
  8155. RecordAlarmCode(index, "012235");
  8156. ocpp_set_stopReason_by_cmd(index, "Other");
  8157. ChargingAlarmProcess ( index );
  8158. }
  8159. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  8160. {
  8161. // GFD 警告
  8162. RecordAlarmCode(index, "012297");
  8163. }
  8164. }
  8165. }
  8166. void CcidAuthorizationProcessing(byte gun_index)
  8167. {
  8168. if (chargingInfo [gun_index]->Type == _Type_CCS &&
  8169. chargingInfo [gun_index]->IsAvailable &&
  8170. ShmDcCommonData->enObtainAuthorizbyCCID &&
  8171. chargingInfo[gun_index]->ConnectorPlugIn &&
  8172. strcmp ((char *)ShmSysConfigAndInfo->SysConfig.UserId, "") == EQUAL)
  8173. {
  8174. AddPlugInTimes(gun_index);
  8175. PRINTF_FUNC("CcidAuthorizationProcessing. \n");
  8176. ChangeGunSelectByIndex(gun_index);
  8177. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8178. }
  8179. }
  8180. void CheckCcidAuthorizationFunction(byte gun_index)
  8181. {
  8182. if (chargingInfo [gun_index]->Type == _Type_CCS &&
  8183. ShmDcCommonData->enObtainAuthorizbyCCID)
  8184. {
  8185. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_GET &&
  8186. ShmDcCommonData->_CcidAuthProcessStep == _CCID_NONE &&
  8187. strcmp ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" ) == EQUAL)
  8188. {
  8189. PRINTF_FUNC ( "index = %d, Get for CCID (main). \n", gun_index );
  8190. ShmDcCommonData->_authWithCcidFlag [gun_index] = _CCID_CHECK;
  8191. ShmDcCommonData->_CcidAuthProcessStep = _CCID_CHECK;
  8192. //LWADD_EVCCID
  8193. if(strcmp((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[EVCCID_PREFIX].ItemData, "") != EQUAL)
  8194. {
  8195. strcpy((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[EVCCID_PREFIX].ItemData);
  8196. strcat((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)chargingInfo [gun_index]->EVCCID);
  8197. PRINTF_FUNC ( "Prfix CCID:%s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  8198. }
  8199. else
  8200. strcpy((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)chargingInfo[gun_index]->EVCCID);
  8201. }
  8202. else if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_CHECK)
  8203. {
  8204. if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHFAIL)
  8205. {
  8206. PRINTF_FUNC ( "index = %d, _CCID_AUTHFAIL. \n", gun_index );
  8207. ShmDcCommonData->_authWithCcidFlag [gun_index] = ShmDcCommonData->_CcidAuthProcessStep;
  8208. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  8209. }
  8210. else if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHCOMP)
  8211. {
  8212. PRINTF_FUNC ( "index = %d, _CCID_AUTHCOMP. \n", gun_index );
  8213. ShmDcCommonData->_authWithCcidFlag [gun_index] = ShmDcCommonData->_CcidAuthProcessStep;
  8214. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  8215. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8216. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  8217. }
  8218. }
  8219. }
  8220. }
  8221. bool isCcidEnterToCharging(byte gun_index)
  8222. {
  8223. bool result = false;
  8224. if (ShmDcCommonData->enObtainAuthorizbyCCID)
  8225. {
  8226. if (ShmDcCommonData->_CcidAuthProcessStep == _CCID_AUTHCOMP)
  8227. {
  8228. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8229. {
  8230. if (chargingInfo [_index]->Type == _Type_CCS &&
  8231. chargingInfo [_index]->IsAvailable &&
  8232. ShmDcCommonData->_authWithCcidFlag [_index] == _CCID_CHECK)
  8233. {
  8234. if (_index != gun_index)
  8235. result = true;
  8236. break;
  8237. }
  8238. }
  8239. if (!result)
  8240. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  8241. }
  8242. }
  8243. return result;
  8244. }
  8245. bool IsNoneSelectAcModule()
  8246. {
  8247. bool result = false;
  8248. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0 ||
  8249. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  8250. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE))
  8251. {
  8252. result = true;
  8253. }
  8254. return result;
  8255. }
  8256. bool IsSelectAcModule()
  8257. {
  8258. bool result = false;
  8259. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  8260. return result;
  8261. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  8262. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  8263. {
  8264. result = true;
  8265. }
  8266. return result;
  8267. }
  8268. void DisplayPageChkByAcModule()
  8269. {
  8270. if (IsSelectAcModule())
  8271. {
  8272. if (ac_chargingInfo [0]->ConnectorPlugIn == AC_SYS_A)
  8273. {
  8274. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8275. }
  8276. else
  8277. {
  8278. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8279. }
  8280. }
  8281. }
  8282. void UpdateDcMeterPowerConsumption(byte _index)
  8283. {
  8284. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'P')
  8285. {
  8286. if (chargingInfo[_index]->PowerConsumption != ShmSysConfigAndInfo->SysInfo.DcMeterInfo[_index].totlizeImportEnergy)
  8287. chargingInfo[_index]->PowerConsumption = ShmSysConfigAndInfo->SysInfo.DcMeterInfo[_index].totlizeImportEnergy;
  8288. }
  8289. else if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
  8290. ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L')
  8291. {
  8292. if(chargingInfo[_index]->PowerConsumption != ShmCsuMeterData->_meter[_index]._curTotalCharging / 100)
  8293. chargingInfo[_index]->PowerConsumption = ShmCsuMeterData->_meter[_index]._curTotalCharging / 100;
  8294. }
  8295. }
  8296. int PresentChargedAcEnergyClear(unsigned char gun_index)
  8297. {
  8298. int result = FAIL;
  8299. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  8300. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod)*sizeof(float));
  8301. result = PASS;
  8302. return result;
  8303. }
  8304. void CheckTcpTimeout()
  8305. {
  8306. int _tpcTimebuf = 0;
  8307. _tpcTimebuf = GetTimeoutValue(&_tcpDetectStatus_time);
  8308. if (_tpcTimebuf < 0)
  8309. GetTimespecFunc(&_tcpDetectStatus_time);
  8310. else
  8311. {
  8312. if (_tpcTimebuf >= TPC_PLUG_DETECT_TIMEOUT)
  8313. {
  8314. _DetectTpcPlugInTimeout();
  8315. }
  8316. }
  8317. }
  8318. int main(void)
  8319. {
  8320. if(CreateShareMemory() == 0)
  8321. {
  8322. #ifdef SystemLogMessage
  8323. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  8324. #endif
  8325. if(ShmStatusCodeData!=NULL)
  8326. {
  8327. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  8328. }
  8329. sleep(5);
  8330. system("reboot -f");
  8331. sleep(5);
  8332. system("reboot -f");
  8333. }
  8334. PRINTF_FUNC("===== Boot and Initialization start ===== \n");
  8335. if (!InitialSystemDefaultConfig())
  8336. {
  8337. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  8338. StopProcessingLoop();
  8339. }
  8340. PRINTF_FUNC("CheckConnectorTypeStatus and CheckIsAlternatvie \n");
  8341. CheckGunTypeFromHw();
  8342. CheckIsAlternatvieByModelName();
  8343. InitialShareMemoryInfo();
  8344. PRINTF_FUNC("InitialShareMemoryInfo OK. \n");
  8345. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  8346. PRINTF_FUNC("ChargerType (IEC or UL) = IEC \n");
  8347. else
  8348. PRINTF_FUNC("ChargerType (IEC or UL) = UL \n");
  8349. ChangeLcmByIndex(_LCM_INIT);
  8350. if (!CheckConnectorTypeStatus())
  8351. isModelNameMatch = false;
  8352. Initialization();
  8353. PRINTF_FUNC("===== Initial Complete ===== \n");
  8354. PRINTF_FUNC("===== Spawn all Task and self test start ===== \n");
  8355. SpawnTask();
  8356. if (!isModelNameMatch)
  8357. {
  8358. // Module Name 與硬體對應不正確
  8359. PRINTF_FUNC("*** Module Name & HW info none match : Please check if the Model name and HW are matched. ***\n");
  8360. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  8361. ChangeLcmByIndex(_LCM_FIX);
  8362. sleep(3);
  8363. KillTask();
  8364. StopProcessingLoop();
  8365. }
  8366. PRINTF_FUNC("Module Name & HW info correct. \n");
  8367. CreateTimeoutFork();
  8368. if (!DEBUG_ALSTON)
  8369. {
  8370. PRINTF_FUNC("Self test run..\n");
  8371. SelfTestRun();
  8372. }
  8373. else
  8374. {
  8375. sleep(3);
  8376. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  8377. }
  8378. StopSystemTimeoutDet();
  8379. if (!DEBUG_ALSTON &&
  8380. (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  8381. ShmPsuData->Work_Step == _NO_WORKING ||
  8382. ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES ||
  8383. strcmp((char *)ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ") == EQUAL))
  8384. {
  8385. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  8386. {
  8387. if (!DisplaySelfTestFailReason() &&
  8388. ShmDcCommonData->rebootCount < 1)
  8389. {
  8390. PRINTF_FUNC("=======Soft reboot for retry self-test======= \n");
  8391. SetupRebootCount(1);
  8392. sleep(3);
  8393. KillAllTask();
  8394. system("/usr/bin/run_evse_restart.sh");
  8395. }
  8396. else if (ShmDcCommonData->rebootCount < 1)
  8397. {
  8398. PRINTF_FUNC("=======Soft reboot for retry self-test (Normal)======= \n");
  8399. SetupRebootCount(1);
  8400. sleep(3);
  8401. KillAllTask();
  8402. system("/usr/bin/run_evse_restart.sh");
  8403. }
  8404. else
  8405. SetupRebootCount(0);
  8406. }
  8407. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8408. {
  8409. ChargingAlarmProcess(gun_index);
  8410. }
  8411. //ChangeLcmByIndex(_LCM_FIX);
  8412. sleep(3);
  8413. //if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  8414. //{
  8415. KillTaskExceptDetectTask();
  8416. //}
  8417. //else
  8418. // KillTask();
  8419. StopProcessingLoop();
  8420. }
  8421. else
  8422. {
  8423. //lwmark
  8424. // for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8425. // {
  8426. // SetChargerMode(gun_index, SYS_MODE_IDLE);
  8427. // }
  8428. SetupRebootCount(0);
  8429. }
  8430. PRINTF_FUNC("===== Self Test Complete ===== \n");
  8431. PRINTF_FUNC("===== Finally Create DB ===== \n");
  8432. // Local DB
  8433. if(DB_Open(localDb) != PASS)
  8434. {
  8435. PRINTF_FUNC("DB_Open fail. \n");
  8436. isDb_ready = false;
  8437. }
  8438. else
  8439. {
  8440. isDb_ready = true;
  8441. for (int _acIndex = 0; _acIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _acIndex++)
  8442. {
  8443. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  8444. ac_chargingInfo[0]->IsAvailable = DB_Get_Operactive(localDb, _acIndex + hasDc);
  8445. }
  8446. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  8447. {
  8448. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  8449. chargingInfo[_index]->PowerConsumption = DB_Get_PowerConsumption(localDb, _index);
  8450. }
  8451. DB_Reboot_Record(localDb);
  8452. }
  8453. //lwmark
  8454. for (int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index ++)
  8455. {
  8456. if (chargingInfo [_index]->IsAvailable == NO)
  8457. SetChargerMode ( _index, SYS_MODE_MAINTAIN );
  8458. else
  8459. SetChargerMode ( _index, SYS_MODE_IDLE );
  8460. }
  8461. if (NetworkDB_Open(networkDb) != PASS)
  8462. {
  8463. PRINTF_FUNC("NetworkDB_Open fail. \n");
  8464. }
  8465. ChangeLcmByIndex(_LCM_IDLE);
  8466. sleep(1);
  8467. CreateRfidFork();
  8468. sleep(1);
  8469. if (!DEBUG_ALSTON)
  8470. {
  8471. PRINTF_FUNC("===== Create chk system Task ===== \n");
  8472. CreateCheckSystemTaskFork();
  8473. }
  8474. PRINTF_FUNC("===== Create DB End ===== \n");
  8475. if (CheckTheSystemType())
  8476. {
  8477. PRINTF_FUNC("Can't confirm the system type. (reboot) \n");
  8478. sleep(3);
  8479. KillAllTask();
  8480. system("/usr/bin/run_evse_restart.sh");
  8481. }
  8482. PRINTF_FUNC("===== Charger info ===== ");
  8483. PRINTF_FUNC("SW Version = %s", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  8484. PRINTF_FUNC("Bootloader = %s", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  8485. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  8486. InfoSystemType();
  8487. InfoLcmVersion();
  8488. InfoFwSlotStatusLog();
  8489. AdjustChargerCurrent();
  8490. PRINTF_FUNC("===== Charger info End ===== ");
  8491. GetTimespecFunc(&_cmdMainPriority_time);
  8492. _ocppProfileChkFlag_1 = 0;
  8493. _ocppProfileChkFlag_2 = 0;
  8494. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") == EQUAL)
  8495. {
  8496. DEBUG_ERROR_MSG("URL is empty kill Module_OcppBackend...\n");
  8497. system ("pkill OcppBackend");
  8498. }
  8499. int _mainTimebuf = 0;
  8500. for (;;)
  8501. {
  8502. if (ShmDcCommonData->_isAutoRunTest != _isTestRun)
  8503. {
  8504. if (ShmDcCommonData->_isAutoRunTest == YES)
  8505. {
  8506. GetTimespecFunc(&_autoRuntestTime);
  8507. }
  8508. _isTestRun = ShmDcCommonData->_isAutoRunTest;
  8509. }
  8510. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8511. {
  8512. ocpp_ac_chk_availability_cmd();
  8513. ocpp_ac_chk_unlock_cmd();
  8514. }
  8515. ocpp_auto_response_BootNotification();
  8516. ocpp_chk_reset_cmd(NO);
  8517. ButtonTriggerEvent();
  8518. if (!DEBUG_ALSTON)
  8519. ChkPrimaryStatus();
  8520. if ((IsConnectorWholeIdle() ||
  8521. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX))
  8522. {
  8523. CheckFactoryConfigFunction();
  8524. CheckFwUpdateFunction();
  8525. }
  8526. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  8527. {
  8528. sleep(1);
  8529. continue;
  8530. }
  8531. // OCPP 邏輯
  8532. ocpp_chk_remoteStart_cmd();
  8533. // 讀卡邏輯
  8534. ScannerCardProcess();
  8535. // 當 AC 沒有搭上時,清除一些錯誤碼
  8536. ClearAlarmCodeWhenAcOff();
  8537. // 確認是否要回到充電中的槍畫面邏輯判斷
  8538. CheckReturnToChargingConn();
  8539. if ((_acgunIndex > 0 && isDetectPlugin() && !isCardScan))
  8540. {
  8541. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8542. }
  8543. _mainTimebuf = GetTimeoutValue(&_cmdMainPriority_time);
  8544. if (ShmDcCommonData->_tpcPlugDetectState == _TPC_PLUG_DETECT_STATUS_WAIT)
  8545. CheckTcpTimeout();
  8546. if (_mainTimebuf < 0)
  8547. GetTimespecFunc(&_cmdMainPriority_time);
  8548. if (_mainTimebuf >= 5)
  8549. {
  8550. if (ShmPsuData->PsuStopChargeFlag)
  8551. Check267Alarm();
  8552. CheckTask();
  8553. Run4gResetProc();
  8554. bool isBothCharging = true;
  8555. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  8556. {
  8557. UpdateDcMeterPowerConsumption(_index);
  8558. if ((chargingInfo[_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  8559. (chargingInfo[_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  8560. {
  8561. ocpp_chargingProfile_process(_index);
  8562. if (chargingInfo[_index]->SystemStatus == SYS_MODE_CHARGING &&
  8563. ((_index == 0 && _ocppProfileChkFlag_1 >= 12) ||
  8564. (_index == 1 && _ocppProfileChkFlag_2 >= 12)))
  8565. {
  8566. ocpp_lift_profileReq_cmd(_index);
  8567. if (_index == 0)
  8568. _ocppProfileChkFlag_1 = 0;
  8569. else if (_index == 1)
  8570. _ocppProfileChkFlag_2 = 0;
  8571. }
  8572. else if (chargingInfo[_index]->SystemStatus != SYS_MODE_CHARGING)
  8573. {
  8574. ocpp_lift_profileReq_cmd(_index);
  8575. if (_index == 0)
  8576. _ocppProfileChkFlag_1 = 0;
  8577. else if (_index == 1)
  8578. _ocppProfileChkFlag_2 = 0;
  8579. }
  8580. else
  8581. {
  8582. if (_index == 0)
  8583. _ocppProfileChkFlag_1++;
  8584. else if (_index == 1)
  8585. _ocppProfileChkFlag_2++;
  8586. }
  8587. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  8588. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  8589. {
  8590. DB_Update_PowerConsumption(localDb, _index, chargingInfo[_index]->PowerConsumption);
  8591. }
  8592. }
  8593. if (chargingInfo[_index]->SystemStatus == SYS_MODE_RESERVATION)
  8594. {
  8595. if (opcc_chk_reserve_expired(_index))
  8596. {
  8597. chargingInfo[_index]->SystemStatus = SYS_MODE_IDLE;
  8598. }
  8599. }
  8600. // OTP
  8601. CheckConnectorOTP(_index);
  8602. if (ShmSmartBoxData->ConnectorStatus[_index].ConnectorStaus != _CONNECTOR_STATUS_USING)
  8603. isBothCharging = false;
  8604. }
  8605. if (isBothCharging)
  8606. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_BOTH;
  8607. else
  8608. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_NORMAL;
  8609. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  8610. {
  8611. if ((ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING) ||
  8612. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  8613. {
  8614. ocpp_ac_chargingProfile_process();
  8615. }
  8616. if ((ac_chargingInfo[_index]->SystemStatus >= SYS_MODE_PREPARING &&
  8617. ac_chargingInfo[_index]->SystemStatus <= SYS_MODE_CHARGING))
  8618. {
  8619. if (ac_chargingInfo[_index]->SystemStatus == SYS_MODE_CHARGING &&
  8620. _ac_ocppProfileChkFlag == 12)
  8621. {
  8622. ocpp_ac_lift_profileReq_cmd(_index);
  8623. _ac_ocppProfileChkFlag = 0;
  8624. }
  8625. else if (ac_chargingInfo[_index]->SystemStatus != SYS_MODE_CHARGING)
  8626. {
  8627. ocpp_ac_lift_profileReq_cmd(_index);
  8628. _ac_ocppProfileChkFlag = 0;
  8629. }
  8630. else
  8631. _ac_ocppProfileChkFlag++;
  8632. }
  8633. }
  8634. CheckOcppCostAndPrice();
  8635. CheckAuthrizeByEvccid();
  8636. GetTimespecFunc(&_cmdMainPriority_time);
  8637. if (!DEBUG_ALSTON && wtdFd == -1)
  8638. {
  8639. PRINTF_FUNC("===== Create crazy dog ===== \n");
  8640. CreateWatchdog();
  8641. }
  8642. }
  8643. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
  8644. ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L')
  8645. CheckStatusOfMeter();
  8646. // 確認當前錯誤 Level = 2 ?
  8647. ReviewCriticalAlarm();
  8648. errCollect.GunErrMessage = 0;
  8649. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8650. {
  8651. CheckAcOcppProfileConfirm();
  8652. ocpp_ac_chk_reserved_cmd();
  8653. }
  8654. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8655. {
  8656. //CheckGpioInStatus();
  8657. // 確認系統的錯誤狀態
  8658. CheckSystemErrorFunction(gun_index);
  8659. // 確認 Relay Welding or Driving Fault
  8660. CheckRelayWeldingOrDrivingFault(gun_index);
  8661. // 收集各槍的錯誤狀態
  8662. CollectError(gun_index);
  8663. ocpp_chk_reserved_cmd(gun_index);
  8664. ocpp_chk_availability_cmd(gun_index);
  8665. ocpp_chk_unlock_cmd(gun_index);
  8666. ocpp_default_errorCode_cmd(gun_index);
  8667. // check charging profile confirm flag
  8668. CheckOcppProfileConfirm(gun_index);
  8669. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  8670. switch(chargingInfo[gun_index]->SystemStatus)
  8671. {
  8672. case SYS_MODE_IDLE:
  8673. case SYS_MODE_RESERVATION:
  8674. case SYS_MODE_MAINTAIN:
  8675. case SYS_MODE_FAULT:
  8676. {
  8677. if ((chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE ||
  8678. chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION) &&
  8679. isModeChange(gun_index))
  8680. {
  8681. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  8682. {
  8683. PRINTF_FUNC("================= SYS_MODE_IDLE (%x) ================ \n", gun_index);
  8684. chargingInfo[gun_index]->ReservationId = NO_RESERVE;
  8685. }
  8686. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  8687. {
  8688. PRINTF_FUNC("================= SYS_MODE_RESERVATION (%x) ================ \n", gun_index);
  8689. PRINTF_FUNC("================= Id : %s ================ \n", ShmDcCommonData->_reserved_UserId[gun_index]);
  8690. }
  8691. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_MAINTAIN)
  8692. {
  8693. PRINTF_FUNC("================= SYS_MODE_MAINTAIN (%x) ================ \n", gun_index);
  8694. }
  8695. chargingInfo[gun_index]->PresentChargedDuration = 0;
  8696. chargingInfo[gun_index]->RemainChargingDuration = 0;
  8697. chargingInfo[gun_index]->PresentChargingVoltage = 0;
  8698. chargingInfo[gun_index]->PresentChargingCurrent = 0;
  8699. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "00:00:00");
  8700. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "00:00:00");
  8701. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  8702. strcpy((char *)chargingInfo[gun_index]->EVCCID, "");
  8703. _isSendStartTransaction[gun_index] = false;
  8704. ocpp_auto_response_StopTransationConf(gun_index);
  8705. ocpp_auto_response_StopTransationConfPH(gun_index);
  8706. ClearEnergyTimePeriod(gun_index);
  8707. ocpp_clear_stopReason(gun_index);
  8708. ocpp_clear_stopTansaction_Idtag(gun_index);//LWADD
  8709. ocpp_auto_response_StartTransationConf(gun_index);
  8710. ocpp_auto_response_StartTransationConfPH(gun_index);
  8711. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_WAIT;
  8712. ShmDcCommonData->_authWithCcidFlag[gun_index] = _CCID_NONE;
  8713. ShmDcCommonData->startTransactionID[gun_index] = 0;
  8714. ShmDcCommonData->_ctepEnergyCost [gun_index] = 0;
  8715. ShmDcCommonData->_ctepParkingFee [gun_index] = 0;
  8716. ShmDcCommonData->_ctepTotalCost [gun_index] = 0;
  8717. PresentChargedAcEnergyClear(gun_index);
  8718. for(byte _vCount = 0; _vCount < VOLTAGE_SAMPLE_COUNT; _vCount++)
  8719. {
  8720. ShmDcCommonData->_outputVoltageSapmle[gun_index][_vCount] = 0;
  8721. }
  8722. }
  8723. if (chargingInfo[gun_index]->deratingByConnOtp.deratingIndex != 0)
  8724. chargingInfo[gun_index]->deratingByConnOtp.deratingIndex = 0;
  8725. //lwmark
  8726. if (chargingInfo [gun_index]->IsAvailable == NO &&
  8727. chargingInfo[gun_index]->SystemStatus != SYS_MODE_MAINTAIN)
  8728. {
  8729. SetChargerMode(gun_index, SYS_MODE_MAINTAIN);
  8730. }
  8731. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  8732. {
  8733. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  8734. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  8735. else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_IDLE &&
  8736. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_RESERVATION &&
  8737. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_MAINTAIN &&
  8738. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_FAULT)
  8739. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8740. ClearDetectPluginFlag();
  8741. ocpp_set_errCode_cmd(gun_index);
  8742. SetChargerMode(gun_index, SYS_MODE_FAULT);
  8743. }
  8744. else
  8745. {
  8746. if (PrecheckIsPass(gun_index))
  8747. {
  8748. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_FAULT)
  8749. {
  8750. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8751. SetChargerMode(gun_index, SYS_MODE_IDLE);
  8752. }
  8753. if (chargingInfo[gun_index]->ReservationId == NO_RESERVE)
  8754. CcidAuthorizationProcessing(gun_index);
  8755. //
  8756. {
  8757. // Idle 正常程序起點
  8758. // 判斷是否有啟用檢查插槍
  8759. if(isDetectPlugin())
  8760. {
  8761. // 卡號驗證成功後,等待充電槍插入充電車
  8762. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  8763. {
  8764. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  8765. chargingInfo[gun_index]->IsAvailable)
  8766. {
  8767. PRINTF_FUNC("Conn %d Start Charging : Remote \n", gun_index);
  8768. chargingInfo[gun_index]->RemoteStartFlag = NO;
  8769. chargingInfo[gun_index]->isRemoteStart = YES;
  8770. ChangeGunSelectByIndex(gun_index);
  8771. AddPlugInTimes(gun_index);
  8772. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8773. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  8774. ClearDetectPluginFlag();
  8775. continue;
  8776. }
  8777. }
  8778. else if (chargingInfo[gun_index]->ReservedStartFlag == YES)
  8779. {
  8780. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  8781. chargingInfo[gun_index]->IsAvailable)
  8782. {
  8783. chargingInfo[gun_index]->ReservedStartFlag = NO;
  8784. ChangeGunSelectByIndex(gun_index);
  8785. AddPlugInTimes(gun_index);
  8786. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmDcCommonData->_reserved_UserId[gun_index]);
  8787. PRINTF_FUNC("Conn %d Start Charging : Reserved CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  8788. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8789. strcpy((char *)ShmDcCommonData->_reserved_UserId[gun_index], "");
  8790. // 當前操作的槍號,進入 Preparing
  8791. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8792. ClearDetectPluginFlag();
  8793. continue;
  8794. }
  8795. }
  8796. else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE)
  8797. {
  8798. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  8799. chargingInfo[gun_index]->IsAvailable &&
  8800. chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  8801. {
  8802. ChangeGunSelectByIndex(gun_index);
  8803. AddPlugInTimes(gun_index);
  8804. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  8805. PRINTF_FUNC("Conn %d Start Charging : RFID CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  8806. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8807. // 當前操作的槍號,進入 Preparing
  8808. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8809. ClearDetectPluginFlag();
  8810. continue;
  8811. }
  8812. }
  8813. if (!isCardScan &&
  8814. (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected ||
  8815. (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)))
  8816. {
  8817. // LCM => Waiting for plugging
  8818. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8819. }
  8820. }
  8821. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  8822. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  8823. {
  8824. PRINTF_FUNC("Conn %d Start Charging : Plug and charing \n", gun_index);
  8825. bool isCanStartChargingFlag = GetStartChargingByAlterMode(gun_index);
  8826. if (isCanStartChargingFlag)
  8827. {
  8828. ChangeGunSelectByIndex(gun_index);
  8829. AddPlugInTimes(gun_index);
  8830. SetChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  8831. ClearDetectPluginFlag();
  8832. ocpp_get_freeVendor(gun_index);
  8833. continue;
  8834. }
  8835. }
  8836. else
  8837. {
  8838. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8839. {
  8840. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8841. ocpp_remote_start_wait_cmd()))
  8842. {
  8843. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  8844. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  8845. {
  8846. if (ac_chargingInfo[0]->ConnectorPlugIn == AC_SYS_A)
  8847. {
  8848. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8849. }
  8850. else
  8851. {
  8852. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8853. }
  8854. }
  8855. else
  8856. {
  8857. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  8858. }
  8859. }
  8860. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  8861. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_WAIT_FOR_PLUG)
  8862. {
  8863. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8864. }
  8865. // else if (ChkAcModuleStatusIsNone() &&
  8866. // (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING && ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL))
  8867. // {
  8868. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8869. // }
  8870. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  8871. }
  8872. }
  8873. } // Idle 正常程序終點
  8874. }
  8875. else
  8876. {
  8877. SetChargerMode(gun_index, SYS_MODE_FAULT);
  8878. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  8879. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  8880. }
  8881. ReleaseSysAlarmCode(gun_index);
  8882. }
  8883. }
  8884. break;
  8885. case SYS_MODE_MODE_REASSIGN_CHECK:
  8886. {
  8887. if (isModeChange(gun_index))
  8888. {
  8889. PRINTF_FUNC("================= SYS_MODE_MODE_REASSIGN_CHECK (%x) ================ \n", gun_index);
  8890. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8891. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  8892. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  8893. StopSystemTimeoutDet();
  8894. ShmDcCommonData->_tpcPlugDetectState = _TPC_PLUG_DETECT_STATUS_NONE;
  8895. }
  8896. ocpp_clear_errorCode_cmd(gun_index);
  8897. if (ShmSmartBoxData->ConnectorStatus[gun_index].ConnectorStaus == _CONNECTOR_STATUS_WAIT)
  8898. {
  8899. PRINTF_FUNC("Conn_%d wait for Psu Module. \n", gun_index);
  8900. SetChargerMode(gun_index, SYS_MODE_REASSIGN);
  8901. }
  8902. else if (ShmSmartBoxData->ConnectorStatus[gun_index].ConnectorStaus == _CONNECTOR_STATUS_USING)
  8903. {
  8904. PRINTF_FUNC("Conn_%d start charging with Psu Module. \n", gun_index);
  8905. SetChargerMode(gun_index, SYS_MODE_PREPARING);
  8906. }
  8907. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8908. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8909. {
  8910. if (IsNoneSelectAcModule())
  8911. {
  8912. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8913. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8914. {
  8915. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8916. }
  8917. }
  8918. else
  8919. {
  8920. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8921. ocpp_remote_start_wait_cmd ()))
  8922. {
  8923. DisplayPageChkByAcModule();
  8924. }
  8925. }
  8926. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8927. }
  8928. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8929. {
  8930. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8931. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8932. }
  8933. }
  8934. break;
  8935. case SYS_MODE_REASSIGN:
  8936. {
  8937. if (isModeChange(gun_index))
  8938. {
  8939. PRINTF_FUNC("================== SYS_MODE_REASSIGN (%x) ================ \n", gun_index);
  8940. }
  8941. ocpp_clear_errorCode_cmd(gun_index);
  8942. if (ShmSmartBoxData->ConnectorStatus[gun_index].ConnectorStaus == _CONNECTOR_STATUS_USING)
  8943. SetChargerMode(gun_index, SYS_MODE_PREPARING);
  8944. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8945. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8946. {
  8947. if (IsNoneSelectAcModule())
  8948. {
  8949. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8950. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8951. {
  8952. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8953. }
  8954. }
  8955. else
  8956. {
  8957. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  8958. ocpp_remote_start_wait_cmd ()))
  8959. {
  8960. DisplayPageChkByAcModule();
  8961. }
  8962. }
  8963. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8964. }
  8965. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8966. {
  8967. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8968. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8969. }
  8970. }
  8971. break;
  8972. case SYS_MODE_PREPARING:
  8973. {
  8974. if (isModeChange(gun_index))
  8975. {
  8976. PRINTF_FUNC("=================== SYS_MODE_PREPARING (%x) =============== \n", gun_index);
  8977. StopGunInfoTimeoutDet(gun_index);
  8978. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  8979. }
  8980. ocpp_clear_errorCode_cmd(gun_index);
  8981. if (chargingInfo[gun_index]->AvailableChargingPower > 0 &&
  8982. ShmPsuData->Work_Step == _WORK_CHARGING)
  8983. {
  8984. SetChargerMode(gun_index, SYS_MODE_PREPARE_FOR_EV);
  8985. }
  8986. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  8987. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  8988. {
  8989. if (IsNoneSelectAcModule())
  8990. {
  8991. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  8992. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  8993. {
  8994. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8995. }
  8996. }
  8997. else
  8998. {
  8999. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9000. ocpp_remote_start_wait_cmd ()))
  9001. {
  9002. DisplayPageChkByAcModule();
  9003. }
  9004. }
  9005. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9006. }
  9007. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9008. {
  9009. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9010. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9011. }
  9012. }
  9013. break;
  9014. case SYS_MODE_PREPARE_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  9015. {
  9016. if (isModeChange(gun_index))
  9017. {
  9018. PRINTF_FUNC("================== SYS_MODE_PREPARE_FOR_EV (%x) ================ \n", gun_index);
  9019. _presentChargingTimeBuf[gun_index] = 0;
  9020. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  9021. StopGunInfoTimeoutDet(gun_index);
  9022. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  9023. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_WAIT;
  9024. // Start Transation
  9025. PRINTF_FUNC("StartUserId = %s \n", chargingInfo[gun_index]->StartUserId);
  9026. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9027. strcmp((char *)chargingInfo[gun_index]->StartUserId, "") != EQUAL ||
  9028. chargingInfo[gun_index]->isRemoteStart == YES)
  9029. {
  9030. _isSendStartTransaction[gun_index] = true;
  9031. ocpp_set_startTransation_cmd(gun_index);
  9032. ocpp_set_startTransationPH_cmd(gun_index);
  9033. GetTimespecFunc(&_startTransation_time[gun_index]);
  9034. }
  9035. ShmDcCommonData->_CcidAuthProcessStep = _CCID_NONE;
  9036. }
  9037. ocpp_clear_errorCode_cmd(gun_index);
  9038. CheckCcidAuthorizationFunction(gun_index);
  9039. if (!_isSendStartTransaction[gun_index] &&
  9040. chargingInfo [gun_index]->Type == _Type_CCS &&
  9041. ShmDcCommonData->enObtainAuthorizbyCCID)
  9042. {
  9043. if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_AUTHCOMP)
  9044. {
  9045. PRINTF_FUNC("_CCID_AUTHCOMP \n");
  9046. _isSendStartTransaction[gun_index] = true;
  9047. ocpp_set_startTransation_cmd(gun_index);
  9048. ocpp_set_startTransationPH_cmd(gun_index);
  9049. GetTimespecFunc(&_startTransation_time[gun_index]);
  9050. }
  9051. else if (ShmDcCommonData->_authWithCcidFlag [gun_index] == _CCID_AUTHFAIL)
  9052. {
  9053. PRINTF_FUNC("_CCID_AUTHFAIL \n");
  9054. ChargingTerminalProcess(gun_index);
  9055. }
  9056. }
  9057. ShmDcCommonData->startTransactionFlag[gun_index] = ocpp_is_resPass_StartTransactionConf(gun_index);
  9058. if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_PASS)
  9059. {
  9060. if (ocpp_chk_invalid_id_cmd(gun_index))
  9061. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_FAIL;
  9062. }
  9063. else
  9064. {
  9065. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  9066. {
  9067. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING ||
  9068. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9069. !ocpp_get_startTransation_req(gun_index) ||
  9070. strcmp((char *)chargingInfo[gun_index]->StartUserId, "") != EQUAL ||
  9071. _isTestRun)
  9072. {
  9073. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_PASS;
  9074. }
  9075. }
  9076. }
  9077. // Offline policy - No charging
  9078. ChkNoCharging(gun_index);
  9079. // 小板停止
  9080. ChkConnectorBoardStop(gun_index);
  9081. // psu 停止
  9082. ChkPsuErrorStop(gun_index);
  9083. if (ocpp_chk_remoteStop_cmd(gun_index) == YES ||
  9084. WifiScheduleStop(gun_index) ||
  9085. (_isSendStartTransaction[gun_index] && ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_FAIL) ||
  9086. (_isSendStartTransaction[gun_index] && ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_WAIT && GetTimeoutValue(&_startTransation_time[gun_index]) > 45))
  9087. {
  9088. // 後臺要求停止
  9089. ChargingTerminalProcess(gun_index);
  9090. if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_FAIL)
  9091. PRINTF_FUNC("StartTransaction req fail. (%d) \n", gun_index);
  9092. else if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_WAIT)
  9093. PRINTF_FUNC("StartTransaction req timeout. (%d) \n", gun_index);
  9094. }
  9095. // 檢查車端的槍鎖是否為鎖上
  9096. if (isEvGunLocked(gun_index) == YES)
  9097. {
  9098. SetChargerMode(gun_index, SYS_MODE_PREPARE_FOR_EVSE);
  9099. }
  9100. // LCM => Pre-charging
  9101. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9102. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9103. {
  9104. if (IsNoneSelectAcModule())
  9105. {
  9106. // if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9107. // ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9108. // {
  9109. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9110. // }
  9111. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  9112. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  9113. { }
  9114. else
  9115. {
  9116. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9117. }
  9118. }
  9119. else
  9120. {
  9121. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9122. ocpp_remote_start_wait_cmd ()))
  9123. {
  9124. DisplayPageChkByAcModule();
  9125. }
  9126. }
  9127. if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_NONE)
  9128. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9129. }
  9130. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9131. {
  9132. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  9133. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  9134. {}
  9135. else
  9136. {
  9137. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9138. }
  9139. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9140. }
  9141. }
  9142. break;
  9143. case SYS_MODE_PREPARE_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  9144. {
  9145. if (isModeChange(gun_index))
  9146. {
  9147. PRINTF_FUNC("================== SYS_MODE_PREPARE_FOR_EVSE (%x) ================ \n", gun_index);
  9148. StopGunInfoTimeoutDet(gun_index);
  9149. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  9150. }
  9151. ocpp_clear_errorCode_cmd(gun_index);
  9152. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  9153. {
  9154. // 檢查樁端的 GFD 結果
  9155. if (isPrechargeStatus_chademo(gun_index) >= 6)
  9156. {
  9157. // 當前操作的槍號,進入 Charging
  9158. SetChargerMode(gun_index, SYS_MODE_CHARGING);
  9159. }
  9160. }
  9161. else if (chargingInfo[gun_index]->Type == _Type_GB)
  9162. {
  9163. // 檢查樁端的 GFD 結果
  9164. if (isPrechargeStatus_gb(gun_index) >= 6)
  9165. {
  9166. SetChargerMode(gun_index, SYS_MODE_CHARGING);
  9167. }
  9168. }
  9169. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  9170. {
  9171. // 檢查樁端的 GFD 結果
  9172. if ((chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  9173. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING))
  9174. {
  9175. SetChargerMode(gun_index, SYS_MODE_CCS_PRECHARGE_STEP0);
  9176. }
  9177. }
  9178. RecordGfdResult(gun_index);
  9179. // Offline policy - No charging
  9180. ChkNoCharging ( gun_index );
  9181. // 小板停止
  9182. ChkConnectorBoardStop(gun_index);
  9183. // psu 停止
  9184. ChkPsuErrorStop(gun_index);
  9185. // 後台要求停止
  9186. ChkBackendStop(gun_index);
  9187. // LCM => Pre-charging
  9188. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9189. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9190. {
  9191. if (IsNoneSelectAcModule())
  9192. {
  9193. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9194. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9195. {
  9196. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9197. }
  9198. }
  9199. else
  9200. {
  9201. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9202. ocpp_remote_start_wait_cmd ()))
  9203. {
  9204. DisplayPageChkByAcModule();
  9205. }
  9206. }
  9207. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9208. }
  9209. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9210. {
  9211. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9212. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9213. }
  9214. }
  9215. break;
  9216. case SYS_MODE_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  9217. {
  9218. if (isModeChange(gun_index))
  9219. {
  9220. PRINTF_FUNC("================== SYS_MODE_CHARGING (%x) ================ \n", gun_index);
  9221. StopGunInfoTimeoutDet(gun_index);
  9222. ftime(&startChargingTime[gun_index]);
  9223. ocpp_clear_idTag_cmd(gun_index);
  9224. ChangeStartOrStopDateTime(YES, gun_index);
  9225. if (_isTestRun == YES)
  9226. {
  9227. GetTimespecFunc(&_autoRuntestTime);
  9228. }
  9229. chargingInfo[gun_index]->EvBatteryStartSoc = chargingInfo[gun_index]->EvBatterySoc;
  9230. }
  9231. ocpp_clear_errorCode_cmd(gun_index);
  9232. if (_isTestRun == YES)
  9233. {
  9234. if (GetTimeoutValue(&_autoRuntestTime) > 60)
  9235. {
  9236. ChargingTerminalProcess(gun_index);
  9237. }
  9238. }
  9239. SavePresentChargedDuration(gun_index);
  9240. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  9241. {
  9242. if (ShmDcCommonData->minOutputCur != 0 &&
  9243. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  9244. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  9245. {
  9246. // UCP
  9247. RecordAlarmCode(gun_index, "012320");
  9248. ChargingTerminalProcess(gun_index);
  9249. }
  9250. }
  9251. else if (chargingInfo[gun_index]->Type == _Type_GB)
  9252. {
  9253. if (ShmDcCommonData->minOutputCur != 0 &&
  9254. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  9255. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  9256. {
  9257. // UCP
  9258. RecordAlarmCode(gun_index, "012322");
  9259. ChargingTerminalProcess(gun_index);
  9260. }
  9261. }
  9262. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  9263. {
  9264. if (ShmDcCommonData->minOutputCur != 0 &&
  9265. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  9266. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  9267. {
  9268. // UCP
  9269. RecordAlarmCode(gun_index, "012321");
  9270. ChargingTerminalProcess(gun_index);
  9271. }
  9272. }
  9273. RecordGfdResult(gun_index);
  9274. // Offline policy - No charging
  9275. ChkNoCharging(gun_index);
  9276. // 小板停止
  9277. ChkConnectorBoardStop(gun_index);
  9278. // psu 停止
  9279. ChkPsuErrorStop(gun_index);
  9280. // 後台要求停止
  9281. ChkBackendStop(gun_index);
  9282. // 設定停止
  9283. ChkConfigurationStop(gun_index);
  9284. // LCM => Charging
  9285. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9286. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9287. {
  9288. if (IsNoneSelectAcModule() || !stopChargingChkByCard)
  9289. {
  9290. // if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9291. // ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9292. // {
  9293. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9294. // }
  9295. if (ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZING ||
  9296. ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZ_FAIL)
  9297. { }
  9298. else
  9299. {
  9300. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9301. }
  9302. }
  9303. else
  9304. {
  9305. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9306. ocpp_remote_start_wait_cmd ()))
  9307. {
  9308. DisplayPageChkByAcModule();
  9309. }
  9310. }
  9311. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  9312. }
  9313. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9314. {
  9315. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  9316. if (!stopChargingChkByCard)
  9317. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9318. }
  9319. }
  9320. break;
  9321. case SYS_MODE_ALARM:
  9322. case SYS_MODE_TERMINATING:
  9323. {
  9324. if (isModeChange(gun_index))
  9325. {
  9326. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip ||
  9327. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  9328. ocpp_set_stopReason_by_cmd(gun_index, "EmergencyStop");
  9329. else
  9330. ocpp_set_stopReason_by_cmd(gun_index, "Local");
  9331. ocpp_copy_stopReason2maintainServer(gun_index);
  9332. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  9333. {
  9334. PRINTF_FUNC("================== SYS_MODE_ALARM (%x) ================ \n", gun_index);
  9335. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  9336. {
  9337. ocpp_auto_response_StartTransationConf(gun_index);
  9338. ocpp_auto_response_StartTransationConfPH(gun_index);
  9339. if (_isSendStartTransaction[gun_index])
  9340. {
  9341. ocpp_set_stopTransation_cmd(gun_index);
  9342. ocpp_set_stopTransationPH_cmd(gun_index);
  9343. }
  9344. }
  9345. TheEndCharging(gun_index);
  9346. }
  9347. else
  9348. {
  9349. PRINTF_FUNC("================== SYS_MODE_TERMINATING (%x) ================ \n", gun_index);
  9350. }
  9351. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = NO;
  9352. StopGunInfoTimeoutDet(gun_index);
  9353. }
  9354. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  9355. {
  9356. ocpp_set_errCode_cmd(gun_index);
  9357. }
  9358. if (chargingInfo[gun_index]->Type == _Type_Chademo) { }
  9359. else if (chargingInfo[gun_index]->Type == _Type_GB) { }
  9360. else if (chargingInfo[gun_index]->Type == _Type_CCS) { }
  9361. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  9362. {
  9363. if (chargingInfo [gun_index]->ConnectorPlugIn == NO)
  9364. {
  9365. if (ShmDcCommonData->_returnIdleHoldFlag [gun_index] == NO &&
  9366. GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 10)
  9367. {
  9368. ShmDcCommonData->_returnIdleHoldFlag [gun_index] = YES;
  9369. StopGunInfoTimeoutDet ( gun_index );
  9370. StartGunInfoTimeoutDet ( gun_index, Timeout_EvseCompleteDet );
  9371. }
  9372. else if (ShmDcCommonData->_returnIdleHoldFlag [gun_index] == YES &&
  9373. GetTimeoutValue ( & chargingInfo [gun_index]->ConnectorTimeout ) >= 2)
  9374. {
  9375. SetChargerMode ( gun_index, SYS_MODE_IDLE );
  9376. }
  9377. }
  9378. if (_isTestRun == YES &&
  9379. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  9380. {
  9381. SetChargerMode(gun_index, SYS_MODE_IDLE);
  9382. }
  9383. }
  9384. else
  9385. {
  9386. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  9387. {
  9388. if (isEvGunLocked(gun_index) == NO ||
  9389. isPrechargeStatus_chademo(gun_index) <= 0)
  9390. {
  9391. SetChargerMode(gun_index, SYS_MODE_COMPLETE);
  9392. }
  9393. }
  9394. else if (chargingInfo[gun_index]->Type == _Type_GB)
  9395. {
  9396. if (isEvGunLocked(gun_index) == NO ||
  9397. isPrechargeStatus_gb(gun_index) <= 0)
  9398. {
  9399. SetChargerMode(gun_index, SYS_MODE_COMPLETE);
  9400. }
  9401. }
  9402. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  9403. {
  9404. if (isEvGunLocked(gun_index) == NO &&
  9405. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  9406. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14))
  9407. {
  9408. SetChargerMode(gun_index, SYS_MODE_COMPLETE);
  9409. }
  9410. }
  9411. }
  9412. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9413. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9414. {
  9415. if (IsNoneSelectAcModule())
  9416. {
  9417. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9418. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9419. {
  9420. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9421. }
  9422. }
  9423. else
  9424. {
  9425. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9426. ocpp_remote_start_wait_cmd ()))
  9427. {
  9428. DisplayPageChkByAcModule();
  9429. }
  9430. }
  9431. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9432. }
  9433. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9434. {
  9435. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9436. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9437. }
  9438. }
  9439. break;
  9440. case SYS_MODE_COMPLETE:
  9441. {
  9442. if (isModeChange(gun_index))
  9443. {
  9444. PRINTF_FUNC("================== SYS_MODE_COMPLETE (%x) ================ \n", gun_index);
  9445. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  9446. {
  9447. ocpp_set_errCode_cmd(gun_index);
  9448. ocpp_auto_response_StartTransationConf(gun_index);
  9449. ocpp_auto_response_StartTransationConfPH(gun_index);
  9450. if (_isSendStartTransaction[gun_index])
  9451. {
  9452. ocpp_set_stopTransation_cmd(gun_index);
  9453. ocpp_set_stopTransationPH_cmd(gun_index);
  9454. }
  9455. }
  9456. TheEndCharging(gun_index);
  9457. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = NO;
  9458. }
  9459. ocpp_clear_errorCode_cmd(gun_index);
  9460. if (chargingInfo[gun_index]->ConnectorPlugIn == NO)
  9461. {
  9462. if (ShmDcCommonData->_returnIdleHoldFlag[gun_index] == NO &&
  9463. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  9464. {
  9465. ShmDcCommonData->_returnIdleHoldFlag[gun_index] = YES;
  9466. StopGunInfoTimeoutDet(gun_index);
  9467. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  9468. }
  9469. else if (ShmDcCommonData->_returnIdleHoldFlag[gun_index] == YES &&
  9470. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 2)
  9471. {
  9472. SetChargerMode(gun_index, SYS_MODE_IDLE);
  9473. }
  9474. }
  9475. if (_isTestRun == YES &&
  9476. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  9477. {
  9478. SetChargerMode(gun_index, SYS_MODE_IDLE);
  9479. }
  9480. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9481. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9482. {
  9483. if (IsNoneSelectAcModule())
  9484. {
  9485. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9486. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9487. {
  9488. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9489. }
  9490. }
  9491. else
  9492. {
  9493. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9494. ocpp_remote_start_wait_cmd ()))
  9495. {
  9496. DisplayPageChkByAcModule();
  9497. }
  9498. }
  9499. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9500. }
  9501. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9502. {
  9503. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  9504. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9505. }
  9506. }
  9507. break;
  9508. case SYS_MODE_CCS_PRECHARGE_STEP0:
  9509. {
  9510. if (isModeChange(gun_index))
  9511. {
  9512. PRINTF_FUNC("================== SYS_MODE_CCS_PRECHARGE_STEP0 (%x) ================ \n", gun_index);
  9513. StopGunInfoTimeoutDet(gun_index);
  9514. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  9515. }
  9516. ocpp_clear_errorCode_cmd(gun_index);
  9517. RecordGfdResult(gun_index);
  9518. // Offline policy - No charging
  9519. ChkNoCharging(gun_index);
  9520. // 小板停止
  9521. ChkConnectorBoardStop(gun_index);
  9522. // psu 停止
  9523. ChkPsuErrorStop(gun_index);
  9524. // 後台要求停止
  9525. ChkBackendStop(gun_index);
  9526. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  9527. // 切換 D+ Relay to Precharge Relay
  9528. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  9529. {
  9530. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  9531. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  9532. {
  9533. if (isPrechargeStatus_ccs(gun_index) == 39)
  9534. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PreChargeResponse (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  9535. else if (isPrechargeStatus_ccs(gun_index) == 40)
  9536. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PowerDeliveryRequest start (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  9537. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  9538. }
  9539. }
  9540. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  9541. {
  9542. SetChargerMode(gun_index, SYS_MODE_CCS_PRECHARGE_STEP1);
  9543. }
  9544. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9545. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9546. {
  9547. if (IsNoneSelectAcModule())
  9548. {
  9549. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9550. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9551. {
  9552. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9553. }
  9554. }
  9555. else
  9556. {
  9557. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9558. ocpp_remote_start_wait_cmd ()))
  9559. {
  9560. DisplayPageChkByAcModule();
  9561. }
  9562. }
  9563. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9564. }
  9565. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9566. {
  9567. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9568. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9569. }
  9570. break;
  9571. }
  9572. case SYS_MODE_CCS_PRECHARGE_STEP1:
  9573. {
  9574. if (isModeChange(gun_index))
  9575. {
  9576. PRINTF_FUNC("================== SYS_MODE_CCS_PRECHARGE_STEP1 (%x) ================ \n", gun_index);
  9577. }
  9578. ocpp_clear_errorCode_cmd(gun_index);
  9579. RecordGfdResult(gun_index);
  9580. // Offline policy - No charging
  9581. ChkNoCharging(gun_index);
  9582. // 小板停止
  9583. ChkConnectorBoardStop(gun_index);
  9584. // psu 停止
  9585. ChkPsuErrorStop(gun_index);
  9586. // 後台要求停止
  9587. ChkBackendStop(gun_index);
  9588. // 等待小板通知進入充電
  9589. // 切換 D+ Relay to Precharge Relay
  9590. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  9591. {
  9592. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  9593. SetChargerMode(gun_index, SYS_MODE_CHARGING);
  9594. }
  9595. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index &&
  9596. ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  9597. {
  9598. if (IsNoneSelectAcModule())
  9599. {
  9600. if (ShmSysConfigAndInfo->SysInfo.SystemPage <= _LCM_AUTHORIZING ||
  9601. ShmSysConfigAndInfo->SysInfo.SystemPage >= _LCM_AUTHORIZ_FAIL)
  9602. {
  9603. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9604. }
  9605. }
  9606. else
  9607. {
  9608. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  9609. ocpp_remote_start_wait_cmd ()))
  9610. {
  9611. DisplayPageChkByAcModule();
  9612. }
  9613. }
  9614. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9615. }
  9616. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  9617. {
  9618. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  9619. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  9620. }
  9621. break;
  9622. }
  9623. }
  9624. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE ||
  9625. chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION ||
  9626. chargingInfo[gun_index]->SystemStatus == SYS_MODE_MAINTAIN ||
  9627. chargingInfo[gun_index]->SystemStatus == SYS_MODE_FAULT)
  9628. {
  9629. ocpp_chk_notificationMessage_cmd(gun_index);
  9630. }
  9631. }
  9632. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  9633. {
  9634. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  9635. }
  9636. else
  9637. {
  9638. bool dcPageRun = false;
  9639. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  9640. {
  9641. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE)
  9642. ChangeLcmByIndex(_LCM_IDLE);
  9643. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_PREPARING)
  9644. ChangeLcmByIndex(_LCM_PRE_CHARGE);
  9645. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  9646. ChangeLcmByIndex(_LCM_CHARGING);
  9647. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_TERMINATING ||
  9648. ac_chargingInfo[0]->SystemStatus == SYS_MODE_COMPLETE)
  9649. ChangeLcmByIndex(_LCM_COMPLETE);
  9650. else
  9651. dcPageRun = true;
  9652. }
  9653. else
  9654. dcPageRun = true;
  9655. if (dcPageRun)
  9656. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  9657. }
  9658. bool isCharging = false;
  9659. SystemAlarmCheck();
  9660. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  9661. {
  9662. AlarmCheck(chargingInfo[gun_index]->Type);
  9663. if (_isTestRun == YES)
  9664. {
  9665. if (IsGunUsing(gun_index))
  9666. {
  9667. isCharging = true;
  9668. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_COMPLETE ||
  9669. chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  9670. {
  9671. GetTimespecFunc(&_autoRuntestTime);
  9672. }
  9673. }
  9674. }
  9675. }
  9676. if (_isTestRun == YES)
  9677. {
  9678. if (!isCharging)
  9679. {
  9680. if (GetTimeoutValue(&_autoRuntestTime) >= 60)
  9681. {
  9682. DetectPluginStart();
  9683. GetTimespecFunc(&_autoRuntestTime);
  9684. }
  9685. }
  9686. else
  9687. {
  9688. ClearDetectPluginFlag();
  9689. }
  9690. }
  9691. write(wtdFd, "a", 1);
  9692. usleep(whileLoopTime);
  9693. }
  9694. return FAIL;
  9695. }