main.c 299 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304
  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 <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include <time.h>
  25. #include <ctype.h>
  26. #include <ifaddrs.h>
  27. #include <math.h>
  28. #include <stdbool.h>
  29. #include <dirent.h>
  30. #include "../../define.h"
  31. #include "CheckSystemTask.h"
  32. #include "Config.h"
  33. #include "timeout.h"
  34. #define DEBUG_ALSTON 0
  35. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  36. #define PASS 1
  37. #define FAIL -1
  38. #define MODELNAME_FAIL 0
  39. #define BUFFER_SIZE 128
  40. #define YES 1
  41. #define NO 0
  42. #define NORMAL 0
  43. #define ABNORMAL 1
  44. #define EQUAL 0
  45. #define BTN_RELEASE 0
  46. #define BTN_PRESS 1
  47. #define MAX_BUF 64
  48. //#define MtdBlockSize 0x600000
  49. #define MtdBlockSize 0x300000
  50. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  51. #define UPGRADE_FAN 0x02
  52. #define UPGRADE_RB 0x03
  53. #define UPGRADE_PRI 0x04
  54. #define UPGRADE_AC 0x05
  55. #define UPGRADE_LED 0x06
  56. #define SYSTEM_MIN_VOL 80
  57. #define MIN_OUTPUT_CUR 0
  58. #define AC_OUTPUT_VOL 220
  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 10
  78. #define GUN_PRECHARGING_TIMEOUT 60
  79. #define TIMEOUT_SPEC_HANDSHAKING 180000
  80. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  81. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  82. int whileLoopTime = 10000; // 10 ms
  83. int wtdFd = -1;
  84. byte _authorizeIndex = NO_DEFINE;
  85. bool IsAuthorizingMode();
  86. void ClearAuthorizedFlag();
  87. bool isDetectPlugin();
  88. void ClearDetectPluginFlag();
  89. int mystrcmp(unsigned char *p1, unsigned char *p2);
  90. void CheckTask();
  91. long long DiffTimebWithNow(struct timeb ST);
  92. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  93. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  94. void ChargingTerminalProcess(byte gunIndex);
  95. void ChkPrimaryStatus();
  96. void StartSystemTimeoutDet(unsigned char flag);
  97. void StopSystemTimeoutDet();
  98. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  99. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  100. int StoreLogMsg_1(const char *fmt, ...);
  101. int GetTimeoutValue(struct timespec *startTime);
  102. void gpio_set_value(unsigned int gpio, unsigned int value);
  103. void PRINTF_FUNC(char *string, ...);
  104. void ChangeGunSelectByIndex(byte sel);
  105. void ChargingAlarmProcess(byte gunIndex);
  106. void CheckSystemErrorFunction(byte index);
  107. int opcc_chk_reserve_expired(byte gunIndex);
  108. void ocpp_process_start();
  109. void ocpp_auto_response_BootNotification();
  110. void ocpp_auto_response_StartTransationConf(byte gunIndex);
  111. bool ocpp_chk_authrization_cmd();
  112. void ocpp_chk_reset_cmd(byte isforce);
  113. void ocpp_chk_reserved_cmd(byte gunIndex);
  114. void ocpp_chk_availability_cmd(byte gunIndex);
  115. void ocpp_chk_unlock_cmd(byte gunIndex);
  116. bool ocpp_chk_remoteStop_cmd(byte gunIndex);
  117. void ocpp_chk_remoteStart_cmd();
  118. void ocpp_chk_update_cmd();
  119. bool ocpp_chk_invalid_id_cmd(byte gunIndex);
  120. void ocpp_set_startTransation_cmd(byte gunIndex);
  121. void ocpp_set_stopTransation_cmd(byte gunIndex);
  122. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString);
  123. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason);
  124. void ocpp_set_authorizeReq_cmd(byte value);
  125. void ocpp_set_authorizeConf_cmd(byte value);
  126. void ocpp_set_errCode_cmd(byte gunIndex);
  127. void ocpp_clear_errorCode_cmd(byte gunIndex);
  128. void ocpp_clear_idTag_cmd(byte gun_index);
  129. void ocpp_clear_stopReason(byte gun_index);
  130. bool ocpp_get_authorize_conf();
  131. unsigned short _ocpp_get_connect_timeout();
  132. void ocpp_chargingProfile_process(byte gun_index);
  133. void RecordAlarmCode(byte gunIndex, char *code);
  134. void ReleaseSysAlarmCode(byte gunIndex);
  135. void ReviewCriticalAlarm();
  136. int DB_Open(sqlite3 *db);
  137. int DB_Insert_Record(sqlite3 *db, int gun_index);
  138. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  139. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  140. void InitialDHCP();
  141. int GetStartScheduleTime(unsigned char *time);
  142. void KillAllTask();
  143. void CheckSystemTaskAlive();
  144. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  145. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  146. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  147. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  148. struct StatusCodeData *ShmStatusCodeData;
  149. struct PsuData *ShmPsuData;
  150. struct CHAdeMOData *ShmCHAdeMOData;
  151. struct GBTData *ShmGBTData;
  152. struct CcsData *ShmCcsData;
  153. struct PrimaryMcuData *ShmPrimaryMcuData;
  154. struct FanModuleData *ShmFanModuleData;
  155. struct RelayModuleData *ShmRelayModuleData;
  156. struct LedModuleData *ShmLedModuleData;
  157. struct OCPP16Data *ShmOCPP16Data;
  158. struct OCPP20Data *ShmOCPP20Data;
  159. struct MeterInformation *ShmCsuMeterData;
  160. struct DcCommonInformation *ShmDcCommonData;
  161. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  162. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  163. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  164. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  165. int _presentChargingTimeBuf[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  166. struct timespec _startTransation_time[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  167. float gunOutputVol [CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  168. float _ChargingProfilePwBuffer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  169. float _AcChargingProfilePwBuffer;
  170. GunErr errCollect;
  171. struct timespec _autoRuntestTime;
  172. byte _isTestRun = NO;
  173. // for initial index to check EV board type is correct
  174. byte _gunIndex = 0;
  175. byte _acgunIndex = 0;
  176. byte _chademoIndex = 0;
  177. byte _ccsIndex = 0;
  178. byte _gb_Index = 0;
  179. byte _ac_Index = 0;
  180. byte bd0_1_status = 0;
  181. byte bd0_2_status = 0;
  182. byte bd1_1_status = 0;
  183. byte bd1_2_status = 0;
  184. bool isCardScan = false;
  185. bool isModelNameMatch = true;
  186. int rfidFd = -1;
  187. char* rfidPortName = "/dev/ttyS2";
  188. char* fwVersion = "V2.02.00.0000.00";
  189. sqlite3 *localDb;
  190. bool isDb_ready;
  191. sqlite3 *networkDb;
  192. char MyDefaultPriceString[128];
  193. byte reset4gStep = RESET_4G_STEP_NONE;
  194. //================================================
  195. // initial can-bus
  196. //================================================
  197. int InitCanBus()
  198. {
  199. int s0,nbytes;
  200. struct timeval tv;
  201. struct ifreq ifr0;
  202. struct sockaddr_can addr0;
  203. system("/sbin/ip link set can0 down");
  204. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  205. system("/sbin/ip link set can0 up");
  206. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  207. tv.tv_sec = 0;
  208. tv.tv_usec = 10000;
  209. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  210. {
  211. #ifdef SystemLogMessage
  212. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  213. #endif
  214. }
  215. nbytes=40960;
  216. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  217. {
  218. #ifdef SystemLogMessage
  219. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  220. #endif
  221. }
  222. nbytes=40960;
  223. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  224. {
  225. #ifdef SystemLogMessage
  226. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  227. #endif
  228. }
  229. strcpy(ifr0.ifr_name, "can0" );
  230. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  231. addr0.can_family = AF_CAN;
  232. addr0.can_ifindex = ifr0.ifr_ifindex;
  233. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  234. return s0;
  235. }
  236. //================================================
  237. // initial uart port
  238. //================================================
  239. char *_priPortName = "/dev/ttyS1";
  240. char *_485PortName = "/dev/ttyS5";
  241. int InitComPort(byte target)
  242. {
  243. int fd;
  244. struct termios tios;
  245. if(target == UPGRADE_PRI)
  246. fd = open(_priPortName, O_RDWR);
  247. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  248. fd = open(_485PortName, O_RDWR);
  249. if(fd<=0)
  250. {
  251. #ifdef SystemLogMessage
  252. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  253. #endif
  254. return -1;
  255. }
  256. ioctl (fd, TCGETS, &tios);
  257. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  258. tios.c_lflag = 0;
  259. tios.c_iflag = 0;
  260. tios.c_oflag = 0;
  261. tios.c_cc[VMIN]=0;
  262. tios.c_cc[VTIME]=(unsigned char)5;
  263. tios.c_lflag=0;
  264. tcflush(fd, TCIFLUSH);
  265. ioctl (fd, TCSETS, &tios);
  266. return fd;
  267. }
  268. //=================================
  269. // Common routine
  270. //=================================
  271. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  272. {
  273. strncpy(dest, src + start, cnt);
  274. dest[cnt] = 0;
  275. }
  276. int InitWatchDog()
  277. {
  278. int fd;
  279. int timeout = 180;
  280. system("/usr/bin/fuser -k /dev/watchdog");
  281. sleep(1);
  282. system("echo V > /dev/watchdog");
  283. sleep(1);
  284. fd = open("/dev/watchdog", O_RDWR);
  285. if(fd <= 0)
  286. {
  287. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  288. }
  289. ioctl(fd, _IOWR('W', 6, int), &timeout);
  290. return fd;
  291. }
  292. void CloseWatchDog()
  293. {
  294. if (wtdFd > 0)
  295. {
  296. close(wtdFd);
  297. sleep(1);
  298. system("/usr/bin/fuser -k /dev/watchdog");
  299. sleep(1);
  300. system("echo V > /dev/watchdog");
  301. sleep(1);
  302. }
  303. }
  304. int StoreLogMsg_1(const char *fmt, ...)
  305. {
  306. char Buf[4096+256];
  307. char buffer[4096];
  308. va_list args;
  309. struct timeb SeqEndTime;
  310. struct tm *tm;
  311. va_start(args, fmt);
  312. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  313. va_end(args);
  314. memset(Buf,0,sizeof(Buf));
  315. ftime(&SeqEndTime);
  316. SeqEndTime.time = time(NULL);
  317. tm=localtime(&SeqEndTime.time);
  318. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES ||
  319. DEBUG_ALSTON)
  320. {
  321. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  322. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  323. printf("%s \n", Buf);
  324. }
  325. else
  326. {
  327. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog_%s",
  328. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  329. buffer,
  330. tm->tm_year+1900,tm->tm_mon+1,
  331. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  332. system(Buf);
  333. }
  334. return rc;
  335. }
  336. //unsigned long GetTimeoutValue(struct timeval _sour_time)
  337. //{
  338. // struct timeval _end_time;
  339. // gettimeofday(&_end_time, NULL);
  340. //
  341. // return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  342. //}
  343. int GetTimeoutValue(struct timespec *startTime)
  344. {
  345. struct timespec endTime;
  346. clock_gettime(CLOCK_MONOTONIC_COARSE, &endTime);
  347. return endTime.tv_sec - startTime->tv_sec;
  348. }
  349. void GetTimespecFunc(struct timespec *time)
  350. {
  351. clock_gettime(CLOCK_MONOTONIC_COARSE, time);
  352. }
  353. int mystrcmp(unsigned char *p1, unsigned char *p2)
  354. {
  355. while(*p1==*p2)
  356. {
  357. if(*p1=='\0' || *p2=='\0')
  358. break;
  359. p1++;
  360. p2++;
  361. }
  362. if(*p1=='\0' && *p2=='\0')
  363. return(PASS);
  364. else
  365. return(FAIL);
  366. }
  367. int DiffTimeb(struct timeb ST, struct timeb ET)
  368. {
  369. //return milli-second
  370. unsigned int StartTime,StopTime;
  371. StartTime=(unsigned int)ST.time;
  372. StopTime=(unsigned int)ET.time;
  373. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  374. return (StopTime-StartTime);
  375. }
  376. int DiffTimebWithNowSec(struct timeb ST)
  377. {
  378. //return milli-second
  379. struct timeb ET;
  380. unsigned int StartTime,StopTime;
  381. ftime(&ET);
  382. StartTime=(unsigned int)ST.time;
  383. StopTime=(unsigned int)ET.time;
  384. return (StopTime-StartTime);
  385. }
  386. int CheckTimeOut(uint8_t *start)
  387. {
  388. int result = YES;
  389. struct ParsingResult
  390. {
  391. int result;
  392. int scanedElement;
  393. int year;
  394. int month;
  395. int mday;
  396. int hour;
  397. int min;
  398. int sec;
  399. int tz_hour;
  400. int tz_min;
  401. float minSec;
  402. }parsingResult;
  403. struct tm tmStart;
  404. struct timeb tbStart;
  405. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  406. if(strstr((char*)start, ".") != NULL)
  407. {
  408. // Original data with mini second
  409. if(strstr((char*)start, "Z") != NULL)
  410. {
  411. // Original data with Z
  412. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  413. &parsingResult.year,
  414. &parsingResult.month,
  415. &parsingResult.mday,
  416. &parsingResult.hour,
  417. &parsingResult.min,
  418. &parsingResult.sec,
  419. &parsingResult.minSec);
  420. }
  421. else
  422. {
  423. // Original data without Z
  424. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  425. &parsingResult.year,
  426. &parsingResult.month,
  427. &parsingResult.mday,
  428. &parsingResult.hour,
  429. &parsingResult.min,
  430. &parsingResult.sec,
  431. &parsingResult.minSec,
  432. &parsingResult.tz_hour,
  433. &parsingResult.tz_min);
  434. }
  435. }
  436. else
  437. {
  438. // Original data without mini second
  439. if(strstr((char*)start, "Z") != NULL)
  440. {
  441. // Original data with Z
  442. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  443. &parsingResult.year,
  444. &parsingResult.month,
  445. &parsingResult.mday,
  446. &parsingResult.hour,
  447. &parsingResult.min,
  448. &parsingResult.sec);
  449. }
  450. else
  451. {
  452. // Original data without Z
  453. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  454. &parsingResult.year,
  455. &parsingResult.month,
  456. &parsingResult.mday,
  457. &parsingResult.hour,
  458. &parsingResult.min,
  459. &parsingResult.sec,
  460. &parsingResult.tz_hour,
  461. &parsingResult.tz_min);
  462. }
  463. }
  464. if(parsingResult.scanedElement >= 6)
  465. {
  466. tmStart.tm_year = parsingResult.year - 1900;
  467. tmStart.tm_mon = parsingResult.month - 1;
  468. tmStart.tm_mday = parsingResult.mday;
  469. tmStart.tm_hour = parsingResult.hour;
  470. tmStart.tm_min = parsingResult.min;
  471. tmStart.tm_sec = parsingResult.sec;
  472. tmStart.tm_gmtoff = 0;
  473. tbStart.time = mktime(&tmStart);
  474. tbStart.timezone = 0;
  475. tbStart.millitm = 0;
  476. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  477. if(DiffTimebWithNowSec(tbStart) <= 0)
  478. result = NO;
  479. else
  480. result = YES;
  481. }
  482. else
  483. {
  484. PRINTF_FUNC("Start date parsing error.\n");
  485. }
  486. return result;
  487. }
  488. void setChargerMode(byte gun_index, byte mode)
  489. {
  490. chargingInfo[gun_index]->SystemStatus = mode;
  491. }
  492. void PRINTF_FUNC(char *string, ...)
  493. {
  494. va_list args;
  495. char buffer[4096];
  496. va_start(args, string);
  497. vsnprintf(buffer, sizeof(buffer), string, args);
  498. va_end(args);
  499. DEBUG_INFO_MSG("%s ", buffer);
  500. }
  501. long long DiffTimebWithNow(struct timeb ST)
  502. {
  503. //return milli-second
  504. struct timeb ET;
  505. long long StartTime, StopTime;
  506. ftime(&ET);
  507. StartTime = (long long)ST.time;
  508. StopTime = (long long)ET.time;
  509. return ((StopTime - StartTime)*1000) + (ET.millitm - ST.millitm);
  510. }
  511. bool CheckBackendChargingTimeout(byte gunIndex)
  512. {
  513. bool result = false;
  514. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  515. {
  516. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  517. {
  518. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  519. result = true;
  520. }
  521. }
  522. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  523. {
  524. // 隨插即充電的要看 offline
  525. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  526. {
  527. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  528. result = true;
  529. }
  530. }
  531. return result;
  532. }
  533. bool CheckBackendChargingEnergy(byte gunIndex)
  534. {
  535. bool result = false;
  536. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  537. {
  538. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  539. {
  540. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  541. result = true;
  542. }
  543. }
  544. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  545. {
  546. // 隨插即充電的要看 offline
  547. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  548. {
  549. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  550. result = true;
  551. }
  552. }
  553. return result;
  554. }
  555. bool IsGunUsing(byte dcGunIndex)
  556. {
  557. bool result = false;
  558. if((chargingInfo[dcGunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  559. chargingInfo[dcGunIndex]->SystemStatus <= SYS_MODE_ALARM) ||
  560. (chargingInfo[dcGunIndex]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  561. chargingInfo[dcGunIndex]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  562. {
  563. result = true;
  564. }
  565. return result;
  566. }
  567. //==========================================
  568. // Log
  569. //==========================================
  570. void CheckFwSlotStatusLog()
  571. {
  572. if (bd0_1_status == 0 && bd0_2_status == 1)
  573. PRINTF_FUNC("Connector 1 : Chademo");
  574. else if (bd0_1_status == 1 && bd0_2_status == 0)
  575. PRINTF_FUNC("Connector 1 : CCS");
  576. else if (bd0_1_status == 1 && bd0_2_status == 1)
  577. PRINTF_FUNC("Connector 1 : GB");
  578. if (bd1_1_status == 0 && bd1_2_status == 1)
  579. PRINTF_FUNC("Connector 2 : Chademo");
  580. else if (bd1_1_status == 1 && bd1_2_status == 0)
  581. PRINTF_FUNC("Connector 2 : CCS");
  582. else if (bd1_1_status == 1 && bd1_2_status == 1)
  583. PRINTF_FUNC("Connector 2 : GB");
  584. }
  585. void CheckHwSlotStatusLog(byte index)
  586. {
  587. if (chargingInfo[index]->Type == _Type_Chademo)
  588. {
  589. PRINTF_FUNC("Hw check : Connector %d, Type : Chademo, Evboard_id = %d \n",
  590. index, chargingInfo[index]->Evboard_id);
  591. }
  592. else if (chargingInfo[index]->Type == _Type_CCS)
  593. {
  594. PRINTF_FUNC("Hw check : Connector %d, Type : CCS, Evboard_id = %d \n",
  595. index, chargingInfo[index]->Evboard_id);
  596. }
  597. else if (chargingInfo[index]->Type == _Type_GB)
  598. {
  599. PRINTF_FUNC("Hw check : Connector %d, Type : GB, Evboard_id = %d \n",
  600. index, chargingInfo[index]->Evboard_id);
  601. }
  602. }
  603. //==========================================
  604. // Check interface status
  605. //==========================================
  606. int isInterfaceUp(const char *interface)
  607. {
  608. int result = FAIL;
  609. FILE *fp;
  610. char cmd[256];
  611. char buf[512];
  612. strcpy(cmd, "ifconfig");
  613. fp = popen(cmd, "r");
  614. if(fp != NULL)
  615. {
  616. while(fgets(buf, sizeof(buf), fp) != NULL)
  617. {
  618. if(strstr(buf, interface) > 0)
  619. {
  620. result = PASS;
  621. }
  622. }
  623. }
  624. pclose(fp);
  625. return result;
  626. }
  627. //=================================
  628. // Create all share memory
  629. //=================================
  630. int CreateShareMemory()
  631. {
  632. int MeterSMId;
  633. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  634. {
  635. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  636. return 0;
  637. }
  638. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  639. {
  640. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  641. return 0;
  642. }
  643. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  644. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  645. {
  646. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  647. return 0;
  648. }
  649. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  650. {
  651. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  652. return 0;
  653. }
  654. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  655. //creat ShmPsuData
  656. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  657. {
  658. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  659. return 0;
  660. }
  661. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  662. {
  663. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  664. return 0;
  665. }
  666. memset(ShmPsuData, 0, sizeof(struct PsuData));
  667. if(CHAdeMO_QUANTITY > 0)
  668. {
  669. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  670. {
  671. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  672. return 0;
  673. }
  674. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  675. {
  676. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  677. return 0;
  678. }
  679. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  680. }
  681. if(GB_QUANTITY > 0)
  682. {
  683. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  684. {
  685. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  686. return 0;
  687. }
  688. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  689. {
  690. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  691. return 0;
  692. }
  693. memset(ShmGBTData, 0, sizeof(struct GBTData));
  694. }
  695. //creat ShmCcsData
  696. if(CCS_QUANTITY > 0)
  697. {
  698. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  699. {
  700. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  701. return 0;
  702. }
  703. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  704. {
  705. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  706. return 0;
  707. }
  708. memset(ShmCcsData, 0, sizeof(struct CcsData));
  709. }
  710. //creat ShmPrimaryMcuData
  711. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  712. {
  713. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  714. return 0;
  715. }
  716. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  717. {
  718. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  719. return 0;
  720. }
  721. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  722. //creat ShmFanModuleData
  723. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  724. {
  725. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  726. return 0;
  727. }
  728. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  729. {
  730. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  731. return 0;
  732. }
  733. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  734. //creat ShmRelayModuleData
  735. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  736. {
  737. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  738. return 0;
  739. }
  740. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  741. {
  742. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  743. return 0;
  744. }
  745. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  746. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  747. {
  748. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmLedModuleData NG \n");
  749. return 0;
  750. }
  751. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  752. {
  753. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmLedModuleData NG \n");
  754. return 0;
  755. }
  756. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  757. //creat ShmOCPP16Data
  758. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  759. {
  760. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  761. return 0;
  762. }
  763. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  764. {
  765. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  766. return 0;
  767. }
  768. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  769. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  770. {
  771. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget OCPP20Data NG\n");
  772. return 0;
  773. }
  774. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  775. {
  776. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat OCPP20Data NG\n");
  777. return 0;
  778. }
  779. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  780. if ((MeterSMId = shmget(ShmCsuMeterKey, sizeof(struct MeterInformation), IPC_CREAT | 0777)) < 0)
  781. {
  782. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCsuMeterKey NG \n");
  783. return 0;
  784. }
  785. else if ((ShmCsuMeterData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  786. {
  787. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCsuMeterData NG \n");
  788. return 0;
  789. }
  790. memset(ShmCsuMeterData, 0, sizeof(struct MeterInformation));
  791. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  792. {
  793. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCommonKey NG \n");
  794. return 0;
  795. }
  796. else if ((ShmDcCommonData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  797. {
  798. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCommonKey NG \n");
  799. return 0;
  800. }
  801. byte buf = 0;
  802. if (ShmDcCommonData->rebootCount == 1)
  803. buf = ShmDcCommonData->rebootCount;
  804. memset(ShmDcCommonData, 0, sizeof(struct DcCommonInformation));
  805. ShmDcCommonData->rebootCount = buf;
  806. return 1;
  807. }
  808. void SetupRebootCount(byte value)
  809. {
  810. int MeterSMId;
  811. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  812. {
  813. DEBUG_ERROR_MSG("[main]RecordRebootCount:shmget ShmCommonKey NG \n");
  814. }
  815. else
  816. {
  817. ShmDcCommonData->rebootCount = value;
  818. shmctl(MeterSMId, IPC_SET, 0);
  819. }
  820. }
  821. //=================================
  822. // LCM Page
  823. //=================================
  824. void ChangeLcmByIndex(byte page_index)
  825. {
  826. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL ||
  827. page_index == _LCM_COMPLETE || page_index == _LCM_FIX || page_index == _LCM_EMC)
  828. {
  829. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0 &&
  830. (page_index == _LCM_FIX || page_index == _LCM_EMC))
  831. {
  832. bool isUpdate = false;
  833. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  834. {
  835. if (chargingInfo[_index]->SystemStatus != SYS_MODE_RESERVATION &&
  836. chargingInfo[_index]->SystemStatus != SYS_MODE_MAINTAIN)
  837. {
  838. isUpdate = true;
  839. }
  840. }
  841. if (isUpdate &&
  842. ShmDcCommonData->LcmFwVersion > 0 &&
  843. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  844. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_EMC;
  845. else
  846. ShmSysConfigAndInfo->SysInfo.PageIndex = _LCM_FIX;
  847. }
  848. else
  849. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  850. }
  851. }
  852. //======================================================
  853. // Peripheral initial
  854. //======================================================
  855. void InitGPIO()
  856. {
  857. /*****************0~3, 4 bank, bank x 32+ num*********************/
  858. /***************************************************************/
  859. /*************** GPIO 0 ***************************************/
  860. /***************************************************************/
  861. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  862. system("echo 22 > /sys/class/gpio/export");
  863. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  864. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  865. system("echo 23 > /sys/class/gpio/export");
  866. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  867. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  868. system("echo 26 > /sys/class/gpio/export");
  869. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  870. system("echo 1 > /sys/class/gpio/gpio26/value");
  871. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  872. system("echo 27 > /sys/class/gpio/export");
  873. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  874. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  875. system("echo 29 > /sys/class/gpio/export");
  876. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  877. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  878. system("echo 19 > /sys/class/gpio/export");
  879. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  880. system("echo 1 > /sys/class/gpio/gpio19/value");
  881. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  882. system("echo 20 > /sys/class/gpio/export");
  883. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  884. /***************************************************************/
  885. /*************** GPIO 1 ***************************************/
  886. /***************************************************************/
  887. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  888. system("echo 44 > /sys/class/gpio/export");
  889. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  890. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  891. system("echo 45 > /sys/class/gpio/export");
  892. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  893. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  894. system("echo 46 > /sys/class/gpio/export");
  895. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  896. system("echo 0 > /sys/class/gpio/gpio46/value");
  897. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  898. system("echo 47 > /sys/class/gpio/export");
  899. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  900. /***************************************************************/
  901. /*************** GPIO 2 ***************************************/
  902. /***************************************************************/
  903. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  904. system("echo 89 > /sys/class/gpio/export");
  905. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  906. system("echo 1 > /sys/class/gpio/gpio89/value");
  907. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  908. system("echo 87 > /sys/class/gpio/export");
  909. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  910. system("echo 0 > /sys/class/gpio/gpio87/value");
  911. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  912. system("echo 88 > /sys/class/gpio/export");
  913. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  914. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  915. system("echo 86 > /sys/class/gpio/export");
  916. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  917. /***************************************************************/
  918. /*************** GPIO 3 ***************************************/
  919. /***************************************************************/
  920. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  921. system("echo 111 > /sys/class/gpio/export");
  922. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  923. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  924. system("echo 114 > /sys/class/gpio/export");
  925. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  926. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  927. system("echo 113 > /sys/class/gpio/export");
  928. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  929. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  930. system("echo 110 > /sys/class/gpio/export");
  931. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  932. system("echo 0 > /sys/class/gpio/gpio110/value");
  933. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  934. system("echo 115 > /sys/class/gpio/export");
  935. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  936. system("echo 0 > /sys/class/gpio/gpio115/value");
  937. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  938. system("echo 112 > /sys/class/gpio/export");
  939. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  940. system("echo 1 > /sys/class/gpio/gpio112/value");
  941. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  942. system("echo 116 > /sys/class/gpio/export");
  943. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  944. system("echo 0 > /sys/class/gpio/gpio116/value");
  945. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  946. system("echo 103 > /sys/class/gpio/export");
  947. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  948. system("echo 0 > /sys/class/gpio/gpio103/value");
  949. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  950. system("echo 104 > /sys/class/gpio/export");
  951. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  952. system("echo 0 > /sys/class/gpio/gpio104/value");
  953. #ifdef SystemLogMessage
  954. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK \n");
  955. #endif
  956. }
  957. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  958. {
  959. int fd,wrd;
  960. unsigned char *buf;
  961. unsigned int ChkSum,ChkSumOrg;
  962. if((buf=malloc(MtdBlockSize))==NULL)
  963. {
  964. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  965. if(ShmStatusCodeData!=NULL)
  966. {
  967. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  968. }
  969. sleep(5);
  970. system("reboot -f");
  971. sleep(5);
  972. system("reboot -f");
  973. }
  974. memset(buf, 0, MtdBlockSize);
  975. //================================================
  976. // Load configuration from mtdblock10
  977. //================================================
  978. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  979. //fd = open("/dev/mtdblock10", O_RDWR);
  980. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  981. if (fd < 0)
  982. {
  983. free(buf);
  984. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  985. if(ShmStatusCodeData!=NULL)
  986. {
  987. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  988. }
  989. sleep(5);
  990. system("reboot -f");
  991. sleep(5);
  992. system("reboot -f");
  993. }
  994. wrd=read(fd, buf, MtdBlockSize);
  995. close(fd);
  996. if(wrd<MtdBlockSize)
  997. {
  998. free(buf);
  999. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  1000. if(ShmStatusCodeData!=NULL)
  1001. {
  1002. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1003. }
  1004. sleep(5);
  1005. system("reboot -f");
  1006. sleep(5);
  1007. system("reboot -f");
  1008. }
  1009. ChkSum=0;
  1010. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1011. // {
  1012. // ChkSum+=buf[wrd];
  1013. // }
  1014. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1015. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1016. {
  1017. ChkSum+=buf[wrd];
  1018. }
  1019. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1020. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1021. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1022. //================================================
  1023. // Load configuration from mtdblock11
  1024. //================================================
  1025. if(ChkSum!=ChkSumOrg)
  1026. {
  1027. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  1028. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1029. //fd = open("/dev/mtdblock11", O_RDWR);
  1030. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1031. if (fd < 0)
  1032. {
  1033. free(buf);
  1034. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  1035. if(ShmStatusCodeData!=NULL)
  1036. {
  1037. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1038. }
  1039. sleep(5);
  1040. system("reboot -f");
  1041. sleep(5);
  1042. system("reboot -f");
  1043. }
  1044. memset(buf, 0, MtdBlockSize);
  1045. wrd=read(fd, buf,MtdBlockSize);
  1046. close(fd);
  1047. if(wrd<MtdBlockSize)
  1048. {
  1049. free(buf);
  1050. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  1051. if(ShmStatusCodeData!=NULL)
  1052. {
  1053. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1054. }
  1055. sleep(5);
  1056. system("reboot -f");
  1057. sleep(5);
  1058. system("reboot -f");
  1059. }
  1060. ChkSum=0;
  1061. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1062. // {
  1063. // ChkSum+=buf[wrd];
  1064. // }
  1065. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1066. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1067. {
  1068. ChkSum+=buf[wrd];
  1069. }
  1070. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1071. //================================================
  1072. // Load configuration from mtdblock12 (Factory default)
  1073. //================================================
  1074. if(ChkSum!=ChkSumOrg)
  1075. {
  1076. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  1077. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1078. //fd = open("/dev/mtdblock12", O_RDWR);
  1079. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1080. if (fd < 0)
  1081. {
  1082. free(buf);
  1083. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  1084. if(ShmStatusCodeData!=NULL)
  1085. {
  1086. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1087. }
  1088. sleep(5);
  1089. system("reboot -f");
  1090. sleep(5);
  1091. system("reboot -f");
  1092. }
  1093. memset(buf, 0, MtdBlockSize);
  1094. wrd=read(fd, buf,MtdBlockSize);
  1095. close(fd);
  1096. if(wrd<MtdBlockSize)
  1097. {
  1098. free(buf);
  1099. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  1100. if(ShmStatusCodeData!=NULL)
  1101. {
  1102. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1103. }
  1104. sleep(5);
  1105. system("reboot -f");
  1106. sleep(5);
  1107. system("reboot -f");
  1108. }
  1109. ChkSum=0;
  1110. // for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  1111. // {
  1112. // ChkSum+=buf[wrd];
  1113. // }
  1114. // memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  1115. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1116. {
  1117. ChkSum+=buf[wrd];
  1118. }
  1119. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1120. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1121. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1122. if(ChkSum!=ChkSumOrg)
  1123. {
  1124. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  1125. free(buf);
  1126. //system("cd /root;./FactoryConfig -m");
  1127. sleep(5);
  1128. char facBuf[256];
  1129. sprintf(facBuf, "cd /root;./FactoryConfig -m %s %s",
  1130. ShmSysConfigAndInfo->SysConfig.ModelName,
  1131. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  1132. system(facBuf);
  1133. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1134. system("sync");
  1135. sleep(5);
  1136. system("reboot -f");
  1137. sleep(5);
  1138. system("reboot -f");
  1139. return FAIL;
  1140. }
  1141. }
  1142. }
  1143. //load OK
  1144. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1145. free(buf);
  1146. system("rm -f /mnt/EvseConfig.bin");
  1147. // SysConfig in flash is empty (0xffffffff)
  1148. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1149. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1150. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1151. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1152. {
  1153. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1154. {
  1155. PRINTF_FUNC("Model name over length.\n");
  1156. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1157. }
  1158. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1159. {
  1160. PRINTF_FUNC("Model serial number over length.\n");
  1161. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1162. }
  1163. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1164. {
  1165. PRINTF_FUNC("System id over length.\n");
  1166. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1167. }
  1168. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1169. {
  1170. PRINTF_FUNC("Ethernet dhcp config is null.\n");
  1171. }
  1172. system("cd /root;./Module_FactoryConfig -m");
  1173. sleep(3);
  1174. system("/usr/bin/run_evse_restart.sh");
  1175. }
  1176. PRINTF_FUNC("*********************************************************** \n");
  1177. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  1178. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1179. {
  1180. memcpy((char*)ShmOCPP16Data->ChargeBoxId,
  1181. (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1182. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1183. }
  1184. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1185. {
  1186. memcpy((char*)ShmOCPP20Data->ChargeBoxId,
  1187. (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  1188. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1189. }
  1190. return PASS;
  1191. }
  1192. int isRouteFail()
  1193. {
  1194. int result = YES;
  1195. FILE *fp;
  1196. char buf[512];
  1197. fp = popen("route -n", "r");
  1198. if(fp != NULL)
  1199. {
  1200. while(fgets(buf, sizeof(buf), fp) != NULL)
  1201. {
  1202. if(strstr(buf, "eth0") != NULL)
  1203. result = NO;
  1204. }
  1205. }
  1206. pclose(fp);
  1207. return result;
  1208. }
  1209. int isReachableInternet()
  1210. {
  1211. int result = FAIL;
  1212. FILE *fp;
  1213. char cmd[256];
  1214. char buf[512];
  1215. char tmp[512];
  1216. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1217. // {
  1218. // result = PASS;
  1219. // }
  1220. // else
  1221. {
  1222. strcpy(cmd, "ifconfig eth0");
  1223. fp = popen(cmd, "r");
  1224. if (fp != NULL)
  1225. {
  1226. while(fgets(buf, sizeof(buf), fp) != NULL)
  1227. {
  1228. if (strstr(buf, "inet addr:") > 0)
  1229. {
  1230. sscanf(buf, "%*s%s", tmp);
  1231. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  1232. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  1233. {
  1234. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1235. }
  1236. sscanf(buf, "%*s%*s%*s%s", tmp);
  1237. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  1238. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  1239. {
  1240. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  1241. }
  1242. }
  1243. }
  1244. }
  1245. pclose(fp);
  1246. memset(buf, 0x00, sizeof(buf));
  1247. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1248. {
  1249. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1250. fp = popen(cmd, "r");
  1251. if(fp != NULL)
  1252. {
  1253. while(fgets(buf, sizeof(buf), fp) != NULL)
  1254. {
  1255. if(strstr(buf, "transmitted") > 0)
  1256. {
  1257. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1258. if(strstr(buf,"100%") != NULL)
  1259. {
  1260. }
  1261. else
  1262. {
  1263. result = PASS;
  1264. }
  1265. //DEBUG_INFO("%s",buf);
  1266. //DEBUG_INFO("%s\n",tmp);
  1267. }
  1268. }
  1269. }
  1270. pclose(fp);
  1271. }
  1272. }
  1273. return result;
  1274. }
  1275. void InitEthernet()
  1276. {
  1277. system("ifconfig eth0 down");// eth0 down
  1278. system("ifconfig eth1 down");// eth1 down
  1279. sleep(2);
  1280. char tmpbuf[256];
  1281. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1282. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1283. sleep(2);
  1284. //Init Eth0 for internet
  1285. memset(tmpbuf,0,256);
  1286. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1287. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1288. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1289. system(tmpbuf);
  1290. memset(tmpbuf,0,256);
  1291. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1292. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1293. system(tmpbuf);
  1294. //system("ifconfig lo up");
  1295. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1296. //Init Eth1 for administrator tool
  1297. memset(tmpbuf,0,256);
  1298. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  1299. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  1300. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  1301. system(tmpbuf);
  1302. //Run DHCP client if enabled
  1303. system("killall udhcpc");
  1304. system("rm -rf /etc/resolv.conf");
  1305. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1306. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  1307. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1308. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1309. {
  1310. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1311. system(tmpbuf);
  1312. }
  1313. //Upgrade system id to /etc/hostname
  1314. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1315. system(tmpbuf);
  1316. pid_t pid = fork();
  1317. uint8_t cnt_pingDNS_Fail;
  1318. if(pid == 0)
  1319. {
  1320. for(;;)
  1321. {
  1322. if (isRouteFail())
  1323. {
  1324. //PRINTF_FUNC("eth0 not in route, restart eht0. \n");
  1325. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1326. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1327. {
  1328. InitialDHCP();
  1329. }
  1330. else
  1331. {
  1332. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1333. memset(tmpbuf,0,256);
  1334. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1335. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1336. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1337. system(tmpbuf);
  1338. memset(tmpbuf,0,256);
  1339. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1340. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1341. system(tmpbuf);
  1342. }
  1343. }
  1344. if(isReachableInternet() == PASS)
  1345. {
  1346. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1347. cnt_pingDNS_Fail = 0;
  1348. }
  1349. else
  1350. {
  1351. if(++cnt_pingDNS_Fail > 3)
  1352. {
  1353. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1354. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1355. {
  1356. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1357. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1358. system(tmpbuf);
  1359. }
  1360. else
  1361. {
  1362. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1363. memset(tmpbuf,0,256);
  1364. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1365. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1366. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1367. system(tmpbuf);
  1368. memset(tmpbuf,0,256);
  1369. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1370. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1371. system(tmpbuf);
  1372. }
  1373. cnt_pingDNS_Fail = 0;
  1374. }
  1375. }
  1376. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1377. if(ethResult == YES)
  1378. {
  1379. system("/sbin/ifmetric eth0 0");
  1380. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') ||
  1381. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1382. system("/sbin/ifmetric mlan0 1");
  1383. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') ||
  1384. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1385. system("/sbin/ifmetric ppp0 2");
  1386. }
  1387. if (!ethResult &&
  1388. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1389. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1390. {
  1391. //ethResult = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn;
  1392. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1393. if (ethResult)
  1394. {
  1395. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1396. {
  1397. system("/sbin/ifmetric eth0 1");
  1398. system("/sbin/ifmetric mlan0 0");
  1399. }
  1400. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1401. system("/sbin/ifmetric ppp0 2");
  1402. }
  1403. }
  1404. if (!ethResult &&
  1405. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1406. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1407. {
  1408. //ethResult = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn;
  1409. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == YES ? NO : YES;
  1410. if (ethResult)
  1411. {
  1412. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1413. system("/sbin/ifmetric mlan0 2");
  1414. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1415. {
  1416. system("/sbin/ifmetric eth0 1");
  1417. system("/sbin/ifmetric ppp0 0");
  1418. }
  1419. }
  1420. }
  1421. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1422. sleep(5);
  1423. }
  1424. }
  1425. #ifdef SystemLogMessage
  1426. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK. \n");
  1427. #endif
  1428. }
  1429. int InitialRfidPort()
  1430. {
  1431. int uartO2 = open(rfidPortName, O_RDWR);
  1432. struct termios tios;
  1433. if (uartO2 != FAIL)
  1434. {
  1435. ioctl (uartO2, TCGETS, &tios);
  1436. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1437. tios.c_lflag = 0;
  1438. tios.c_iflag = 0;
  1439. tios.c_oflag = 0;
  1440. tios.c_cc[VMIN] = 0;
  1441. tios.c_cc[VTIME] = (unsigned char) 1;
  1442. tios.c_lflag = 0;
  1443. tcflush(uartO2, TCIFLUSH);
  1444. ioctl(uartO2, TCSETS, &tios);
  1445. }
  1446. if (uartO2 < 0)
  1447. {
  1448. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1449. }
  1450. return uartO2;
  1451. }
  1452. void GetMacAddress()
  1453. {
  1454. for (byte index = 0; index < 2; index++)
  1455. {
  1456. int fd;
  1457. struct ifreq ifr;
  1458. char tarEth[5];
  1459. char Mac[18];
  1460. sprintf(tarEth,"eth%d",index);
  1461. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1462. ifr.ifr_addr.sa_family = AF_INET;
  1463. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1464. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1465. close(fd);
  1466. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1467. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1468. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1469. if (index == 0)
  1470. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1471. else
  1472. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1473. }
  1474. }
  1475. void GetFirmwareVersion()
  1476. {
  1477. // Get CSU root file system version
  1478. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1479. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1480. for(uint8_t idx=0;idx<3;idx++)
  1481. {
  1482. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J' ||
  1483. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'K')
  1484. {
  1485. chademo++;
  1486. count++;
  1487. }
  1488. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1489. {
  1490. gb++;
  1491. count++;
  1492. }
  1493. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1494. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E' ||
  1495. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'T' ||
  1496. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'D' ||
  1497. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'M' ||
  1498. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'N')
  1499. {
  1500. ccs++;
  1501. count++;
  1502. }
  1503. }
  1504. if (count == 1)
  1505. {
  1506. if (chademo > 0)
  1507. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1508. else if (ccs > 0)
  1509. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1510. else if (gb > 0)
  1511. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1512. }
  1513. else
  1514. {
  1515. if (chademo == 1 && ccs == 1)
  1516. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1517. else if (chademo == 1 && gb == 1)
  1518. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1519. else if (ccs == 1 && gb == 1)
  1520. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1521. else if (chademo == 2)
  1522. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '7';
  1523. else if (ccs == 2)
  1524. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '8';
  1525. else if (gb == 2)
  1526. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '9';
  1527. }
  1528. // Get network option from model name
  1529. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1530. {
  1531. case 'B':
  1532. case 'U':
  1533. //Blue tooth
  1534. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1535. break;
  1536. case 'W':
  1537. // WIFI
  1538. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1539. break;
  1540. case 'T':
  1541. // 3G/4G
  1542. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1543. break;
  1544. case 'D':
  1545. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1546. break;
  1547. default:
  1548. // LAN
  1549. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1550. break;
  1551. }
  1552. // Get rating power from model name
  1553. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1554. // Get IEC or UL
  1555. char _buf[3] = {0};
  1556. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 1);
  1557. if (strcmp(_buf, "Y") == EQUAL || strcmp(_buf, "Y") == EQUAL)
  1558. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1559. else if (strcmp(_buf, "W") == EQUAL)
  1560. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1561. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
  1562. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
  1563. if (strcmp(_buf, "PN") == EQUAL ||
  1564. strcmp(_buf, "N7") == EQUAL ||
  1565. strcmp(_buf, "NX") == EQUAL ||
  1566. strcmp(_buf, "OL") == EQUAL)
  1567. ShmDcCommonData->ShowLogoFlag = NO;
  1568. else
  1569. ShmDcCommonData->ShowLogoFlag = YES;
  1570. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'G')
  1571. ShmDcCommonData->chargerType = CHARGER_TYPE_SIMPLE;
  1572. else
  1573. ShmDcCommonData->chargerType = CHARGER_TYPE_STANDARD;
  1574. }
  1575. void InitialGunIndexToUnUse()
  1576. {
  1577. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  1578. {
  1579. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index = NO_DEFINE;
  1580. }
  1581. for (byte index = 0; index < CCS_QUANTITY; index++)
  1582. {
  1583. ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index = NO_DEFINE;
  1584. }
  1585. for (byte index = 0; index < GB_QUANTITY; index++)
  1586. {
  1587. ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index = NO_DEFINE;
  1588. }
  1589. for (byte index = 0; index < AC_QUANTITY; index++)
  1590. {
  1591. ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index = NO_DEFINE;
  1592. }
  1593. }
  1594. void InitialShareMemoryInfo()
  1595. {
  1596. FILE *fp;
  1597. char cmd[512];
  1598. char buf[512];
  1599. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1600. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1601. // sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1602. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1603. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1604. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1605. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1606. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1607. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1608. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1609. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1610. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1611. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1612. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1613. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1614. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1615. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1616. sprintf(cmd, "/bin/uname -r");
  1617. fp = popen(cmd, "r");
  1618. if(fp == NULL)
  1619. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1620. else
  1621. {
  1622. while(fgets(buf, sizeof(buf), fp) != NULL)
  1623. {
  1624. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1625. }
  1626. }
  1627. GetFirmwareVersion();
  1628. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1629. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1630. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1631. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1632. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1633. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1634. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1635. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1636. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1637. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1638. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1639. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1640. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1641. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1642. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1643. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1644. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1645. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1646. ShmSysConfigAndInfo->SysInfo.InternetConn = NO;
  1647. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = NO;
  1648. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1649. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1650. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1651. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1652. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1653. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = NO;
  1654. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1655. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1656. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1657. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1658. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail = NO;
  1659. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1660. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1661. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1662. ShmRelayModuleData->SelfTest_Comp = NO;
  1663. ShmFanModuleData->SelfTest_Comp = NO;
  1664. ShmLedModuleData->SelfTest_Comp = NO;
  1665. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1666. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1667. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1668. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1669. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOTTING;
  1670. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  1671. ShmFanModuleData->TestFanSpeed = 0;
  1672. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1673. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1674. InitialGunIndexToUnUse();
  1675. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1676. ShmDcCommonData->CcsVersion = _CCS_VERSION_CHECK_TAG_V015S0;
  1677. ShmDcCommonData->psuKeepCommunication = NO;
  1678. ShmDcCommonData->acContactSwitch = NO;
  1679. ShmDcCommonData->ConnectErrList[0].GunErrMessage = 0;
  1680. ShmDcCommonData->ConnectErrList[1].GunErrMessage = 0;
  1681. ShmDcCommonData->LcmFwVersion = 0;
  1682. ShmDcCommonData->minOutputCur = 0;
  1683. ShmDcCommonData->evBoardResetFlag = NO;
  1684. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1685. }
  1686. int Initialization()
  1687. {
  1688. // 初始化卡號驗證的 Flag
  1689. ClearAuthorizedFlag();
  1690. // 初始化插槍驗證的 Flag
  1691. ClearDetectPluginFlag();
  1692. // UART 2 for Rfid
  1693. rfidFd = InitialRfidPort();
  1694. int pinOut[2] = { 116, 115 }; //D13(Cha)、C13(CCS)
  1695. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1696. {
  1697. chargingInfo[count]->RemoteStartFlag = NO;
  1698. chargingInfo[count]->ReservedStartFlag = NO;
  1699. if (chargingInfo[count]->Type == _Type_Chademo)
  1700. {
  1701. gpio_set_value(pinOut[count], 0x00);
  1702. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1703. }
  1704. else if (chargingInfo[count]->Type == _Type_GB)
  1705. {
  1706. gpio_set_value(pinOut[count], 0x00);
  1707. ShmGBTData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1708. }
  1709. else if (chargingInfo[count]->Type == _Type_CCS)
  1710. {
  1711. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1712. gpio_set_value(pinOut[1], 0x01);
  1713. else
  1714. gpio_set_value(pinOut[count], 0x01);
  1715. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1716. }
  1717. strcpy((char *)ShmDcCommonData->_reserved_UserId[count], "");
  1718. }
  1719. byte acDirIndex = 0;
  1720. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  1721. {
  1722. if (count == 1)
  1723. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  1724. ocpp_set_noErrorCode_cmd(count + acDirIndex, "NoError");
  1725. }
  1726. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  1727. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  1728. {
  1729. ac_chargingInfo[count]->RemoteStartFlag = NO;
  1730. if (ac_chargingInfo[count]->Type == _Type_AC)
  1731. {
  1732. ac_chargingInfo[count]->SelfTest_Comp = NO;
  1733. ocpp_set_noErrorCode_cmd(count + hasDc, "NoError");
  1734. }
  1735. }
  1736. PRINTF_FUNC("Information check done \n");
  1737. return PASS;
  1738. }
  1739. bool InitialSystemDefaultConfig()
  1740. {
  1741. bool result = true;
  1742. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1743. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  1744. InitGPIO();
  1745. InitEthernet();
  1746. GetMacAddress();
  1747. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1748. // sleep(3);
  1749. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1750. // sleep(1);
  1751. // system("/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up");
  1752. return result;
  1753. }
  1754. bool DisplaySelfTestFailReason()
  1755. {
  1756. bool result = false;
  1757. // RB、FB、407、EV 小板中有些板子無回應
  1758. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1759. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; result = true; }
  1760. if (ShmFanModuleData->SelfTest_Comp == NO)
  1761. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; result = true; }
  1762. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1763. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; result = true; }
  1764. // if (ShmLedModuleData->SelfTest_Comp == NO)
  1765. // { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES; }
  1766. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES)
  1767. { result = true; }
  1768. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1769. {
  1770. if (chargingInfo[index]->Type == _Type_Chademo)
  1771. {
  1772. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1773. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; result = true; }
  1774. }
  1775. else if (chargingInfo[index]->Type == _Type_GB)
  1776. {
  1777. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1778. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; result = true; }
  1779. }
  1780. else if (chargingInfo[index]->Type == _Type_CCS)
  1781. {
  1782. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1783. {
  1784. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1785. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; result = true; }
  1786. }
  1787. }
  1788. }
  1789. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1790. {
  1791. // 先借 GBT 顯示
  1792. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  1793. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; result = true; }
  1794. }
  1795. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1796. {
  1797. // AC Contact 未搭上
  1798. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1799. result = true;
  1800. }
  1801. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  1802. {
  1803. result = true;
  1804. }
  1805. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  1806. {
  1807. // PSU 通訊問題
  1808. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1809. result = true;
  1810. }
  1811. return result;
  1812. }
  1813. void SelfTestRun()
  1814. {
  1815. bool evInitFlag = false;
  1816. StartSystemTimeoutDet(Timeout_SelftestChk);
  1817. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1818. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  1819. GetTimeoutValue(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) <= 20)
  1820. {
  1821. ReviewCriticalAlarm();
  1822. ChkPrimaryStatus();
  1823. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL ||
  1824. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuDipSwitchStestFail == YES)
  1825. {
  1826. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1827. return;
  1828. }
  1829. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  1830. {
  1831. if (ShmPsuData->Work_Step == _NO_WORKING ||
  1832. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  1833. {
  1834. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1835. return;
  1836. }
  1837. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  1838. {
  1839. case _STEST_VERSION:
  1840. {
  1841. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
  1842. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  1843. {
  1844. //PRINTF_FUNC("RB pass \n");
  1845. ShmRelayModuleData->SelfTest_Comp = YES;
  1846. }
  1847. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  1848. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  1849. {
  1850. //PRINTF_FUNC("Fan pass \n");
  1851. ShmFanModuleData->SelfTest_Comp = YES;
  1852. }
  1853. if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
  1854. ShmPrimaryMcuData->version[0] != '\0')
  1855. {
  1856. //PRINTF_FUNC("407 pass \n");
  1857. ShmPrimaryMcuData->SelfTest_Comp = YES;
  1858. }
  1859. // EV 小板
  1860. if (!evInitFlag)
  1861. {
  1862. evInitFlag = YES;
  1863. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1864. {
  1865. if (chargingInfo[index]->Type == _Type_Chademo)
  1866. {
  1867. if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
  1868. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1869. {
  1870. //PRINTF_FUNC("chademo pass \n");
  1871. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1872. }
  1873. else
  1874. {
  1875. //PRINTF_FUNC("chademo fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1876. evInitFlag = NO;
  1877. }
  1878. }
  1879. else if (chargingInfo[index]->Type == _Type_GB)
  1880. {
  1881. if (strlen((char *)ShmGBTData->evse[chargingInfo[index]->type_index].version) != 0 ||
  1882. ShmGBTData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  1883. {
  1884. //PRINTF_FUNC("GBT pass \n");
  1885. ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1886. }
  1887. else
  1888. {
  1889. //PRINTF_FUNC("GBT fw lose...... %s \n", ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version);
  1890. evInitFlag = NO;
  1891. }
  1892. }
  1893. else if (chargingInfo[index]->Type == _Type_CCS)
  1894. {
  1895. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1896. {
  1897. if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
  1898. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  1899. {
  1900. //PRINTF_FUNC("ccs fw = %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1901. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1902. }
  1903. else
  1904. {
  1905. //PRINTF_FUNC("ccs fw lose...... %s \n", ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1906. evInitFlag = NO;
  1907. }
  1908. }
  1909. }
  1910. }
  1911. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1912. {
  1913. if (ac_chargingInfo[index]->Type == _Type_AC)
  1914. {
  1915. if (strlen((char *)ac_chargingInfo[index]->version) != 0 ||
  1916. ac_chargingInfo[index]->version[0] != '\0')
  1917. {
  1918. ac_chargingInfo[index]->SelfTest_Comp = YES;
  1919. }
  1920. else
  1921. {
  1922. evInitFlag = NO;
  1923. }
  1924. }
  1925. }
  1926. }
  1927. if (ShmFanModuleData->SelfTest_Comp &&
  1928. ShmRelayModuleData->SelfTest_Comp &&
  1929. ShmPrimaryMcuData->SelfTest_Comp &&
  1930. evInitFlag)
  1931. {
  1932. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  1933. }
  1934. }
  1935. break;
  1936. case _STEST_AC_CONTACTOR:
  1937. {
  1938. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  1939. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  1940. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  1941. {
  1942. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1943. PRINTF_FUNC("Communication board pass. \n");
  1944. }
  1945. }
  1946. break;
  1947. case _STEST_PSU_DETECT:
  1948. {
  1949. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  1950. {
  1951. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1952. }
  1953. }
  1954. break;
  1955. case _STEST_PSU_CAP:
  1956. {
  1957. // 此測試是要確認當前總輸出能力
  1958. // 如果沒有 PSU 模組請 bypass
  1959. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  1960. {
  1961. sleep(1);
  1962. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1963. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1964. }
  1965. }
  1966. break;
  1967. }
  1968. }
  1969. else
  1970. break;
  1971. usleep(100000);
  1972. }
  1973. }
  1974. int SpawnTask()
  1975. {
  1976. sleep(2);
  1977. system("/root/Module_EventLogging &");
  1978. system("/root/Module_PrimaryComm &");
  1979. system("/root/Module_EvComm &");
  1980. system("/root/Module_LcmControl &");
  1981. system("/root/Module_InternalComm &");
  1982. system("/root/Module_PsuComm &");
  1983. system("/root/Module_ProduceUtils &");
  1984. ocpp_process_start();
  1985. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1986. {
  1987. system("/root/Module_4g &");
  1988. }
  1989. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1990. {
  1991. system("/root/Module_Wifi &");
  1992. }
  1993. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  1994. {
  1995. system("/root/Module_4g &");
  1996. system("/root/Module_Wifi &");
  1997. }
  1998. return PASS;
  1999. }
  2000. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2001. {
  2002. int result = PASS;
  2003. int fd,wrd;
  2004. unsigned int i,Chk;
  2005. unsigned char *ptr, *BufTmp;
  2006. Chk=0;
  2007. ptr=(unsigned char *)UsrData;
  2008. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2009. {
  2010. memset(BufTmp, 0, MtdBlockSize);
  2011. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2012. for(i=0; i<MtdBlockSize-4; i++)
  2013. Chk+=*(ptr+i);
  2014. memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
  2015. fd = open("/dev/mtdblock10", O_RDWR);
  2016. if (fd > 0)
  2017. {
  2018. wrd=write(fd, BufTmp, MtdBlockSize);
  2019. close(fd);
  2020. if(wrd >= MtdBlockSize)
  2021. {
  2022. fd = open("/dev/mtdblock11", O_RDWR);
  2023. if (fd > 0)
  2024. {
  2025. wrd=write(fd, BufTmp, MtdBlockSize);
  2026. close(fd);
  2027. if(wrd < MtdBlockSize)
  2028. {
  2029. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  2030. result = FAIL;
  2031. }
  2032. }
  2033. else
  2034. {
  2035. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  2036. result = FAIL;
  2037. }
  2038. }
  2039. else
  2040. {
  2041. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  2042. result = FAIL;
  2043. }
  2044. }
  2045. else
  2046. {
  2047. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  2048. result = FAIL;
  2049. }
  2050. }
  2051. else
  2052. {
  2053. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  2054. result = FAIL;
  2055. }
  2056. if(BufTmp != NULL)
  2057. free(BufTmp);
  2058. return result;
  2059. }
  2060. //===============================================
  2061. // Common Detect Chk - Stop Charging ?
  2062. //===============================================
  2063. bool isEvBoardStopChargeFlag(byte gunIndex)
  2064. {
  2065. return chargingInfo[gunIndex]->StopChargeFlag;
  2066. }
  2067. bool isEvBoardNormalStopChargeFlag(byte gunIndex)
  2068. {
  2069. return chargingInfo[gunIndex]->NormalStopChargeFlag;
  2070. }
  2071. //===============================================
  2072. // 插槍鎖槍狀況
  2073. //===============================================
  2074. void ClearDetectPluginFlag()
  2075. {
  2076. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2077. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2078. {
  2079. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  2080. chargingInfo[gun_index]->RemoteStartFlag = NO;
  2081. if (chargingInfo[gun_index]->ReservedStartFlag == YES)
  2082. chargingInfo[gun_index]->ReservedStartFlag = NO;
  2083. }
  2084. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  2085. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2086. }
  2087. void DetectPluginStart()
  2088. {
  2089. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  2090. }
  2091. bool isDetectPlugin()
  2092. {
  2093. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  2094. return YES;
  2095. return NO;
  2096. }
  2097. bool isEvGunLocked(byte gunIndex)
  2098. {
  2099. return chargingInfo[gunIndex]->GunLocked;
  2100. }
  2101. //===============================================
  2102. // Common Detect Chk - Chademo
  2103. //===============================================
  2104. bool isEvContactorWelding_chademo(byte gunIndex)
  2105. {
  2106. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  2107. }
  2108. bool isEvStopReq_chademo(byte gunIndex)
  2109. {
  2110. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  2111. }
  2112. byte isPrechargeStatus_chademo(byte gunIndex)
  2113. {
  2114. byte result = 0x00;
  2115. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2116. return result;
  2117. }
  2118. //===============================================
  2119. // Common Detect Chk - GB
  2120. //===============================================
  2121. byte isPrechargeStatus_gb(byte gunIndex)
  2122. {
  2123. byte result = 0x00;
  2124. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2125. return result;
  2126. }
  2127. //===============================================
  2128. // Common Detect Chk - CCS
  2129. //===============================================
  2130. byte isPrechargeStatus_ccs(byte gunIndex)
  2131. {
  2132. byte result = 0x00;
  2133. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2134. {
  2135. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  2136. }
  2137. return result;
  2138. }
  2139. //===============================================
  2140. // Callback
  2141. //===============================================
  2142. void DisplayChargingInfo()
  2143. {
  2144. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  2145. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2146. {
  2147. if (chargingInfo[i]->SystemStatus != SYS_MODE_IDLE &&
  2148. chargingInfo[i]->SystemStatus != SYS_MODE_RESERVATION)
  2149. {
  2150. ChangeGunSelectByIndex(i);
  2151. usleep(50000);
  2152. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2153. return;
  2154. }
  2155. }
  2156. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2157. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  2158. ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_COMPLETE)
  2159. {
  2160. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2161. }
  2162. usleep(50000);
  2163. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2164. }
  2165. void _AutoReturnTimeout()
  2166. {
  2167. PRINTF_FUNC("*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  2168. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2169. {
  2170. ClearDetectPluginFlag();
  2171. }
  2172. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2173. {
  2174. DetectPluginStart();
  2175. }
  2176. usleep(50000);
  2177. stopChargingChkByCard = false;
  2178. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2179. }
  2180. void _SelfTestTimeout()
  2181. {
  2182. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  2183. {
  2184. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  2185. {
  2186. ChargingAlarmProcess(gun_index);
  2187. }
  2188. }
  2189. ShmPsuData->Work_Step = _NO_WORKING;
  2190. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  2191. PRINTF_FUNC("Self test timeout. \n");
  2192. }
  2193. void _AuthorizedTimeout()
  2194. {
  2195. //if(IsAuthorizingMode())
  2196. {
  2197. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  2198. isCardScan = false;
  2199. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2200. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  2201. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2202. ClearAuthorizedFlag();
  2203. }
  2204. }
  2205. void _DetectPlugInTimeout()
  2206. {
  2207. PRINTF_FUNC("*********** _DetectPlugInTimeout (%d)*********** \n", _connectionTimeout);
  2208. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2209. ClearDetectPluginFlag();
  2210. usleep(50000);
  2211. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2212. }
  2213. void _DetectEvChargingEnableTimeout(byte gunIndex)
  2214. {
  2215. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2216. {
  2217. if(!isEvGunLocked(gunIndex))
  2218. {
  2219. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  2220. }
  2221. }
  2222. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2223. {
  2224. if(!isEvGunLocked(gunIndex))
  2225. {
  2226. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  2227. }
  2228. }
  2229. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  2230. {
  2231. if(!isEvGunLocked(gunIndex))
  2232. {
  2233. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  2234. }
  2235. }
  2236. ChargingTerminalProcess(gunIndex);
  2237. _AutoReturnTimeout();
  2238. }
  2239. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  2240. {
  2241. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  2242. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  2243. {
  2244. setChargerMode(gunIndex, SYS_MODE_IDLE);
  2245. _AutoReturnTimeout();
  2246. }
  2247. }
  2248. void _PrepareTimeout(byte gunIndex)
  2249. {
  2250. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  2251. setChargerMode(gunIndex, SYS_MODE_IDLE);
  2252. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  2253. _AutoReturnTimeout();
  2254. }
  2255. void _CcsPrechargeTimeout(byte gunIndex)
  2256. {
  2257. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  2258. setChargerMode(gunIndex, SYS_MODE_IDLE);
  2259. }
  2260. //===============================================
  2261. // 卡號驗證
  2262. //===============================================
  2263. void AuthorizingStart()
  2264. {
  2265. ocpp_set_authorizeReq_cmd(YES);
  2266. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  2267. }
  2268. void ClearAuthorizedFlag()
  2269. {
  2270. ocpp_set_authorizeConf_cmd(NO);
  2271. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  2272. }
  2273. bool isAuthorizedComplete()
  2274. {
  2275. return ocpp_get_authorize_conf();
  2276. }
  2277. bool IsAuthorizingMode()
  2278. {
  2279. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  2280. return false;
  2281. return true;
  2282. }
  2283. //===============================================
  2284. // 紀錄 Alarm Code
  2285. //===============================================
  2286. void RecordAlarmCode(byte gunIndex, char *code)
  2287. {
  2288. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) == EQUAL)
  2289. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  2290. if (strcmp(code, "012234") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = YES;
  2291. else if (strcmp(code, "012235") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  2292. else if (strcmp(code, "012236") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = YES;
  2293. else if (strcmp(code, "012288") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = YES;
  2294. else if (strcmp(code, "012289") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = YES;
  2295. else if (strcmp(code, "012290") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = YES;
  2296. else if (strcmp(code, "012229") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = YES;
  2297. else if (strcmp(code, "012230") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = YES;
  2298. else if (strcmp(code, "012231") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = YES;
  2299. else if (strcmp(code, "012296") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = YES;
  2300. else if (strcmp(code, "012297") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = YES;
  2301. else if (strcmp(code, "012298") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = YES;
  2302. else if (strcmp(code, "011011") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = YES;
  2303. else if (strcmp(code, "011013") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = YES;
  2304. else if (strcmp(code, "011015") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = YES;
  2305. else if (strcmp(code, "011012") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = YES;
  2306. else if (strcmp(code, "011014") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = YES;
  2307. else if (strcmp(code, "011016") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = YES;
  2308. else if (strcmp(code, "011018") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = YES;
  2309. else if (strcmp(code, "011019") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = YES;
  2310. else if (strcmp(code, "011020") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = YES;
  2311. else if (strcmp(code, "012320") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = YES;
  2312. else if (strcmp(code, "012321") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = YES;
  2313. else if (strcmp(code, "012322") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = YES;
  2314. }
  2315. void ResetChargerAlarmCode(byte gunIndex, char *code)
  2316. {
  2317. if (strcmp(code, "012229") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  2318. else if (strcmp(code, "012230") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  2319. else if (strcmp(code, "012231") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  2320. else if (strcmp(code, "011011") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  2321. else if (strcmp(code, "011013") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  2322. else if (strcmp(code, "011015") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  2323. else if (strcmp(code, "011012") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  2324. else if (strcmp(code, "011014") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  2325. else if (strcmp(code, "011016") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  2326. else if (strcmp(code, "011018") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  2327. else if (strcmp(code, "011019") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  2328. else if (strcmp(code, "011020") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  2329. else if (strcmp(code, "012320") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = NO;
  2330. else if (strcmp(code, "012321") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = NO;
  2331. else if (strcmp(code, "012322") == EQUAL) ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = NO;
  2332. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2333. {
  2334. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012229", 6) == EQUAL ||
  2335. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012230", 6) == EQUAL ||
  2336. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012231", 6) == EQUAL ||
  2337. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011011", 6) == EQUAL ||
  2338. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011013", 6) == EQUAL ||
  2339. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011015", 6) == EQUAL ||
  2340. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011012", 6) == EQUAL ||
  2341. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011014", 6) == EQUAL ||
  2342. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011016", 6) == EQUAL ||
  2343. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011018", 6) == EQUAL ||
  2344. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011019", 6) == EQUAL ||
  2345. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "011020", 6) == EQUAL ||
  2346. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012320", 6) == EQUAL ||
  2347. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012321", 6) == EQUAL ||
  2348. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012322", 6) == EQUAL)
  2349. {
  2350. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2351. }
  2352. }
  2353. }
  2354. void ReleaseSysAlarmCode(byte gunIndex)
  2355. {
  2356. // 回 idle 後主要清除 GFD Trip、UVP、OVP、GFD Warning
  2357. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2358. {
  2359. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012251", 6) == EQUAL ||
  2360. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012252", 6) == EQUAL ||
  2361. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012324", 6) == EQUAL ||
  2362. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012325", 6) == EQUAL)
  2363. {
  2364. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2365. }
  2366. }
  2367. if (gunIndex == 0)
  2368. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_1 = NO;
  2369. else if (gunIndex == 1)
  2370. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_2 = NO;
  2371. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  2372. {
  2373. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  2374. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  2375. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP = NO;
  2376. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  2377. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectUCP = NO;
  2378. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2379. {
  2380. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL ||
  2381. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL ||
  2382. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012217", 6) == EQUAL ||
  2383. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012296", 6) == EQUAL)
  2384. {
  2385. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2386. }
  2387. }
  2388. }
  2389. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  2390. {
  2391. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  2392. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  2393. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP = NO;
  2394. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  2395. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectUCP = NO;
  2396. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2397. {
  2398. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL ||
  2399. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL ||
  2400. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012219", 6) == EQUAL ||
  2401. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012297", 6) == EQUAL)
  2402. {
  2403. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2404. }
  2405. }
  2406. }
  2407. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  2408. {
  2409. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  2410. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  2411. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP = NO;
  2412. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  2413. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectUCP = NO;
  2414. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6) != EQUAL)
  2415. {
  2416. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL ||
  2417. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL ||
  2418. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012221", 6) == EQUAL ||
  2419. strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012298", 6) == EQUAL)
  2420. {
  2421. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2422. }
  2423. }
  2424. }
  2425. ocpp_clear_errorCode_cmd(gunIndex);
  2426. }
  2427. void NormalStop(byte gunIndex)
  2428. {
  2429. PRINTF_FUNC("NormalStop (%d) \n", gunIndex);
  2430. }
  2431. //===============================================
  2432. // EmergencyStop and Charging Stop
  2433. //===============================================
  2434. void ChargingTerminalProcess(byte gunIndex)
  2435. {
  2436. setChargerMode(gunIndex, SYS_MODE_TERMINATING);
  2437. }
  2438. void ChargingAlarmProcess(byte gunIndex)
  2439. {
  2440. CheckSystemErrorFunction(gunIndex);
  2441. ocpp_set_errCode_cmd(gunIndex);
  2442. setChargerMode(gunIndex, SYS_MODE_ALARM);
  2443. }
  2444. void AcChargingTerminalProcess()
  2445. {
  2446. ac_chargingInfo[0]->SystemStatus = SYS_MODE_TERMINATING;
  2447. }
  2448. //void StopChargingProcessByString(byte level)
  2449. //{
  2450. // if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2451. // {
  2452. // ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2453. // }
  2454. //}
  2455. //void ReleaseChargingProcessByString(byte level)
  2456. //{
  2457. // if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2458. // ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2459. //}
  2460. // 一般錯誤停止充電處理函式
  2461. void BoardErrOccurByString(byte index, char *code)
  2462. {
  2463. //byte level = 1;
  2464. if ((chargingInfo[index]->SystemStatus > SYS_MODE_IDLE && chargingInfo[index]->SystemStatus < SYS_MODE_TERMINATING) ||
  2465. (chargingInfo[index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2466. {
  2467. PRINTF_FUNC("BoardErrOccurByString (%d) - (%s) \n", index, code);
  2468. if (strncmp(code, "023730", 6) == EQUAL &&
  2469. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2470. {
  2471. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2472. }
  2473. ChargingAlarmProcess(index);
  2474. }
  2475. //60StopChargingProcessByString(level);
  2476. }
  2477. // 急停狀況的停止充電處理函式
  2478. void EmcOccureThenStopCharging()
  2479. {
  2480. //byte level = 2;
  2481. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen...
  2482. // 其錯誤等級為 2
  2483. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2484. {
  2485. if ((chargingInfo[gun]->SystemStatus > SYS_MODE_IDLE && chargingInfo[gun]->SystemStatus < SYS_MODE_TERMINATING) ||
  2486. (chargingInfo[gun]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[gun]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2487. {
  2488. ChargingAlarmProcess(gun);
  2489. }
  2490. }
  2491. //StopChargingProcessByString(level);
  2492. }
  2493. void ReleaseBoardErrOccurByString(byte index, char *code)
  2494. {
  2495. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  2496. {
  2497. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2498. }
  2499. }
  2500. void ReleaseEmsOccureByString(byte index, char *code)
  2501. {
  2502. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2503. {
  2504. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2505. }
  2506. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2507. {
  2508. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2509. }
  2510. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2511. {
  2512. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2513. }
  2514. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2515. {
  2516. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2517. }
  2518. }
  2519. //===============================================
  2520. // 確認硬體 (按鈕) 狀態
  2521. //===============================================
  2522. bool leftBtnPush = false;
  2523. bool rightBtnPush = false;
  2524. void ChkPrimaryStatus()
  2525. {
  2526. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2527. {
  2528. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2529. EmcOccureThenStopCharging();
  2530. }
  2531. else
  2532. ReleaseEmsOccureByString(0, "012251");
  2533. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2534. {
  2535. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2536. EmcOccureThenStopCharging();
  2537. }
  2538. else
  2539. ReleaseEmsOccureByString(0, "012238");
  2540. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2541. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2542. else
  2543. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2544. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2545. {
  2546. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2547. EmcOccureThenStopCharging();
  2548. }
  2549. else
  2550. ReleaseEmsOccureByString(0, "012252");
  2551. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2552. {
  2553. EmcOccureThenStopCharging();
  2554. }
  2555. else
  2556. ReleaseEmsOccureByString(0, "012267");
  2557. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS &&
  2558. ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS)
  2559. ShmSysConfigAndInfo->SysConfig.ShowInformation = YES;
  2560. else
  2561. ShmSysConfigAndInfo->SysConfig.ShowInformation = NO;
  2562. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  2563. {
  2564. if(!leftBtnPush)
  2565. {
  2566. leftBtnPush = true;
  2567. PRINTF_FUNC("left btn down............................... \n");
  2568. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2569. {
  2570. switch(ac_chargingInfo[0]->SystemStatus)
  2571. {
  2572. case SYS_MODE_IDLE:
  2573. case SYS_MODE_MAINTAIN:
  2574. case SYS_MODE_RESERVATION:
  2575. {
  2576. if(isDetectPlugin())
  2577. {
  2578. _DetectPlugInTimeout();
  2579. StopSystemTimeoutDet();
  2580. }
  2581. }
  2582. break;
  2583. case SYS_MODE_MODE_REASSIGN_CHECK:
  2584. case SYS_MODE_REASSIGN:
  2585. case SYS_MODE_PREPARING:
  2586. case SYS_MODE_PREPARE_FOR_EV:
  2587. case SYS_MODE_PREPARE_FOR_EVSE:
  2588. case SYS_MODE_CCS_PRECHARGE_STEP0:
  2589. case SYS_MODE_CCS_PRECHARGE_STEP1:
  2590. {
  2591. // 取消充電
  2592. AcChargingTerminalProcess();
  2593. }
  2594. break;
  2595. case SYS_MODE_CHARGING:
  2596. {
  2597. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  2598. {
  2599. // 停止充電
  2600. AcChargingTerminalProcess();
  2601. }
  2602. }
  2603. break;
  2604. case SYS_MODE_COMPLETE:
  2605. {}
  2606. break;
  2607. }
  2608. }
  2609. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2610. {
  2611. case SYS_MODE_IDLE:
  2612. case SYS_MODE_MAINTAIN:
  2613. case SYS_MODE_RESERVATION:
  2614. {
  2615. if(isDetectPlugin())
  2616. {
  2617. _DetectPlugInTimeout();
  2618. StopSystemTimeoutDet();
  2619. }
  2620. }
  2621. break;
  2622. case SYS_MODE_MODE_REASSIGN_CHECK:
  2623. case SYS_MODE_REASSIGN:
  2624. case SYS_MODE_PREPARING:
  2625. case SYS_MODE_PREPARE_FOR_EV:
  2626. case SYS_MODE_PREPARE_FOR_EVSE:
  2627. case SYS_MODE_CCS_PRECHARGE_STEP0:
  2628. case SYS_MODE_CCS_PRECHARGE_STEP1:
  2629. {
  2630. // 按鈕 - 取消充電
  2631. // if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2632. // AcChargingTerminalProcess();
  2633. // else
  2634. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2635. }
  2636. break;
  2637. case SYS_MODE_CHARGING:
  2638. {
  2639. if (ShmSysConfigAndInfo->SysConfig.StopChargingByButton == YES ||
  2640. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  2641. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->isRemoteStart == NO))
  2642. {
  2643. // 按鈕 - 停止充電
  2644. NormalStop(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2645. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2646. }
  2647. }
  2648. break;
  2649. case SYS_MODE_COMPLETE:
  2650. {
  2651. // 回 IDLE
  2652. //PRINTF_FUNC("right btn down.................SYS_MODE_COMPLETE \n");
  2653. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = SYS_MODE_IDLE;
  2654. }
  2655. break;
  2656. }
  2657. }
  2658. }
  2659. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  2660. {
  2661. if(leftBtnPush)
  2662. {
  2663. leftBtnPush = false;
  2664. PRINTF_FUNC("left btn up............................... \n");
  2665. }
  2666. }
  2667. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  2668. {
  2669. if(!rightBtnPush)
  2670. {
  2671. rightBtnPush = true;
  2672. PRINTF_FUNC("right btn down .................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2673. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2674. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  2675. {
  2676. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2677. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2678. }
  2679. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2680. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)
  2681. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2682. else if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2683. {
  2684. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  2685. {
  2686. if (chargingInfo[_index]->SystemStatus != SYS_MODE_BOOTING &&
  2687. chargingInfo[_index]->SystemStatus != SYS_MODE_IDLE &&
  2688. chargingInfo[_index]->SystemStatus != SYS_MODE_RESERVATION)
  2689. {
  2690. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  2691. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2692. return;
  2693. }
  2694. }
  2695. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2696. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2697. }
  2698. else
  2699. {
  2700. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2701. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2702. }
  2703. }
  2704. }
  2705. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  2706. {
  2707. if(rightBtnPush)
  2708. {
  2709. rightBtnPush = false;
  2710. PRINTF_FUNC("right btn up............................... \n");
  2711. }
  2712. }
  2713. }
  2714. //===============================================
  2715. // 確認各小板偵測的錯誤狀況
  2716. //===============================================
  2717. void CheckSystemErrorFunction(byte index)
  2718. {
  2719. // System
  2720. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  2721. RecordAlarmCode(index, "012251");
  2722. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip)
  2723. RecordAlarmCode(index, "012238");
  2724. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2725. RecordAlarmCode(index, "012252");
  2726. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2727. RecordAlarmCode(index, "012267");
  2728. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2729. {
  2730. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2731. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2732. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2733. {
  2734. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2735. {
  2736. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2737. }
  2738. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2739. RecordAlarmCode(index, "012203");
  2740. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2741. RecordAlarmCode(index, "012204");
  2742. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2743. RecordAlarmCode(index, "012205");
  2744. chargingInfo[index]->StopChargeFlag = YES;
  2745. }
  2746. else
  2747. {
  2748. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2749. {
  2750. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2751. }
  2752. }
  2753. }
  2754. else
  2755. {
  2756. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2757. {
  2758. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2759. }
  2760. }
  2761. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2762. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2763. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2764. {
  2765. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2766. {
  2767. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2768. }
  2769. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2770. RecordAlarmCode(index, "012200");
  2771. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2772. RecordAlarmCode(index, "012201");
  2773. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2774. RecordAlarmCode(index, "012202");
  2775. chargingInfo[index]->StopChargeFlag = YES;
  2776. }
  2777. else
  2778. {
  2779. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2780. {
  2781. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2782. }
  2783. }
  2784. }
  2785. //===============================================
  2786. // 確認 GPIO 狀態
  2787. //===============================================
  2788. void gpio_set_value(unsigned int gpio, unsigned int value)
  2789. {
  2790. int fd;
  2791. char buf[MAX_BUF];
  2792. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2793. fd = open(buf, O_WRONLY);
  2794. if (fd < 0)
  2795. {
  2796. perror("gpio/set-value");
  2797. return;
  2798. }
  2799. if (value)
  2800. write(fd, "1", 2);
  2801. else
  2802. write(fd, "0", 2);
  2803. close(fd);
  2804. }
  2805. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2806. {
  2807. int fd;
  2808. char buf[MAX_BUF];
  2809. char ch;
  2810. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2811. fd = open(buf, O_RDONLY);
  2812. if (fd < 0) {
  2813. perror("gpio/get-value");
  2814. return fd;
  2815. }
  2816. read(fd, &ch, 1);
  2817. if (ch != '0') {
  2818. *value = 1;
  2819. } else {
  2820. *value = 0;
  2821. }
  2822. close(fd);
  2823. return 0;
  2824. }
  2825. void CheckGunTypeFromHw()
  2826. {
  2827. int pinIn[4] = { 22, 23, 44, 45 };
  2828. unsigned int gpioValue = 0;
  2829. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  2830. gpio_get_value(pinIn[i], &gpioValue);
  2831. {
  2832. switch (pinIn[i])
  2833. {
  2834. case 22:
  2835. bd1_1_status = gpioValue;
  2836. break;
  2837. case 23:
  2838. bd1_2_status = gpioValue;
  2839. break;
  2840. case 44:
  2841. bd0_1_status = gpioValue;
  2842. break;
  2843. case 45:
  2844. bd0_2_status = gpioValue;
  2845. break;
  2846. }
  2847. }
  2848. }
  2849. }
  2850. void CheckGpioInStatus()
  2851. {
  2852. int pinIn[2] = { 27, 47 };
  2853. unsigned int gpioValue = 0;
  2854. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  2855. {
  2856. gpio_get_value(pinIn[i], &gpioValue);
  2857. if (gpioValue == 0x01)
  2858. {
  2859. switch(pinIn[i])
  2860. {
  2861. // 小板緊急停止
  2862. case 47:
  2863. {
  2864. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2865. {
  2866. if (chargingInfo[i]->slotsIndex == 1)
  2867. {
  2868. if (chargingInfo[i]->Type == _Type_Chademo)
  2869. BoardErrOccurByString(i, "023730");
  2870. // else if (chargingInfo[i]->Type == _Type_CCS)
  2871. // BoardErrOccurByString(i, "013627");
  2872. break;
  2873. }
  2874. }
  2875. }
  2876. break;
  2877. case 27:
  2878. {
  2879. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2880. {
  2881. if (chargingInfo[i]->slotsIndex == 3)
  2882. {
  2883. if (chargingInfo[i]->Type == _Type_Chademo)
  2884. BoardErrOccurByString(i, "023730");
  2885. // else if (chargingInfo[i]->Type == _Type_CCS)
  2886. // BoardErrOccurByString(i, "013627");
  2887. break;
  2888. }
  2889. }
  2890. }
  2891. break;
  2892. }
  2893. }
  2894. else
  2895. {
  2896. switch (pinIn[i])
  2897. {
  2898. // 小板解除緊急停止
  2899. case 47:
  2900. {
  2901. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2902. {
  2903. if (chargingInfo[i]->slotsIndex == 1)
  2904. {
  2905. if (chargingInfo[i]->Type == _Type_Chademo)
  2906. ReleaseBoardErrOccurByString(i, "023730");
  2907. // else if (chargingInfo[i]->Type == _Type_CCS)
  2908. // ReleaseBoardErrOccurByString(i, "013627");
  2909. break;
  2910. }
  2911. }
  2912. }
  2913. break;
  2914. case 27:
  2915. {
  2916. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2917. {
  2918. if (chargingInfo[i]->slotsIndex == 3)
  2919. {
  2920. if (chargingInfo[i]->Type == _Type_Chademo)
  2921. ReleaseBoardErrOccurByString(i, "023730");
  2922. // else if (chargingInfo[i]->Type == _Type_CCS)
  2923. // ReleaseBoardErrOccurByString(i, "013627");
  2924. break;
  2925. }
  2926. }
  2927. }
  2928. break;
  2929. }
  2930. }
  2931. }
  2932. }
  2933. //===============================================
  2934. // Main process
  2935. //===============================================
  2936. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  2937. {
  2938. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  2939. {
  2940. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  2941. {
  2942. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  2943. return true;
  2944. }
  2945. }
  2946. for (byte index = 0; index < CCS_QUANTITY; index++)
  2947. {
  2948. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  2949. {
  2950. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  2951. return true;
  2952. }
  2953. }
  2954. for (byte index = 0; index < GB_QUANTITY; index++)
  2955. {
  2956. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  2957. {
  2958. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  2959. return true;
  2960. }
  2961. }
  2962. return false;
  2963. }
  2964. // 檢查 Byte 中某個 Bit 的值
  2965. // _byte : 欲改變的 byte
  2966. // _bit : 該 byte 的第幾個 bit
  2967. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  2968. {
  2969. return ( _byte & mask_table[_bit] ) != 0x00;
  2970. }
  2971. // 設定 Byte 中某個 Bit的值
  2972. // _byte : 欲改變的 byte
  2973. // _bit : 該 byte 的第幾個 bit
  2974. // value : 修改的值為 0 or 1
  2975. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  2976. {
  2977. if(value == 1)
  2978. *_byte |= (1 << _bit);
  2979. else if (value == 0)
  2980. *_byte ^= (1 << _bit);
  2981. }
  2982. void UserScanFunction()
  2983. {
  2984. bool idleReq = false;
  2985. unsigned char idleIndex = 255;
  2986. //unsigned char stopReq = 255;
  2987. bool hasGunUsing = false;
  2988. bool isReservedCard = false;
  2989. // 當前非驗證的狀態
  2990. if(!IsAuthorizingMode())
  2991. {
  2992. // 先判斷現在是否可以提供刷卡
  2993. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  2994. // 2. 停止充電
  2995. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX ||
  2996. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_EMC)
  2997. {
  2998. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2999. return;
  3000. }
  3001. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  3002. {
  3003. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  3004. {
  3005. // 如果選 AC 槍
  3006. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  3007. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_ALARM)
  3008. {
  3009. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  3010. {
  3011. char value[32];
  3012. PRINTF_FUNC("ac stop charging \n");
  3013. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  3014. ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  3015. memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId, ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  3016. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  3017. {
  3018. AcChargingTerminalProcess();
  3019. }
  3020. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3021. }
  3022. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE)
  3023. {
  3024. idleReq = true;
  3025. }
  3026. }
  3027. }
  3028. else
  3029. {
  3030. if (IsGunUsing(ShmSysConfigAndInfo->SysInfo.CurGunSelected))
  3031. {
  3032. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_CHARGING)
  3033. {
  3034. char value[32];
  3035. PRINTF_FUNC("stop charging \n");
  3036. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  3037. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  3038. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  3039. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  3040. // 同一張卡直接停掉
  3041. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  3042. {
  3043. NormalStop(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3044. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3045. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3046. }
  3047. else
  3048. {
  3049. PRINTF_FUNC("_LCM_AUTHORIZ_FAIL \n");
  3050. stopChargingChkByCard = true;
  3051. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3052. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3053. }
  3054. }
  3055. }
  3056. // else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE ||
  3057. // chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  3058. // {
  3059. // idleIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  3060. // idleReq = true;
  3061. // }
  3062. else
  3063. {
  3064. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3065. {
  3066. // if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != gun_index &&
  3067. // (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE))
  3068. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  3069. {
  3070. idleIndex = gun_index;
  3071. idleReq = true;
  3072. }
  3073. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3074. {
  3075. idleReq = true;
  3076. }
  3077. else
  3078. {
  3079. hasGunUsing = IsGunUsing(gun_index);
  3080. }
  3081. }
  3082. // 壁掛不給充 - 已經有至少一把槍在充電
  3083. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES &&
  3084. hasGunUsing)
  3085. {
  3086. PRINTF_FUNC("DW Serial not support dual charging. \n");
  3087. idleReq = false;
  3088. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3089. }
  3090. }
  3091. }
  3092. if (idleReq)
  3093. {
  3094. // if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  3095. // stopReq != 255 &&
  3096. // ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  3097. // {
  3098. // idleReq = false;
  3099. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3100. // }
  3101. // else
  3102. {
  3103. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3104. {
  3105. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  3106. {
  3107. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId,
  3108. (char *)ShmDcCommonData->_reserved_UserId[gun_index]) == EQUAL)
  3109. {
  3110. isReservedCard = true;
  3111. // 驗證通過
  3112. chargingInfo[gun_index]->ReservedStartFlag = YES;
  3113. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3114. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3115. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3116. break;
  3117. }
  3118. }
  3119. }
  3120. if (!isReservedCard &&
  3121. ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  3122. (idleIndex != 255 && chargingInfo[idleIndex]->SystemStatus == SYS_MODE_IDLE)))
  3123. {
  3124. PRINTF_FUNC("Start Authorizing... \n");
  3125. // LCM => Authorizing
  3126. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  3127. // 進入確認卡號狀態
  3128. AuthorizingStart();
  3129. }
  3130. else
  3131. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3132. }
  3133. }
  3134. else
  3135. {
  3136. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3137. }
  3138. }
  3139. }
  3140. }
  3141. unsigned char isModeChange(unsigned char gun_index)
  3142. {
  3143. unsigned char result = NO;
  3144. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  3145. {
  3146. result = YES;
  3147. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  3148. }
  3149. return result;
  3150. }
  3151. void ScannerCardProcess()
  3152. {
  3153. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL &&
  3154. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  3155. {
  3156. isCardScan = true;
  3157. // 處理刷卡及驗證卡號的動作
  3158. UserScanFunction();
  3159. }
  3160. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  3161. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3162. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  3163. {
  3164. StartSystemTimeoutDet(Timeout_Authorizing);
  3165. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3166. {
  3167. // 白名單驗證
  3168. for (int i = 0; i < 10; i++)
  3169. {
  3170. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  3171. {
  3172. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  3173. {
  3174. PRINTF_FUNC("White Card Pass (%s)... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3175. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3176. ClearAuthorizedFlag();
  3177. break;
  3178. }
  3179. }
  3180. }
  3181. }
  3182. // 確認驗證卡號完成沒
  3183. if (isAuthorizedComplete() || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3184. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3185. {
  3186. StopSystemTimeoutDet();
  3187. // 判斷後台回覆狀態
  3188. if(ocpp_chk_authrization_cmd() || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  3189. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  3190. {
  3191. // LCM => Authorize complete
  3192. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  3193. }
  3194. else
  3195. {
  3196. // LCM => Authorize fail
  3197. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  3198. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3199. }
  3200. ClearAuthorizedFlag();
  3201. }
  3202. }
  3203. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  3204. {
  3205. StartSystemTimeoutDet(Timeout_VerifyFail);
  3206. isCardScan = false;
  3207. }
  3208. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  3209. {
  3210. StartSystemTimeoutDet(Timeout_VerifyComp);
  3211. }
  3212. else if(ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  3213. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  3214. {
  3215. StartSystemTimeoutDet(Timeout_WaitPlug);
  3216. }
  3217. else
  3218. {
  3219. isCardScan = false;
  3220. }
  3221. }
  3222. bool AddGunInfoByConnector(byte typeValue, byte slots)
  3223. {
  3224. bool result = true;
  3225. switch (typeValue)
  3226. {
  3227. case '0': // none
  3228. break;
  3229. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  3230. break;
  3231. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  3232. break;
  3233. case '3': // IEC 62196-2 Type 2 Plug
  3234. case '4': // IEC 62196-2 Type 2 Socket
  3235. if (AC_QUANTITY > _ac_Index)
  3236. {
  3237. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  3238. // AC 固定 index
  3239. ac_chargingInfo[_acgunIndex]->Index = 0;
  3240. ac_chargingInfo[_acgunIndex]->ReservationId = -1;
  3241. ac_chargingInfo[_acgunIndex]->SystemStatus = SYS_MODE_IDLE;
  3242. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  3243. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  3244. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3245. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3246. _ac_Index++;
  3247. _acgunIndex++;
  3248. }
  3249. else
  3250. result = false;
  3251. break;
  3252. case '5': // GB/T AC Plug
  3253. break;
  3254. case '6': // GB/T AC Socket
  3255. break;
  3256. case 'J': // CHAdeMO
  3257. case 'K': // CHAdeMO - 200A / 500V
  3258. {
  3259. if (CHAdeMO_QUANTITY > _chademoIndex)
  3260. {
  3261. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  3262. chargingInfo[_gunIndex]->Index = _gunIndex;
  3263. chargingInfo[_gunIndex]->ReservationId = -1;
  3264. chargingInfo[_gunIndex]->slotsIndex = slots;
  3265. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3266. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  3267. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  3268. chargingInfo[_gunIndex]->IsAvailable = YES;
  3269. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3270. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3271. chargingInfo[_gunIndex]->ModelType = typeValue;
  3272. _chademoIndex++;
  3273. _gunIndex++;
  3274. }
  3275. else
  3276. result = false;
  3277. }
  3278. break;
  3279. case 'U': // Natural cooling CCS1
  3280. case 'E': // Natural cooling CCS2
  3281. case 'T': // Rema CCS1 - 300A
  3282. case 'D': // Rema CCS2 - 300A
  3283. case 'M': // CCS2 - 80A
  3284. case 'N': // CCS1 - 80A
  3285. {
  3286. if (CCS_QUANTITY > _ccsIndex)
  3287. {
  3288. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  3289. chargingInfo[_gunIndex]->Index = _gunIndex;
  3290. chargingInfo[_gunIndex]->ReservationId = -1;
  3291. chargingInfo[_gunIndex]->slotsIndex = slots;
  3292. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3293. chargingInfo[_gunIndex]->Type = _Type_CCS;
  3294. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  3295. chargingInfo[_gunIndex]->IsAvailable = YES;
  3296. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3297. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3298. chargingInfo[_gunIndex]->ModelType = typeValue;
  3299. // 現階段預設為走 DIN70121
  3300. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  3301. if (typeValue == 'U' || typeValue == 'T' || typeValue == 'N')
  3302. ShmDcCommonData->CcsTypeSaved[_gunIndex] = _CCS_TYPE_CCS1;
  3303. else if (typeValue == 'E' || typeValue == 'D' || typeValue == 'M')
  3304. ShmDcCommonData->CcsTypeSaved[_gunIndex] = _CCS_TYPE_CCS2;
  3305. _ccsIndex++;
  3306. _gunIndex++;
  3307. }
  3308. else
  3309. result = false;
  3310. }
  3311. break;
  3312. case 'G': // GBT DC
  3313. case 'B':
  3314. {
  3315. if (GB_QUANTITY > _gb_Index)
  3316. {
  3317. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  3318. chargingInfo[_gunIndex]->Index = _gunIndex;
  3319. chargingInfo[_gunIndex]->ReservationId = -1;
  3320. chargingInfo[_gunIndex]->slotsIndex = slots;
  3321. chargingInfo[_gunIndex]->SystemStatus = SYS_MODE_BOOTING;
  3322. chargingInfo[_gunIndex]->Type = _Type_GB;
  3323. chargingInfo[_gunIndex]->type_index = _gb_Index;
  3324. chargingInfo[_gunIndex]->IsAvailable = YES;
  3325. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;
  3326. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO;
  3327. chargingInfo[_gunIndex]->ModelType = typeValue;
  3328. _gb_Index++;
  3329. _gunIndex++;
  3330. }
  3331. else
  3332. result = false;
  3333. }
  3334. break;
  3335. }
  3336. return result;
  3337. }
  3338. bool CheckConnectorTypeStatus()
  3339. {
  3340. bool result = true;
  3341. // 硬體 0 1 => CCS,1 0 => Chademo,1 1 => GB
  3342. //CheckFwSlotSta.tusLog();
  3343. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  3344. {
  3345. byte slots = 1;
  3346. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  3347. {
  3348. if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  3349. {
  3350. return false;
  3351. }
  3352. slots++;
  3353. }
  3354. // AC index 接在 DC 後面
  3355. if (_ac_Index > 0)
  3356. ac_chargingInfo[0]->Index += _gunIndex;
  3357. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3358. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  3359. PRINTF_FUNC("Charger Count => DC-Count = %d, AC-Count = %d \n",
  3360. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  3361. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  3362. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  3363. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  3364. result = false;
  3365. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3366. {
  3367. bool isFind = false;
  3368. if (chargingInfo[0]->Type == _Type_Chademo)
  3369. {
  3370. if ((bd0_1_status == 0 && bd0_2_status == 1) ||
  3371. (bd1_1_status == 0 && bd1_2_status == 1))
  3372. {
  3373. isFind = true;
  3374. }
  3375. }
  3376. else if (chargingInfo[0]->Type == _Type_CCS)
  3377. {
  3378. if ((bd0_1_status == 1 && bd0_2_status == 0) ||
  3379. (bd1_1_status == 1 && bd1_2_status == 0))
  3380. {
  3381. isFind = true;
  3382. }
  3383. }
  3384. else if (chargingInfo[0]->Type == _Type_GB)
  3385. {
  3386. if ((bd0_1_status == 1 && bd0_2_status == 1) ||
  3387. (bd1_1_status == 1 && bd1_2_status == 1))
  3388. {
  3389. isFind = true;
  3390. }
  3391. }
  3392. if (isFind)
  3393. {
  3394. chargingInfo[0]->Evboard_id = 0x01;
  3395. CheckHwSlotStatusLog(0);
  3396. }
  3397. else
  3398. result = false;
  3399. }
  3400. else
  3401. {
  3402. char type = NO_DEFINE;
  3403. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  3404. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  3405. {
  3406. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  3407. {
  3408. PRINTF_FUNC("HW Conn %d (Chademo) \n", gunIndex);
  3409. // 與硬體相同 type : Chademo
  3410. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3411. {
  3412. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3413. }
  3414. }
  3415. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  3416. {
  3417. PRINTF_FUNC("HW Conn %d (CCS) \n", gunIndex);
  3418. // 與硬體相同 type : CCS
  3419. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  3420. {
  3421. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3422. }
  3423. }
  3424. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  3425. {
  3426. PRINTF_FUNC("HW Conn %d (GB) \n", gunIndex);
  3427. // 與硬體相同 type : GB
  3428. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3429. {
  3430. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3431. }
  3432. }
  3433. else if (gunIndex == 0)
  3434. {
  3435. PRINTF_FUNC("HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n",
  3436. gunIndex, bd0_1_status, bd0_2_status);
  3437. }
  3438. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  3439. {
  3440. PRINTF_FUNC("HW Conn %d (Chademo) \n", gunIndex);
  3441. // 與硬體相同 type : Chademo
  3442. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3443. {
  3444. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3445. }
  3446. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3447. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3448. }
  3449. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  3450. {
  3451. PRINTF_FUNC("HW Conn %d (CCS) \n", gunIndex);
  3452. // 與硬體相同 type : CCS
  3453. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  3454. {
  3455. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3456. }
  3457. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3458. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3459. }
  3460. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  3461. {
  3462. PRINTF_FUNC("HW Conn %d (GB) \n", gunIndex);
  3463. // 與硬體相同 type : GB
  3464. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3465. {
  3466. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3467. }
  3468. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3469. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3470. }
  3471. else if (gunIndex == 1)
  3472. {
  3473. PRINTF_FUNC("HW Conn fail, Gun_%d, Flag1 = %d, Flag2 = %d \n",
  3474. gunIndex, bd1_1_status, bd1_2_status);
  3475. }
  3476. CheckHwSlotStatusLog(gunIndex);
  3477. if (type == NO_DEFINE)
  3478. type = chargingInfo[gunIndex]->Type;
  3479. else
  3480. {
  3481. // 雙槍都是同一個 type
  3482. if (type == chargingInfo[gunIndex]->Type)
  3483. ShmDcCommonData->SysGunAreSameType = YES;
  3484. }
  3485. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  3486. result = false;
  3487. }
  3488. }
  3489. }
  3490. else
  3491. {
  3492. // Module Name 不正確 - 告警
  3493. result = false;
  3494. }
  3495. return result;
  3496. }
  3497. void KillTask()
  3498. {
  3499. ChangeLcmByIndex(_LCM_FIX);
  3500. system("killall Module_EventLogging");
  3501. system("killall Module_PrimaryComm");
  3502. system("killall Module_EvComm");
  3503. system("killall Module_InternalComm");
  3504. system("killall Module_PsuComm");
  3505. system("killall Module_4g &");
  3506. system("killall Module_Wifi &");
  3507. }
  3508. void KillTaskExceptDetectTask()
  3509. {
  3510. ChangeLcmByIndex(_LCM_FIX);
  3511. sleep(3);
  3512. system("killall Module_EvComm");
  3513. //system("killall Module_InternalComm");
  3514. system("killall Module_PsuComm");
  3515. system("killall Module_4g &");
  3516. system("killall Module_Wifi &");
  3517. }
  3518. void KillAllTask()
  3519. {
  3520. ChangeLcmByIndex(_LCM_FIX);
  3521. system("killall Module_EventLogging");
  3522. system("killall Module_PrimaryComm");
  3523. system("killall Module_EvComm");
  3524. system("killall Module_LcmControl");
  3525. system("killall Module_InternalComm");
  3526. system("killall Module_PsuComm");
  3527. system("killall OcppBackend &");
  3528. system("killall Module_4g &");
  3529. system("killall Module_Wifi &");
  3530. system("killall Module_DcMeter &");
  3531. }
  3532. void LogUpgradeType(unsigned int type)
  3533. {
  3534. switch (type)
  3535. {
  3536. case 0x10000001: PRINTF_FUNC("CSU bootloader (uboot) \n"); break;
  3537. case 0x10000002: PRINTF_FUNC("CSU kernel configuration (dtb) \n"); break;
  3538. case 0x10000003: PRINTF_FUNC("CSU kernel image (zImage) \n"); break;
  3539. case 0x10000004: PRINTF_FUNC("CSU root file system (ramdisk.gz) \n"); break;
  3540. case 0x10000005: PRINTF_FUNC("CSU user configuration (bin) \n"); break;
  3541. case 0x10000007: PRINTF_FUNC("CCS board bootloader (uboot) \n"); break;
  3542. case 0x10000008: PRINTF_FUNC("CCS board kernel configuration (dtb) \n"); break;
  3543. case 0x10000009: PRINTF_FUNC("CCS board kernel image (zImage) \n"); break;
  3544. case 0x1000000A: PRINTF_FUNC("CCS board file system (ramdisk.gz) \n"); break;
  3545. case 0x10000006: PRINTF_FUNC("DCM 407 primary controller \n"); break;
  3546. case 0x1000000D: PRINTF_FUNC("Relay control board \n"); break;
  3547. case 0x1000000E: PRINTF_FUNC("Fan control board \n"); break;
  3548. case 0x20000002: PRINTF_FUNC("AC wall-mount (low-end) controller \n"); break;
  3549. case 0x10000014: PRINTF_FUNC("LED control board \n"); break;
  3550. case 0x1000000B: PRINTF_FUNC("CAHdeMO board \n"); break;
  3551. case 0x1000000C: PRINTF_FUNC("GBT board \n"); break;
  3552. }
  3553. }
  3554. int CheckUpdateProcess(void)
  3555. {
  3556. //bool isPass = true;
  3557. uint8_t retSucc = 0;
  3558. uint8_t retFail = 0;
  3559. byte index = 0;
  3560. byte target = 0;
  3561. char new_str[256];
  3562. unsigned char *ptr = NULL;
  3563. int fd = 0;
  3564. int CanFd = 0;
  3565. int uartFd = 0;
  3566. unsigned int Type = 0;
  3567. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  3568. DIR *d;
  3569. struct dirent *dir;
  3570. byte result = PASS;
  3571. char Buf[256];
  3572. d = opendir("/mnt/");
  3573. if (d)
  3574. {
  3575. while ((dir = readdir(d)) != NULL)
  3576. {
  3577. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
  3578. {
  3579. continue;
  3580. }
  3581. memset(new_str, 0x00, sizeof(new_str));
  3582. strcat(new_str, "/mnt/");
  3583. strcat(new_str, dir->d_name);
  3584. PRINTF_FUNC("%s \n", new_str);
  3585. fd = open(new_str, O_RDONLY);
  3586. if (fd < 0)
  3587. {
  3588. close(fd);
  3589. result = FAIL;
  3590. sleep(1);
  3591. continue;
  3592. }
  3593. ptr = malloc(MaxLen); //-48 is take out the header
  3594. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  3595. //get the image length
  3596. ImageLen = read(fd, ptr, MaxLen);
  3597. bool isModelNameFail = false;
  3598. // DS-----J0E-2PH
  3599. if((ShmSysConfigAndInfo->SysConfig.ModelName[0] != ptr[0]) ||
  3600. (ShmSysConfigAndInfo->SysConfig.ModelName[1] != ptr[1]) ||
  3601. (ShmSysConfigAndInfo->SysConfig.ModelName[7] != ptr[7]) ||
  3602. (ShmSysConfigAndInfo->SysConfig.ModelName[8] != ptr[8]) ||
  3603. (ShmSysConfigAndInfo->SysConfig.ModelName[9] != ptr[9]) ||
  3604. (ShmSysConfigAndInfo->SysConfig.ModelName[11] != ptr[11]) ||
  3605. (ShmSysConfigAndInfo->SysConfig.ModelName[12] != ptr[12]) ||
  3606. (ShmSysConfigAndInfo->SysConfig.ModelName[13] != ptr[13]))
  3607. {
  3608. result = MODELNAME_FAIL;
  3609. isModelNameFail = true;
  3610. }
  3611. if (isModelNameFail)
  3612. {
  3613. close(fd);
  3614. continue;
  3615. }
  3616. PRINTF_FUNC("model name check pass. \n");
  3617. if (ImageLen > 20) {
  3618. Type = (((unsigned int)ptr[16]) << 24 |
  3619. ((unsigned int)ptr[17]) << 16 |
  3620. ((unsigned int)ptr[18]) << 8 |
  3621. ((unsigned int)ptr[19]));
  3622. LogUpgradeType(Type);
  3623. PRINTF_FUNC("Upgrade Status : PASS : %d, FAIL : %d \n", retSucc, retFail);
  3624. free(ptr);
  3625. switch (Type)
  3626. {
  3627. case 0x10000001:
  3628. case 0x10000002:
  3629. case 0x10000003:
  3630. case 0x10000004:
  3631. case 0x10000005:
  3632. system("echo 3 > /proc/sys/vm/drop_caches");
  3633. sleep(2);
  3634. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3635. retSucc++;
  3636. else
  3637. {
  3638. PRINTF_FUNC("Upgrade CSU Failed (%d) \n", Type);
  3639. retFail++;
  3640. }
  3641. memset(Buf, 0, sizeof(Buf));
  3642. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  3643. system(Buf);
  3644. break;
  3645. case 0x10000007:
  3646. case 0x10000008:
  3647. case 0x10000009:
  3648. case 0x1000000A:
  3649. CanFd = InitCanBus();
  3650. if (CanFd > 0)
  3651. {
  3652. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3653. {
  3654. if (chargingInfo[index]->Type == _Type_CCS)
  3655. {
  3656. byte targetID = chargingInfo[index]->Evboard_id;
  3657. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1 &&
  3658. ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0)
  3659. {
  3660. targetID += 1;
  3661. }
  3662. system("echo 3 > /proc/sys/vm/drop_caches");
  3663. sleep(2);
  3664. PRINTF_FUNC("Upgrade CCS Processing..target id = %d \n", targetID);
  3665. if (Upgrade_CCS(CanFd, Type, targetID, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL)
  3666. {
  3667. PRINTF_FUNC("Upgrade CCS Failed \n");
  3668. retFail++;
  3669. }
  3670. else
  3671. retSucc++;
  3672. }
  3673. }
  3674. close(CanFd);
  3675. }
  3676. memset(Buf, 0, sizeof(Buf));
  3677. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  3678. system(Buf);
  3679. break;
  3680. case 0x10000006:
  3681. case 0x1000000D:
  3682. case 0x1000000E:
  3683. case 0x20000002:
  3684. case 0x10000014:
  3685. // CSU_PRIMARY_CONTROLLER : 0x10000006
  3686. target = 0x00;
  3687. if (Type == 0x10000006) {
  3688. target = UPGRADE_PRI;
  3689. } else if (Type == 0x1000000D) {
  3690. target = UPGRADE_RB;
  3691. } else if (Type == 0x1000000E) {
  3692. target = UPGRADE_FAN;
  3693. } else if (Type == 0x20000002) {
  3694. target = UPGRADE_AC;
  3695. } else if (Type == 0x10000014) {
  3696. target = UPGRADE_LED;
  3697. }
  3698. uartFd = InitComPort(target);
  3699. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3700. {
  3701. retSucc++;
  3702. }
  3703. else
  3704. {
  3705. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  3706. retFail++;
  3707. }
  3708. if (uartFd > 0)
  3709. {
  3710. close(uartFd);
  3711. }
  3712. memset(Buf, 0, sizeof(Buf));
  3713. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  3714. system(Buf);
  3715. break;
  3716. case 0x1000000B:
  3717. case 0x1000000C:
  3718. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  3719. CanFd = InitCanBus();
  3720. if (CanFd > 0)
  3721. {
  3722. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3723. {
  3724. if ((Type == 0x1000000B && chargingInfo[index]->Type == _Type_Chademo) ||
  3725. (Type == 0x1000000C && chargingInfo[index]->Type == _Type_GB))
  3726. {
  3727. PRINTF_FUNC("Upgrade GBT / Chademo Processing..target id = %d \n", chargingInfo[index]->Evboard_id);
  3728. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3729. retSucc++;
  3730. else
  3731. {
  3732. if (chargingInfo[index]->Type == _Type_Chademo)
  3733. PRINTF_FUNC("Upgrade Chademo Failed \n");
  3734. else
  3735. PRINTF_FUNC("Upgrade GBT Failed \n");
  3736. retFail++;
  3737. }
  3738. }
  3739. }
  3740. close(CanFd);
  3741. }
  3742. memset(Buf, 0, sizeof(Buf));
  3743. sprintf(Buf, "rm -rvf /mnt/%s", new_str);
  3744. system(Buf);
  3745. break;
  3746. }
  3747. }
  3748. close(fd);
  3749. }
  3750. }
  3751. free(dir);
  3752. closedir(d);
  3753. PRINTF_FUNC("Upgrade Result : PASS : %d, FAIL : %d \n", retSucc, retFail);
  3754. sleep(1);
  3755. system("rm -rvf /mnt/* ");
  3756. if (retFail != 0)
  3757. {
  3758. result = FAIL;
  3759. }
  3760. return result;
  3761. }
  3762. void CreateRfidFork()
  3763. {
  3764. pid_t rfidRecPid;
  3765. rfidRecPid = fork();
  3766. if (rfidRecPid == 0)
  3767. {
  3768. while(true)
  3769. {
  3770. // 刷卡判斷
  3771. RFID rfid;
  3772. if (!ShmSysConfigAndInfo->SysConfig.isRFID)
  3773. {}
  3774. else if(getRequestCardSN(rfidFd, 0, &rfid))
  3775. {
  3776. //PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3777. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  3778. {
  3779. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  3780. {
  3781. switch (rfid.snType)
  3782. {
  3783. case RFID_SN_TYPE_6BYTE:
  3784. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3785. "%02X%02X%02X%02X%02X%02X",
  3786. rfid.currentCard[0], rfid.currentCard[1],
  3787. rfid.currentCard[2], rfid.currentCard[3],
  3788. rfid.currentCard[4], rfid.currentCard[5]);
  3789. break;
  3790. case RFID_SN_TYPE_7BYTE:
  3791. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3792. "%02X%02X%02X%02X%02X%02X%02X",
  3793. rfid.currentCard[0], rfid.currentCard[1],
  3794. rfid.currentCard[2], rfid.currentCard[3],
  3795. rfid.currentCard[4], rfid.currentCard[5],
  3796. rfid.currentCard[6]);
  3797. break;
  3798. case RFID_SN_TYPE_10BYTE:
  3799. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3800. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3801. rfid.currentCard[0], rfid.currentCard[1],
  3802. rfid.currentCard[2], rfid.currentCard[3],
  3803. rfid.currentCard[4], rfid.currentCard[5],
  3804. rfid.currentCard[6], rfid.currentCard[7],
  3805. rfid.currentCard[8], rfid.currentCard[9]);
  3806. break;
  3807. case RFID_SN_TYPE_4BYTE:
  3808. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3809. "%02X%02X%02X%02X",
  3810. rfid.currentCard[0], rfid.currentCard[1],
  3811. rfid.currentCard[2], rfid.currentCard[3]);
  3812. break;
  3813. }
  3814. }
  3815. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  3816. {
  3817. switch (rfid.snType)
  3818. {
  3819. case RFID_SN_TYPE_6BYTE:
  3820. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3821. "%02X%02X%02X%02X%02X%02X",
  3822. rfid.currentCard[5], rfid.currentCard[4],
  3823. rfid.currentCard[3], rfid.currentCard[2],
  3824. rfid.currentCard[1], rfid.currentCard[0]);
  3825. break;
  3826. case RFID_SN_TYPE_7BYTE:
  3827. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3828. "%02X%02X%02X%02X%02X%02X%02X",
  3829. rfid.currentCard[6], rfid.currentCard[5],
  3830. rfid.currentCard[4], rfid.currentCard[3],
  3831. rfid.currentCard[2], rfid.currentCard[1],
  3832. rfid.currentCard[0]);
  3833. break;
  3834. case RFID_SN_TYPE_10BYTE:
  3835. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3836. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3837. rfid.currentCard[9], rfid.currentCard[8],
  3838. rfid.currentCard[7], rfid.currentCard[6],
  3839. rfid.currentCard[5], rfid.currentCard[4],
  3840. rfid.currentCard[3], rfid.currentCard[2],
  3841. rfid.currentCard[1], rfid.currentCard[0]);
  3842. break;
  3843. case RFID_SN_TYPE_4BYTE:
  3844. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3845. "%02X%02X%02X%02X",
  3846. rfid.currentCard[3], rfid.currentCard[2],
  3847. rfid.currentCard[1], rfid.currentCard[0]);
  3848. break;
  3849. }
  3850. }
  3851. PRINTF_FUNC("RFID : Get card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3852. }
  3853. }
  3854. sleep(1);
  3855. }
  3856. }
  3857. }
  3858. void CreateCheckSystemTaskFork()
  3859. {
  3860. pid_t taskPid;
  3861. taskPid = fork();
  3862. if (taskPid == 0)
  3863. {
  3864. bool stopToDet = false;
  3865. while(true)
  3866. {
  3867. if (!stopToDet)
  3868. {
  3869. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  3870. {
  3871. if (chargingInfo[_index]->SystemStatus == SYS_MODE_UPDATE ||
  3872. chargingInfo[_index]->Type == 0x09)
  3873. {
  3874. stopToDet = true;
  3875. continue;
  3876. }
  3877. }
  3878. if (!stopToDet)
  3879. CheckSystemTaskAlive();
  3880. }
  3881. sleep(5);
  3882. }
  3883. }
  3884. }
  3885. void StartSystemTimeoutDet(unsigned char flag)
  3886. {
  3887. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  3888. {
  3889. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  3890. }
  3891. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  3892. }
  3893. void StopSystemTimeoutDet()
  3894. {
  3895. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  3896. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  3897. }
  3898. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  3899. {
  3900. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3901. {
  3902. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  3903. {
  3904. GetTimespecFunc(&chargingInfo[gunIndex]->ConnectorTimeout);
  3905. }
  3906. chargingInfo[gunIndex]->TimeoutFlag = flag;
  3907. }
  3908. }
  3909. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  3910. {
  3911. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3912. {
  3913. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  3914. }
  3915. }
  3916. void CheckConnectionTimeout()
  3917. {
  3918. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3919. {
  3920. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3921. }
  3922. else
  3923. {
  3924. _connectionTimeout = _ocpp_get_connect_timeout();
  3925. if(_connectionTimeout <= 0)
  3926. {
  3927. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3928. }
  3929. }
  3930. }
  3931. void CreateTimeoutFork()
  3932. {
  3933. pid_t timeoutPid;
  3934. timeoutPid = fork();
  3935. if (timeoutPid > 0)
  3936. {
  3937. GetTimespecFunc(&_cmdSubPriority_time);
  3938. CheckConnectionTimeout();
  3939. int _timebuf = 0;
  3940. while(true)
  3941. {
  3942. _timebuf = GetTimeoutValue(&_cmdSubPriority_time);
  3943. if (_timebuf < 0)
  3944. {
  3945. GetTimespecFunc(&_cmdSubPriority_time);
  3946. }
  3947. else
  3948. {
  3949. if (_timebuf >= 5)
  3950. {
  3951. CheckConnectionTimeout();
  3952. GetTimespecFunc(&_cmdSubPriority_time);
  3953. }
  3954. }
  3955. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  3956. // 系統
  3957. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_None)
  3958. {
  3959. _timebuf = GetTimeoutValue(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  3960. if (_timebuf < 0)
  3961. {
  3962. GetTimespecFunc(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer);
  3963. }
  3964. else
  3965. {
  3966. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  3967. {
  3968. case Timeout_SelftestChk:
  3969. if (_timebuf >= SELFTEST_TIMEOUT)
  3970. {
  3971. _SelfTestTimeout();
  3972. StopSystemTimeoutDet();
  3973. }
  3974. break;
  3975. case Timeout_Authorizing:
  3976. if (_timebuf >= AUTHORIZE_TIMEOUT)
  3977. {
  3978. _AuthorizedTimeout();
  3979. StopSystemTimeoutDet();
  3980. }
  3981. break;
  3982. case Timeout_VerifyFail:
  3983. if (_timebuf >= AUTHORIZE_FAIL_TIMEOUT)
  3984. {
  3985. _AutoReturnTimeout();
  3986. StopSystemTimeoutDet();
  3987. }
  3988. break;
  3989. case Timeout_VerifyComp:
  3990. if (_timebuf >= AUTHORIZE_COMP_TIMEOUT)
  3991. {
  3992. _AutoReturnTimeout();
  3993. StopSystemTimeoutDet();
  3994. }
  3995. break;
  3996. case Timeout_WaitPlug:
  3997. if (_timebuf >= _connectionTimeout)
  3998. {
  3999. _DetectPlugInTimeout();
  4000. StopSystemTimeoutDet();
  4001. }
  4002. break;
  4003. case Timeout_ReturnToChargingGunDet:
  4004. {
  4005. if (_timebuf >= RETURN_TO_CHARGING_PAGE)
  4006. {
  4007. DisplayChargingInfo();
  4008. StopSystemTimeoutDet();
  4009. }
  4010. }
  4011. break;
  4012. // case Timeout_AuthorizingForStop:
  4013. // {
  4014. // if (_timebuf / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT)
  4015. // {
  4016. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  4017. // ClearAuthorizedFlag();
  4018. // StopSystemTimeoutDet();
  4019. // }
  4020. // }
  4021. // break;
  4022. }
  4023. }
  4024. }
  4025. // 各槍
  4026. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4027. {
  4028. if (chargingInfo[gun_index]->TimeoutFlag != Timeout_None)
  4029. {
  4030. _timebuf = GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout);
  4031. if (_timebuf < 0)
  4032. {
  4033. GetTimespecFunc(&chargingInfo[gun_index]->ConnectorTimeout);
  4034. }
  4035. else
  4036. {
  4037. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  4038. switch(chargingInfo[gun_index]->TimeoutFlag)
  4039. {
  4040. case Timeout_Preparing:
  4041. {
  4042. if (_timebuf >= GUN_PREPARE_TIMEOUT)
  4043. {
  4044. _PrepareTimeout(gun_index);
  4045. StopGunInfoTimeoutDet(gun_index);
  4046. }
  4047. }
  4048. break;
  4049. case Timeout_EvChargingDet:
  4050. {
  4051. if (_timebuf >= GUN_EV_WAIT_TIMEOUT)
  4052. {
  4053. _DetectEvChargingEnableTimeout(gun_index);
  4054. StopGunInfoTimeoutDet(gun_index);
  4055. }
  4056. }
  4057. break;
  4058. case Timeout_EvseChargingDet:
  4059. {
  4060. if (chargingInfo[gun_index]->Type == _Type_GB)
  4061. {
  4062. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT * 3)
  4063. {
  4064. _DetectEvseChargingEnableTimeout(gun_index);
  4065. StopGunInfoTimeoutDet(gun_index);
  4066. }
  4067. }
  4068. else
  4069. {
  4070. if (_timebuf >= GUN_EVSE_WAIT_TIMEOUT)
  4071. {
  4072. _DetectEvseChargingEnableTimeout(gun_index);
  4073. StopGunInfoTimeoutDet(gun_index);
  4074. }
  4075. }
  4076. }
  4077. break;
  4078. case Timeout_EvseCompleteDet:
  4079. {
  4080. if (_timebuf >= GUN_COMP_WAIT_TIMEOUT)
  4081. {
  4082. StopGunInfoTimeoutDet(gun_index);
  4083. }
  4084. }
  4085. break;
  4086. case Timeout_ForCcsPrechargeDet:
  4087. {
  4088. if (_timebuf >= GUN_PRECHARGING_TIMEOUT)
  4089. {
  4090. _CcsPrechargeTimeout(gun_index);
  4091. StopGunInfoTimeoutDet(gun_index);
  4092. }
  4093. }
  4094. break;
  4095. }
  4096. }
  4097. }
  4098. }
  4099. sleep(1);
  4100. }
  4101. }
  4102. }
  4103. void GetSystemTime()
  4104. {
  4105. struct timeb csuTime;
  4106. struct tm *tmCSU;
  4107. ftime(&csuTime);
  4108. tmCSU = localtime(&csuTime.time);
  4109. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  4110. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4111. tmCSU->tm_sec);
  4112. // byte date[14];
  4113. //
  4114. //
  4115. // //sprintf(&date, "%d", );
  4116. //
  4117. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  4118. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  4119. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  4120. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  4121. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  4122. //
  4123. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  4124. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  4125. //
  4126. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  4127. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  4128. //
  4129. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  4130. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  4131. //
  4132. // date[10] = (tmCSU->tm_min) / 10 % 10;
  4133. // date[11] = (tmCSU->tm_min) / 1 % 10;
  4134. //
  4135. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  4136. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  4137. // 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],
  4138. // date[4], date[5], date[6], date[7],
  4139. // date[8], date[9], date[10], date[11],
  4140. // date[12], date[13]);
  4141. }
  4142. void CheckFactoryConfigFunction()
  4143. {
  4144. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4145. {
  4146. char Buf[256];
  4147. sleep(5);
  4148. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  4149. ShmSysConfigAndInfo->SysConfig.ModelName,
  4150. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  4151. system(Buf);
  4152. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4153. system("sync");
  4154. sleep(5);
  4155. system("reboot -f");
  4156. sleep(5);
  4157. system("reboot -f");
  4158. }
  4159. }
  4160. void CheckFwUpdateFunction()
  4161. {
  4162. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  4163. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  4164. {
  4165. DEBUG_INFO_MSG("ftp : update start. \n");
  4166. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4167. {
  4168. setChargerMode(gun_index, SYS_MODE_UPDATE);
  4169. }
  4170. CloseWatchDog();
  4171. sleep(2);
  4172. KillTask();
  4173. byte updateResult = CheckUpdateProcess();
  4174. if (updateResult == PASS)
  4175. DEBUG_INFO_MSG("ftp : update complete. \n");
  4176. else if (updateResult == MODELNAME_FAIL)
  4177. {
  4178. DEBUG_INFO_MSG("ftp : model name is none match. \n");
  4179. KillAllTask();
  4180. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4181. sleep(5);
  4182. system("/usr/bin/run_evse_restart.sh");
  4183. return;
  4184. }
  4185. else
  4186. DEBUG_INFO_MSG("ftp : update fail. \n");
  4187. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  4188. sleep(5);
  4189. system("reboot -f");
  4190. }
  4191. else
  4192. {
  4193. ocpp_chk_update_cmd();
  4194. }
  4195. }
  4196. int TariffParsing(char *StringItem, char *TariffCode)
  4197. {
  4198. char *ptrSave, *ptrToken;
  4199. char strSource[128];
  4200. int fee = 0;
  4201. memcpy(strSource, StringItem, 128);
  4202. ptrToken = strtok_r(strSource, ";", &ptrSave);
  4203. while(ptrToken != NULL)
  4204. {
  4205. char *strTariff = strstr(ptrToken, TariffCode);
  4206. if(strTariff != NULL)
  4207. {
  4208. char *strFee = strstr(strTariff, "$");
  4209. if(strFee != NULL)
  4210. {
  4211. strFee++;
  4212. float fFee = atof(strFee);
  4213. fee = (int)(fFee * 100);
  4214. break;
  4215. }
  4216. }
  4217. ptrToken = strtok_r(NULL, ";", &ptrSave);
  4218. }
  4219. return fee;
  4220. }
  4221. void DefaultPriceHandlerOcpp16(char *StrDefaultPrice)
  4222. {
  4223. unsigned int connectionFee = 0, currentRate = 0, occupancyFee = 0;
  4224. PRINTF_FUNC("DefaultPriceHandler - StrDefaultPrice = %s \n", StrDefaultPrice);
  4225. connectionFee = TariffParsing(StrDefaultPrice, "Connection Fee");
  4226. currentRate = TariffParsing(StrDefaultPrice, "Current Rate");
  4227. occupancyFee = TariffParsing(StrDefaultPrice, "Occupancy Fee");
  4228. PRINTF_FUNC("*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100));
  4229. PRINTF_FUNC("*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100));
  4230. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100));
  4231. ShmDcCommonData->balanceInfo.defaultPrice = currentRate;
  4232. }
  4233. void UserPriceHandlerOcpp16(char *UserId, char *UserPrice)
  4234. {
  4235. unsigned int connectionFee = 0, currentRate = 0, occupancyFee = 0;
  4236. int accountBalance = 0;
  4237. byte acGunIndex = 0;
  4238. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  4239. {
  4240. if (gunIndex == 1)
  4241. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4242. else
  4243. acGunIndex = 0;
  4244. if((chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4245. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING) &&
  4246. strcmp((char *)chargingInfo[gunIndex]->StartUserId, UserId) == EQUAL)
  4247. {
  4248. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Connection Fee");
  4249. currentRate = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Current Rate");
  4250. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Occupancy Fee");
  4251. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Account Balance");
  4252. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].UserPrice != currentRate ||
  4253. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4254. {
  4255. PRINTF_FUNC("*** Connection Fee: %d.%02d ***", (connectionFee / 100), (connectionFee % 100));
  4256. PRINTF_FUNC("*** Current Rate: %d.%02d ***", (currentRate / 100), (currentRate % 100));
  4257. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***", (occupancyFee / 100), (occupancyFee % 100));
  4258. PRINTF_FUNC("*** Account Balance: %d.%02d ***", (accountBalance / 100), (accountBalance % 100));
  4259. PRINTF_FUNC("*** Connector Id %d User Price %d.%02d, Account Balance %d.%02d ***",
  4260. (gunIndex), (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100));
  4261. }
  4262. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].UserPrice = currentRate;
  4263. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance = accountBalance;
  4264. }
  4265. }
  4266. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex++)
  4267. {
  4268. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4269. if(strcmp((char *)ac_chargingInfo[gunIndex]->StartUserId, UserId) == EQUAL)
  4270. {
  4271. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].UserPrice != currentRate ||
  4272. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance != accountBalance)
  4273. {
  4274. PRINTF_FUNC("*** AC Connector User Price %d.%02d, Account Balance %d.%02d ***",
  4275. (currentRate / 100), (currentRate % 100), (accountBalance / 100), (accountBalance % 100));
  4276. }
  4277. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].UserPrice = currentRate;
  4278. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance = accountBalance;
  4279. }
  4280. }
  4281. }
  4282. void RunningFinalCostHandlerOcpp16()
  4283. {
  4284. unsigned int connectionFee = 0, sessionFee = 0, occupancyFee = 0, totalCost = 0;
  4285. int accountBalance = 0;
  4286. byte acGunIndex = 0;
  4287. bool isFind = false;
  4288. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  4289. {
  4290. isFind = false;
  4291. if (gunIndex == 1)
  4292. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4293. else
  4294. acGunIndex = 0;
  4295. // PRINTF_FUNC("*** gunIndex = %d, description = %s *** \n", gunIndex + acGunIndex,
  4296. // ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description);
  4297. // PRINTF_FUNC("*** gunIndex = %d, f description = %s *** \n", gunIndex + acGunIndex,
  4298. // ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description);
  4299. if (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4300. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4301. {
  4302. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description) > 0)
  4303. {
  4304. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Connection Fee");
  4305. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Session Fee");
  4306. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Occupancy Fee");
  4307. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Total Cost");
  4308. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex].description, "Account Balance");
  4309. PRINTF_FUNC("*** Connector Id: %d Running Cost *** \n", gunIndex);
  4310. isFind = true;
  4311. memset(&ShmOCPP16Data->Cost.RunningCost[gunIndex + acGunIndex], 0x00, sizeof(struct StrcutRunningCost));
  4312. }
  4313. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description) > 0)
  4314. {
  4315. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Connection Fee");
  4316. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Session Fee");
  4317. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Occupancy Fee");
  4318. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Total Cost");
  4319. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex].description, "Account Balance");
  4320. PRINTF_FUNC("********** Connector Id: %d Final Cost ********** \n", gunIndex);
  4321. isFind = true;
  4322. memset(&ShmOCPP16Data->Cost.FinalCost[gunIndex + acGunIndex], 0x00, sizeof(struct StrcutFinalCost));
  4323. }
  4324. if (isFind)
  4325. {
  4326. PRINTF_FUNC("*** Connection Fee: %d.%02d *** \n", (connectionFee / 100), (connectionFee % 100));
  4327. PRINTF_FUNC("*** Session Fee: %d.%02d *** \n", (sessionFee / 100), (sessionFee % 100));
  4328. PRINTF_FUNC("*** Occupancy Fee: %d.%02d *** \n", (occupancyFee / 100), (occupancyFee % 100));
  4329. PRINTF_FUNC("*** Total Cost: %d.%02d *** \n", (totalCost / 100), (totalCost % 100));
  4330. PRINTF_FUNC("*** Account Balance: %d.%02d *** \n", (accountBalance / 100), (accountBalance % 100));
  4331. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost != totalCost ||
  4332. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4333. {
  4334. PRINTF_FUNC("*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d *** \n",
  4335. (gunIndex), (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100));
  4336. }
  4337. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost = totalCost;
  4338. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance = accountBalance;
  4339. }
  4340. }
  4341. }
  4342. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; gunIndex++)
  4343. {
  4344. isFind = false;
  4345. acGunIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4346. if (ac_chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_PREPARING &&
  4347. ac_chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_ALARM)
  4348. {
  4349. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description) > 0)
  4350. {
  4351. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Connection Fee");
  4352. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Session Fee");
  4353. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Occupancy Fee");
  4354. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Total Cost");
  4355. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[acGunIndex].description, "Account Balance");
  4356. PRINTF_FUNC("*** AC Connector : Running Cost ***\n");
  4357. isFind = true;
  4358. memset(&ShmOCPP16Data->Cost.RunningCost[acGunIndex], 0x00, sizeof(struct StrcutRunningCost));
  4359. }
  4360. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description) > 0)
  4361. {
  4362. connectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Connection Fee");
  4363. sessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Session Fee");
  4364. occupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Occupancy Fee");
  4365. totalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Total Cost");
  4366. accountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[acGunIndex].description, "Account Balance");
  4367. PRINTF_FUNC("*** AC Connector : Final Cost ***\n");
  4368. isFind = true;
  4369. memset(&ShmOCPP16Data->Cost.FinalCost[acGunIndex], 0x00, sizeof(struct StrcutFinalCost));
  4370. }
  4371. if (isFind)
  4372. {
  4373. PRINTF_FUNC("*** Connection Fee: %d.%02d ***\n", (connectionFee / 100), (connectionFee % 100));
  4374. PRINTF_FUNC("*** Session Fee: %d.%02d ***\n", (sessionFee / 100), (sessionFee % 100));
  4375. PRINTF_FUNC("*** Occupancy Fee: %d.%02d ***\n", (occupancyFee / 100), (occupancyFee % 100));
  4376. PRINTF_FUNC("*** Total Cost: %d.%02d ***\n", (totalCost / 100), (totalCost % 100));
  4377. PRINTF_FUNC("*** Account Balance: %d.%02d ***\n", (accountBalance / 100), (accountBalance % 100));
  4378. if (ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].TotalCost != totalCost ||
  4379. ShmDcCommonData->balanceInfo.connectorBalanceInfo[gunIndex + acGunIndex].AccountBalance != accountBalance)
  4380. {
  4381. PRINTF_FUNC("*** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d ***\n",
  4382. (gunIndex), (totalCost / 100), (totalCost % 100), (accountBalance / 100), (accountBalance % 100));
  4383. }
  4384. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].TotalCost = totalCost;
  4385. ShmDcCommonData->balanceInfo.connectorBalanceInfo[acGunIndex].AccountBalance = accountBalance;
  4386. }
  4387. }
  4388. }
  4389. }
  4390. //===============================================
  4391. // ocpp key
  4392. //===============================================
  4393. bool ocpp_authorizeRemoteTxReqChk_key()
  4394. {
  4395. bool result = false;
  4396. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4397. {
  4398. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
  4399. result = true;
  4400. }
  4401. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4402. {
  4403. }
  4404. return result;
  4405. }
  4406. //===============================================
  4407. // Check reservation date is expired
  4408. //===============================================
  4409. int opcc_chk_reserve_expired(byte gun_index)
  4410. {
  4411. int result = NO;
  4412. byte acDirIndex = 0;
  4413. if (gun_index == 1)
  4414. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4415. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4416. {
  4417. if(chargingInfo[gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  4418. PRINTF_FUNC("%s \n", ShmOCPP16Data->ReserveNow[gun_index + acDirIndex].ExpiryDate);
  4419. if (CheckTimeOut(ShmOCPP16Data->ReserveNow[gun_index + acDirIndex].ExpiryDate))
  4420. result = YES;
  4421. }
  4422. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4423. {
  4424. if(chargingInfo[gun_index]->SystemStatus != SYS_MODE_RESERVATION)
  4425. PRINTF_FUNC("%s \n", ShmOCPP20Data->ReserveNow[gun_index + acDirIndex].expiryDateTime);
  4426. if (CheckTimeOut(ShmOCPP20Data->ReserveNow[gun_index + acDirIndex].expiryDateTime))
  4427. result = YES;
  4428. }
  4429. return result;
  4430. }
  4431. //===============================================
  4432. // AC OCPP routine
  4433. //===============================================
  4434. void ocpp_ac_chk_availability_cmd()
  4435. {
  4436. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4437. byte type = 0;
  4438. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  4439. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4440. {
  4441. if (ShmOCPP16Data->CsMsg.bits[hasDc].ChangeAvailabilityReq == YES)
  4442. {
  4443. PRINTF_FUNC("***************ChkOcppStatus : AC OcppChangeAvailability******************** \n");
  4444. DEBUG_ERROR_MSG("***************ChkOcppStatus : AC OcppChangeAvailability******************** \n");
  4445. ShmOCPP16Data->CsMsg.bits[hasDc].ChangeAvailabilityReq = NO;
  4446. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[hasDc].Type, "Operative") == EQUAL)
  4447. {
  4448. if (isDb_ready)
  4449. DB_Update_Operactive(localDb, hasDc, true);
  4450. type = 1;
  4451. }
  4452. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[hasDc].Type, "Inoperative") == EQUAL)
  4453. {
  4454. if (isDb_ready)
  4455. DB_Update_Operactive(localDb, hasDc, false);
  4456. type = 2;
  4457. }
  4458. }
  4459. }
  4460. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4461. {
  4462. if (ShmOCPP20Data->CsMsg.bits[hasDc].ChangeAvailabilityReq == YES)
  4463. {
  4464. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4465. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4466. ShmOCPP20Data->CsMsg.bits[hasDc].ChangeAvailabilityReq = NO;
  4467. if(strcmp((char *)ShmOCPP20Data->ChangeAvailability[hasDc].operationalStatus, "Operative") == EQUAL)
  4468. {
  4469. if (isDb_ready)
  4470. DB_Update_Operactive(localDb, hasDc, true);
  4471. type = 1;
  4472. }
  4473. else if (strcmp((char *)ShmOCPP20Data->ChangeAvailability[hasDc].operationalStatus, "Inoperative") == EQUAL)
  4474. {
  4475. if (isDb_ready)
  4476. DB_Update_Operactive(localDb, hasDc, false);
  4477. type = 2;
  4478. }
  4479. }
  4480. }
  4481. if (type == 1)
  4482. {
  4483. ac_chargingInfo[0]->IsAvailable = YES;
  4484. }
  4485. else if (type == 2)
  4486. {
  4487. ac_chargingInfo[0]->IsAvailable = NO;
  4488. }
  4489. }
  4490. void ocpp_ac_chk_unlock_cmd()
  4491. {
  4492. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4493. // 如果有 DC 槍~ 則 AC 槍在 index = 1 的位置~ 如果沒有 DC 槍則在 index = 0 的位置
  4494. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4495. {
  4496. if (ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorReq == YES)
  4497. {
  4498. ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorReq = NO;
  4499. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4500. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  4501. {
  4502. // Unlocked - 充電中,需停止充電
  4503. strcpy((char *)ShmOCPP16Data->StopTransaction[hasDc].StopReason, "UnlockCommand");
  4504. ac_chargingInfo[0]->StopChargeFlag = YES;
  4505. }
  4506. strcpy((char *)ShmOCPP16Data->UnlockConnector[hasDc].ResponseStatus, "Unlocked");
  4507. ShmOCPP16Data->CsMsg.bits[hasDc].UnlockConnectorConf = YES;
  4508. }
  4509. }
  4510. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4511. {
  4512. if (ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorReq == YES)
  4513. {
  4514. ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorReq = NO;
  4515. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4516. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  4517. {
  4518. // Unlocked - 充電中,需停止充電
  4519. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].transactionInfo.stoppedReason, "UnlockCommand");
  4520. ac_chargingInfo[0]->StopChargeFlag = YES;
  4521. }
  4522. strcpy((char*)ShmOCPP20Data->UnlockConnector[hasDc].Response_status, "Unlocked");
  4523. ShmOCPP20Data->CsMsg.bits[hasDc].UnlockConnectorConf = YES;
  4524. }
  4525. }
  4526. }
  4527. bool ocpp_ac_chk_profileConf_cmd()
  4528. {
  4529. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4530. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4531. {
  4532. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf == YES)
  4533. {
  4534. ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf = NO;
  4535. return true;
  4536. }
  4537. }
  4538. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4539. {
  4540. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf == YES)
  4541. {
  4542. ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf = NO;
  4543. return true;
  4544. }
  4545. }
  4546. return false;
  4547. }
  4548. void ocpp_ac_chargingProfile_process()
  4549. {
  4550. int _time = 0;
  4551. int _startCount = NO_DEFINE;
  4552. int _maxCount = 0;
  4553. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4554. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4555. {
  4556. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingProfileKind, "Absolute") == EQUAL &&
  4557. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingProfileId == YES)
  4558. {
  4559. _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.StartSchedule);
  4560. _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod);
  4561. _startCount = NO_DEFINE;
  4562. for (byte _count = 0; _count < _maxCount; _count++)
  4563. {
  4564. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4565. if (_time >= ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  4566. {
  4567. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  4568. ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  4569. {
  4570. _startCount = _count;
  4571. }
  4572. }
  4573. }
  4574. PRINTF_FUNC("AC - Gun_%d, Get Profile - Profile Index = %d \n", hasDc, _startCount);
  4575. if (_startCount < _maxCount)
  4576. {
  4577. PRINTF_FUNC("Profile Limit = %.2f \n", ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  4578. ac_chargingInfo[0]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->SmartChargingProfile[hasDc].ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  4579. if (ac_chargingInfo[0]->EvBatterytargetVoltage > 0 ||
  4580. ac_chargingInfo[0]->PresentChargingVoltage > 0)
  4581. ac_chargingInfo[0]->ChargingProfileCurrent = (ac_chargingInfo[0]->ChargingProfilePower / ac_chargingInfo[0]->PresentChargingVoltage) * 10;
  4582. else
  4583. ac_chargingInfo[0]->ChargingProfileCurrent = 0;
  4584. }
  4585. else
  4586. {
  4587. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4588. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4589. }
  4590. }
  4591. else
  4592. {
  4593. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4594. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4595. }
  4596. }
  4597. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4598. {
  4599. if (strcmp((char *)ShmOCPP20Data->SmartChargingProfile[hasDc].chargingProfileKind, "Absolute") == EQUAL &&
  4600. ShmOCPP20Data->SmartChargingProfile[hasDc].id == YES)
  4601. {
  4602. _time = GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].startSchedule);
  4603. _maxCount = ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod);
  4604. _startCount = NO_DEFINE;
  4605. for (byte _count = 0; _count < _maxCount; _count++)
  4606. {
  4607. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4608. if (_time >= ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].startPeriod)
  4609. {
  4610. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].limit >= MIN_OUTPUT_CUR) ||
  4611. ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_count].limit > MIN_OUTPUT_CUR)
  4612. {
  4613. _startCount = _count;
  4614. }
  4615. }
  4616. }
  4617. PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", 1, _startCount);
  4618. if (_startCount < _maxCount)
  4619. {
  4620. PRINTF_FUNC("Profile Limit = %.2f \n", ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit);
  4621. ac_chargingInfo[0]->ChargingProfilePower = ShmOCPP20Data->SmartChargingProfile[hasDc].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit * AC_OUTPUT_VOL;
  4622. if (ac_chargingInfo[0]->EvBatterytargetVoltage > 0 && ac_chargingInfo[0]->PresentChargingVoltage > 0)
  4623. ac_chargingInfo[0]->ChargingProfileCurrent = (ac_chargingInfo[0]->ChargingProfilePower / ac_chargingInfo[0]->PresentChargingVoltage) * 10;
  4624. else
  4625. ac_chargingInfo[0]->ChargingProfileCurrent = 0;
  4626. }
  4627. else
  4628. {
  4629. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4630. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4631. }
  4632. }
  4633. else
  4634. {
  4635. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4636. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4637. }
  4638. }
  4639. else
  4640. {
  4641. ac_chargingInfo[0]->ChargingProfilePower = -1;
  4642. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  4643. }
  4644. if (ac_chargingInfo [0]->ChargingProfilePower > 0 &&
  4645. _AcChargingProfilePwBuffer != chargingInfo [0]->ChargingProfilePower)
  4646. {
  4647. _AcChargingProfilePwBuffer = chargingInfo [0]->ChargingProfilePower;
  4648. PRINTF_FUNC ( "Profile : ChargingProfilePower = %.2f KW \n", ac_chargingInfo [0]->ChargingProfilePower / 1000 );
  4649. PRINTF_FUNC ( "Profile : ChargingProfileCurrent = %.2f \n", ac_chargingInfo[0]->ChargingProfileCurrent / 10);
  4650. }
  4651. }
  4652. void CheckAcSmartChargeProfile()
  4653. {
  4654. if (ocpp_ac_chk_profileConf_cmd())
  4655. {
  4656. ocpp_ac_chargingProfile_process();
  4657. }
  4658. }
  4659. void ocpp_ac_lift_profileReq_cmd(byte gunIndex)
  4660. {
  4661. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  4662. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4663. {
  4664. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileConf == NO)
  4665. {
  4666. if (ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileReq == NO)
  4667. ShmOCPP16Data->CSUMsg.bits[hasDc].ChargingProfileReq = YES;
  4668. }
  4669. }
  4670. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4671. {
  4672. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileConf == NO)
  4673. {
  4674. if (ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileReq == NO)
  4675. ShmOCPP20Data->CSUMsg.bits[hasDc].ChargingProfileReq = YES;
  4676. }
  4677. }
  4678. }
  4679. //===============================================
  4680. // OCPP routine
  4681. //===============================================
  4682. void ocpp_process_start()
  4683. {
  4684. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  4685. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  4686. {
  4687. if(system("pidof -s OcppBackend > /dev/null") != 0)
  4688. {
  4689. DEBUG_ERROR_MSG("OcppBackend not running, restart it.\r\n");
  4690. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4691. system("/root/OcppBackend &");
  4692. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4693. system("/root/OcppBackend20 &");
  4694. }
  4695. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4696. {
  4697. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  4698. {
  4699. DEBUG_ERROR_MSG("OcppBackend watch dog timeout task restart.\n");
  4700. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  4701. system("pkill OcppBackend");
  4702. }
  4703. }
  4704. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4705. {
  4706. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  4707. {
  4708. DEBUG_ERROR_MSG("OcppBackend20 watch dog timeout task restart.\n");
  4709. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  4710. system("pkill OcppBackend20");
  4711. }
  4712. }
  4713. }
  4714. }
  4715. void ocpp_auto_response_BootNotification()
  4716. {
  4717. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4718. {
  4719. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  4720. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  4721. }
  4722. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4723. {
  4724. if (ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES)
  4725. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = NO;
  4726. }
  4727. }
  4728. bool ocpp_is_resPass_StartTransactionConf(byte gun_index)
  4729. {
  4730. bool result = false;
  4731. byte acDirIndex = 0;
  4732. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  4733. if (gun_index == 1)
  4734. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4735. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4736. {
  4737. result = ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf;
  4738. }
  4739. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4740. {
  4741. result = ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf;
  4742. }
  4743. return result;
  4744. }
  4745. void ocpp_auto_response_StartTransationConf(byte gun_index)
  4746. {
  4747. byte acDirIndex = 0;
  4748. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  4749. if (gun_index == 1)
  4750. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4751. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4752. {
  4753. if (ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf)
  4754. ShmOCPP16Data->CpMsg.bits[gun_index + acDirIndex].StartTransactionConf = NO;
  4755. }
  4756. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4757. {
  4758. if (ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf)
  4759. ShmOCPP20Data->CpMsg.bits[gun_index + acDirIndex].TransactionEventConf = NO;
  4760. }
  4761. }
  4762. void ocpp_auto_response_ReserveConf(byte gun_index)
  4763. {
  4764. byte acDirIndex = 0;
  4765. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  4766. if (gun_index == 1)
  4767. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4768. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4769. ShmOCPP16Data->CsMsg.bits[gun_index + acDirIndex].ReserveNowConf = YES;
  4770. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4771. ShmOCPP20Data->CsMsg.bits[gun_index + acDirIndex].ReserveNowConf = YES;
  4772. }
  4773. bool ocpp_chk_authrization_cmd()
  4774. {
  4775. char buf2[16] = "";
  4776. memset(buf2, 0, ARRAY_SIZE(buf2));
  4777. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4778. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  4779. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4780. sprintf(buf2, "%s", ShmOCPP20Data->Authorize.Response_idTokenInfo.status);
  4781. // 因為無法得知實際的長度,所以只能用搜尋的方式
  4782. if(strcmp(buf2, "Accepted") == EQUAL)
  4783. return true;
  4784. return false;
  4785. }
  4786. bool opcc_sub_get_reset_req()
  4787. {
  4788. bool result = false;
  4789. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4790. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  4791. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4792. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  4793. return result;
  4794. }
  4795. void ocpp_sub_run_reset(bool canReset)
  4796. {
  4797. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4798. {
  4799. if(canReset && strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  4800. {
  4801. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  4802. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  4803. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  4804. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  4805. sleep(3);
  4806. system("reboot -f");
  4807. }
  4808. else if (canReset && strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  4809. {
  4810. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  4811. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  4812. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  4813. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  4814. sleep(3);
  4815. KillAllTask();
  4816. CloseWatchDog();
  4817. system("/usr/bin/run_evse_restart.sh");
  4818. }
  4819. }
  4820. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4821. {
  4822. if(canReset && strcmp((char *)ShmOCPP20Data->Reset.type, "Immediate") == EQUAL)
  4823. {
  4824. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  4825. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  4826. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  4827. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  4828. sleep(3);
  4829. system("reboot -f");
  4830. }
  4831. else if (canReset && strcmp((char *)ShmOCPP20Data->Reset.type, "OnIdle") == EQUAL)
  4832. {
  4833. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  4834. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  4835. ShmOCPP20Data->MsMsg.bits.ResetReq = NO;
  4836. ShmOCPP20Data->MsMsg.bits.ResetConf = YES;
  4837. sleep(3);
  4838. KillAllTask();
  4839. CloseWatchDog();
  4840. system("/usr/bin/run_evse_restart.sh");
  4841. }
  4842. }
  4843. }
  4844. void ocpp_chk_reset_cmd(byte isforce)
  4845. {
  4846. if (opcc_sub_get_reset_req())
  4847. {
  4848. bool canReset = true;
  4849. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  4850. {
  4851. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  4852. {
  4853. if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  4854. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING)
  4855. {
  4856. // Reset - AC 先停止充電
  4857. ac_chargingInfo[0]->StopChargeFlag = YES;
  4858. GetTimespecFunc(&_resetChkTime);
  4859. }
  4860. }
  4861. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4862. {
  4863. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_REASSIGN &&
  4864. chargingInfo[_index]->SystemStatus <= SYS_MODE_TERMINATING)
  4865. {
  4866. canReset = false;
  4867. // Reset 系統先停止充電
  4868. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4869. {
  4870. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  4871. ocpp_set_stopReason_by_cmd(_index, "HardReset");
  4872. else
  4873. ocpp_set_stopReason_by_cmd(_index, "SoftReset");
  4874. }
  4875. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4876. {
  4877. if(strcmp((char *)ShmOCPP20Data->Reset.type, "Immediate") == EQUAL)
  4878. ocpp_set_stopReason_by_cmd(_index, "HardReset");
  4879. else
  4880. ocpp_set_stopReason_by_cmd(_index, "SoftReset");
  4881. }
  4882. ChargingTerminalProcess(_index);
  4883. GetTimespecFunc(&_resetChkTime);
  4884. }
  4885. }
  4886. }
  4887. if (canReset)
  4888. {
  4889. if (GetTimeoutValue(&_resetChkTime) < 0)
  4890. GetTimespecFunc(&_resetChkTime);
  4891. if (GetTimeoutValue(&_resetChkTime) >= 5)
  4892. {
  4893. ShmDcCommonData->evBoardResetFlag = YES;
  4894. // Reset -> change status to maintain.
  4895. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4896. {
  4897. setChargerMode(_index, SYS_MODE_MAINTAIN);
  4898. }
  4899. ChangeLcmByIndex(_LCM_FIX);
  4900. }
  4901. else
  4902. canReset = false;
  4903. }
  4904. else
  4905. GetTimespecFunc(&_resetChkTime);
  4906. if (isforce == YES)
  4907. canReset = true;
  4908. ocpp_sub_run_reset(canReset);
  4909. }
  4910. }
  4911. void ocpp_chk_reserved_cmd(byte gunIndex)
  4912. {
  4913. byte acDirIndex = 0;
  4914. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  4915. if (gunIndex == 1)
  4916. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4917. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4918. {
  4919. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE &&
  4920. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq == YES)
  4921. {
  4922. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4923. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq = NO;
  4924. if (!opcc_chk_reserve_expired(gunIndex))
  4925. {
  4926. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId);
  4927. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].IdTag);
  4928. PRINTF_FUNC("*************ChkOcppStatus : OcppReservedStatus End****************** \n");
  4929. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  4930. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  4931. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex],
  4932. (char *)ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].IdTag);
  4933. }
  4934. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowConf = YES;
  4935. }
  4936. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION &&
  4937. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq == YES)
  4938. {
  4939. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq = NO;
  4940. if (ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId == chargingInfo[gunIndex]->ReservationId)
  4941. {
  4942. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4943. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP16Data->ReserveNow[gunIndex + acDirIndex].ReservationId);
  4944. PRINTF_FUNC("***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n");
  4945. chargingInfo[gunIndex]->ReservationId = 0;
  4946. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_IDLE;
  4947. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex], "");
  4948. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationConf = YES;
  4949. }
  4950. }
  4951. }
  4952. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4953. {
  4954. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE &&
  4955. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq == YES)
  4956. {
  4957. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4958. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowReq = NO;
  4959. if (!opcc_chk_reserve_expired(gunIndex))
  4960. {
  4961. PRINTF_FUNC("id = %d \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id);
  4962. PRINTF_FUNC("idToken = %s \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].idToken.idToken);
  4963. PRINTF_FUNC("*************ChkOcppStatus : OcppReservedStatus End****************** \n");
  4964. chargingInfo[gunIndex]->ReservationId = ShmOCPP20Data->ReserveNow[gunIndex].id;
  4965. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_RESERVATION;
  4966. strncpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex],
  4967. (char *)ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].idToken.idToken, 30);
  4968. }
  4969. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ReserveNowConf = YES;
  4970. }
  4971. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION &&
  4972. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq == YES)
  4973. {
  4974. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationReq = NO;
  4975. if (ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id == chargingInfo[gunIndex]->ReservationId)
  4976. {
  4977. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4978. PRINTF_FUNC("ReservationId = %d \n", ShmOCPP20Data->ReserveNow[gunIndex + acDirIndex].id);
  4979. PRINTF_FUNC("***********ChkOcppStatus : Cancel OcppReservedStatus End**************** \n");
  4980. chargingInfo[gunIndex]->ReservationId = 0;
  4981. chargingInfo[gunIndex]->SystemStatus = SYS_MODE_IDLE;
  4982. strcpy((char *)ShmDcCommonData->_reserved_UserId[gunIndex], "");
  4983. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].CancelReservationConf = YES;
  4984. }
  4985. }
  4986. }
  4987. }
  4988. void ocpp_chk_availability_cmd(byte gunIndex)
  4989. {
  4990. byte type = 0; // 0 : none, 1 : operative, 2 inoperative
  4991. byte acDirIndex = 0;
  4992. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  4993. if (gunIndex == 1)
  4994. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4995. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4996. {
  4997. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  4998. {
  4999. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5000. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5001. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5002. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex + acDirIndex].Type, "Operative") == EQUAL)
  5003. {
  5004. if (isDb_ready)
  5005. DB_Update_Operactive(localDb, gunIndex + acDirIndex, true);
  5006. type = 1;
  5007. }
  5008. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex + acDirIndex].Type, "Inoperative") == EQUAL)
  5009. {
  5010. if (isDb_ready)
  5011. DB_Update_Operactive(localDb, gunIndex + acDirIndex, false);
  5012. type = 2;
  5013. }
  5014. }
  5015. }
  5016. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5017. {
  5018. if (ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq == YES)
  5019. {
  5020. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5021. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  5022. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].ChangeAvailabilityReq = NO;
  5023. if(strcmp((char *)ShmOCPP20Data->ChangeAvailability[gunIndex + acDirIndex].operationalStatus, "Operative") == EQUAL)
  5024. {
  5025. if (isDb_ready)
  5026. DB_Update_Operactive(localDb, gunIndex + acDirIndex, true);
  5027. type = 1;
  5028. }
  5029. else if (strcmp((char *)ShmOCPP20Data->ChangeAvailability[gunIndex + acDirIndex].operationalStatus, "Inoperative") == EQUAL)
  5030. {
  5031. if (isDb_ready)
  5032. DB_Update_Operactive(localDb, gunIndex + acDirIndex, false);
  5033. type = 2;
  5034. }
  5035. }
  5036. }
  5037. if (type == 1)
  5038. {
  5039. chargingInfo[gunIndex]->IsAvailable = YES;
  5040. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5041. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION ||
  5042. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5043. {
  5044. setChargerMode(gunIndex, SYS_MODE_IDLE);
  5045. }
  5046. }
  5047. else if (type == 2)
  5048. {
  5049. chargingInfo[gunIndex]->IsAvailable = NO;
  5050. if (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5051. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_RESERVATION ||
  5052. chargingInfo[gunIndex]->SystemStatus == SYS_MODE_MAINTAIN)
  5053. {
  5054. setChargerMode(gunIndex, SYS_MODE_MAINTAIN);
  5055. }
  5056. }
  5057. }
  5058. void ocpp_chk_unlock_cmd(byte gunIndex)
  5059. {
  5060. byte acDirIndex = 0;
  5061. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5062. if (gunIndex == 1)
  5063. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5064. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5065. {
  5066. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5067. {
  5068. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5069. if (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5070. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING)
  5071. {
  5072. // Unlocked - 充電中,需停止充電
  5073. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "UnlockCommand");
  5074. ChargingTerminalProcess(gunIndex);
  5075. }
  5076. if (chargingInfo[gunIndex]->Type == _Type_Chademo ||
  5077. chargingInfo[gunIndex]->Type == _Type_GB)
  5078. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "UnlockCommand");
  5079. else
  5080. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "UnlockCommand");
  5081. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorConf = YES;
  5082. }
  5083. }
  5084. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5085. {
  5086. if (ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq == YES)
  5087. {
  5088. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorReq = NO;
  5089. if (chargingInfo[gunIndex]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK &&
  5090. chargingInfo[gunIndex]->SystemStatus <= SYS_MODE_CHARGING)
  5091. {
  5092. // Unlocked - 充電中,需停止充電
  5093. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "UnlockCommand");
  5094. ChargingTerminalProcess(gunIndex);
  5095. }
  5096. if (chargingInfo[gunIndex]->Type == _Type_Chademo ||
  5097. chargingInfo[gunIndex]->Type == _Type_GB)
  5098. strcpy((char*)ShmOCPP20Data->UnlockConnector[gunIndex + acDirIndex].Response_status, "UnlockCommand");
  5099. else
  5100. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex + acDirIndex].ResponseStatus, "UnlockCommand");
  5101. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].UnlockConnectorConf = YES;
  5102. }
  5103. }
  5104. }
  5105. bool ocpp_chk_remoteStop_cmd(byte gunIndex)
  5106. {
  5107. bool result = false;
  5108. byte acDirIndex = 0;
  5109. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5110. if (gunIndex == 1)
  5111. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5112. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5113. result = ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStopTransactionReq;
  5114. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5115. result = ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStopTransactionReq;
  5116. if (result)
  5117. {
  5118. DEBUG_INFO_MSG("Remote Stop by OCPP (%d) \n", gunIndex + acDirIndex);
  5119. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5120. {
  5121. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "Remote");
  5122. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].RemoteStopTransactionReq = NO;
  5123. }
  5124. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5125. {
  5126. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "Remote");
  5127. ShmOCPP20Data->CsMsg.bits[gunIndex + acDirIndex].RequestStopTransactionReq = NO;
  5128. }
  5129. }
  5130. return result;
  5131. }
  5132. bool ocpp_sub_get_remote_start_transaction_req(byte gun_index)
  5133. {
  5134. bool result = false;
  5135. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5136. {
  5137. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  5138. }
  5139. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5140. {
  5141. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
  5142. }
  5143. if (result && ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5144. {
  5145. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag,
  5146. (char *)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag);
  5147. }
  5148. return result;
  5149. }
  5150. void ocpp_sub_set_remote_start_transaction_req(byte gunIndex, bool result)
  5151. {
  5152. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5153. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStartTransactionReq = result;
  5154. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5155. ShmOCPP20Data->CsMsg.bits[gunIndex].RequestStartTransactionReq = result;
  5156. }
  5157. void ocpp_chk_remoteStart_cmd()
  5158. {
  5159. if(!isDetectPlugin())
  5160. {
  5161. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  5162. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5163. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++)
  5164. {
  5165. if (ocpp_sub_get_remote_start_transaction_req(acDirIndex))
  5166. {
  5167. if (ac_chargingInfo[ac_index]->SystemStatus == SYS_MODE_IDLE ||
  5168. ac_chargingInfo[ac_index]->SystemStatus == SYS_MODE_RESERVATION)
  5169. {
  5170. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  5171. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5172. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  5173. //ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  5174. DetectPluginStart();
  5175. return;
  5176. }
  5177. ocpp_sub_set_remote_start_transaction_req(acDirIndex, NO);
  5178. }
  5179. }
  5180. byte threeGunIndex = 0;
  5181. byte dcIndex = 0;
  5182. bool isGunUsingStatus = false;
  5183. byte scheduleIndex = 0;
  5184. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5185. {
  5186. // 如果有 AC 槍,且 DC 槍也有兩把
  5187. if (acDirIndex == 1 && _index == 1)
  5188. threeGunIndex = 1;
  5189. if (ocpp_sub_get_remote_start_transaction_req(_index + threeGunIndex))
  5190. dcIndex = _index;
  5191. if (chargingInfo[_index]->SystemStatus != SYS_MODE_IDLE)
  5192. {
  5193. isGunUsingStatus = true;
  5194. }
  5195. if (chargingInfo[_index]->schedule.isTriggerStart)
  5196. scheduleIndex = _index;
  5197. }
  5198. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  5199. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  5200. {
  5201. if (dcIndex == 0)
  5202. threeGunIndex = 0;
  5203. ocpp_sub_set_remote_start_transaction_req(dcIndex + threeGunIndex, NO);
  5204. return;
  5205. }
  5206. if (dcIndex == 0)
  5207. threeGunIndex = 0;
  5208. if (ocpp_sub_get_remote_start_transaction_req(dcIndex + threeGunIndex))
  5209. {
  5210. if (chargingInfo[dcIndex]->SystemStatus == SYS_MODE_IDLE ||
  5211. chargingInfo[dcIndex]->SystemStatus == SYS_MODE_RESERVATION)
  5212. {
  5213. if (ocpp_authorizeRemoteTxReqChk_key())
  5214. {
  5215. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId,
  5216. (char *)ShmOCPP16Data->RemoteStartTransaction[dcIndex + threeGunIndex].IdTag);
  5217. }
  5218. else
  5219. {
  5220. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  5221. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5222. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  5223. DetectPluginStart();
  5224. }
  5225. }
  5226. ocpp_sub_set_remote_start_transaction_req(dcIndex + threeGunIndex, NO);
  5227. }
  5228. else if (chargingInfo[scheduleIndex]->schedule.isTriggerStart)
  5229. {
  5230. if (chargingInfo[scheduleIndex]->SystemStatus == SYS_MODE_IDLE ||
  5231. chargingInfo[scheduleIndex]->SystemStatus == SYS_MODE_RESERVATION)
  5232. {
  5233. chargingInfo[scheduleIndex]->RemoteStartFlag = YES;
  5234. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  5235. DetectPluginStart();
  5236. }
  5237. chargingInfo[scheduleIndex]->schedule.isTriggerStart = NO;
  5238. }
  5239. }
  5240. }
  5241. void ocpp_chk_update_cmd()
  5242. {
  5243. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5244. {
  5245. if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  5246. {
  5247. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  5248. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  5249. {
  5250. DEBUG_INFO_MSG("Backend : update start. \n");
  5251. sleep(2);
  5252. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  5253. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  5254. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5255. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5256. {
  5257. setChargerMode(_index, SYS_MODE_UPDATE);
  5258. }
  5259. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5260. {
  5261. ac_chargingInfo[_index]->SystemStatus = SYS_MODE_UPDATE;
  5262. }
  5263. CloseWatchDog();
  5264. sleep(2);
  5265. KillTask();
  5266. byte updateResult = CheckUpdateProcess();
  5267. if (updateResult == PASS)
  5268. {
  5269. DEBUG_INFO_MSG("Backend : update complete. \n");
  5270. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5271. }
  5272. else if (updateResult == MODELNAME_FAIL)
  5273. {
  5274. DEBUG_INFO_MSG("Backend : model name is none match. \n");
  5275. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5276. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5277. sleep(5);
  5278. KillAllTask();
  5279. system("/usr/bin/run_evse_restart.sh");
  5280. return;
  5281. }
  5282. else
  5283. {
  5284. DEBUG_INFO_MSG("Backend : update fail. \n");
  5285. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5286. }
  5287. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5288. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5289. sleep(5);
  5290. system("reboot -f");
  5291. }
  5292. }
  5293. }
  5294. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5295. {
  5296. if(ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES)
  5297. {
  5298. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = NO;
  5299. if (strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded") == EQUAL)
  5300. {
  5301. DEBUG_INFO_MSG("Backend : update start. \n");
  5302. sleep(2);
  5303. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "");
  5304. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installing");
  5305. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5306. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5307. {
  5308. setChargerMode(_index, SYS_MODE_UPDATE);
  5309. }
  5310. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  5311. {
  5312. ac_chargingInfo[_index]->SystemStatus = SYS_MODE_UPDATE;
  5313. }
  5314. CloseWatchDog();
  5315. sleep(2);
  5316. KillTask();
  5317. byte updateResult = CheckUpdateProcess();
  5318. if (updateResult == PASS)
  5319. {
  5320. DEBUG_INFO_MSG("Backend : update complete. \n");
  5321. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5322. }
  5323. else if (updateResult == MODELNAME_FAIL)
  5324. {
  5325. DEBUG_INFO_MSG("Backend : model name is none match. \n");
  5326. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5327. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5328. sleep(5);
  5329. KillAllTask();
  5330. system("/usr/bin/run_evse_restart.sh");
  5331. return;
  5332. }
  5333. else
  5334. {
  5335. DEBUG_INFO_MSG("Backend : update fail. \n");
  5336. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5337. }
  5338. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5339. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  5340. sleep(5);
  5341. system("reboot -f");
  5342. }
  5343. }
  5344. }
  5345. }
  5346. bool ocpp_chk_invalid_id_cmd(byte gunIndex)
  5347. {
  5348. byte acDirIndex = 0;
  5349. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5350. if (gunIndex == 1)
  5351. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5352. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5353. {
  5354. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  5355. {
  5356. if (strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Blocked") == EQUAL ||
  5357. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Expired") == EQUAL ||
  5358. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex + acDirIndex].ResponseIdTagInfo.Status, "Invalid") == EQUAL)
  5359. return true;
  5360. }
  5361. }
  5362. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5363. {
  5364. if (strstr((char *) ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE"))
  5365. {
  5366. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Blocked") == EQUAL ||
  5367. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Expired") == EQUAL ||
  5368. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Invalid") == EQUAL ||
  5369. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NoCredit") == EQUAL ||
  5370. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAllowedTypeEVSE") == EQUAL ||
  5371. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAtThisLocation") == EQUAL ||
  5372. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "NotAtThisTime") == EQUAL ||
  5373. strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].Response_idTokenInfo.status, "Unknown") == EQUAL)
  5374. return true;
  5375. }
  5376. }
  5377. return false;
  5378. }
  5379. bool ocpp_chk_profileConf_cmd(byte gunIndex)
  5380. {
  5381. byte acDirIndex = 0;
  5382. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5383. if (gunIndex == 1)
  5384. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5385. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5386. {
  5387. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == YES)
  5388. {
  5389. ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf = NO;
  5390. return true;
  5391. }
  5392. }
  5393. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5394. {
  5395. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == YES)
  5396. {
  5397. ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf = NO;
  5398. return true;
  5399. }
  5400. }
  5401. return false;
  5402. }
  5403. void ocpp_chk_notificationMessage_cmd(byte gunIndex)
  5404. {
  5405. byte acDirIndex = 0;
  5406. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5407. if (gunIndex == 1)
  5408. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5409. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5410. {
  5411. if (ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].TriggerMessageReq == YES)
  5412. {
  5413. if (chargingInfo[gunIndex]->Type == _Type_Chademo &&
  5414. (chargingInfo[gunIndex]->SystemStatus == SYS_MODE_IDLE ||
  5415. chargingInfo[gunIndex]->SystemStatus ==SYS_MODE_RESERVATION ||
  5416. chargingInfo[gunIndex]->SystemStatus ==SYS_MODE_MAINTAIN))
  5417. {
  5418. if (ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag == NO)
  5419. {
  5420. PRINTF_FUNC("Trigger msg (StatusNotification) = %d (chademo) \n", gunIndex);
  5421. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = 0;
  5422. GetTimespecFunc(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  5423. ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag = YES;
  5424. }
  5425. }
  5426. else if (chargingInfo[gunIndex]->Type == _Type_CCS ||
  5427. chargingInfo[gunIndex]->Type == _Type_GB ||
  5428. chargingInfo[gunIndex]->Type == _Type_AC)
  5429. {
  5430. PRINTF_FUNC("Trigger msg (StatusNotification) = %d (ccs) \n", gunIndex);
  5431. }
  5432. ShmOCPP16Data->CsMsg.bits[gunIndex + acDirIndex].TriggerMessageReq = NO;
  5433. }
  5434. }
  5435. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5436. {
  5437. }
  5438. if (ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag)
  5439. {
  5440. int _timeBuf = GetTimeoutValue(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  5441. if (_timeBuf < 0)
  5442. {
  5443. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = 0;
  5444. GetTimespecFunc(&ShmDcCommonData->_cha_try_communication[gunIndex].detectTimer);
  5445. }
  5446. if (_timeBuf > 30 ||
  5447. (_timeBuf > 2 &&
  5448. chargingInfo[gunIndex]->ConnectorPlugIn))
  5449. {
  5450. PRINTF_FUNC("Trigger msg - timeout or Plugin. \n");
  5451. ShmDcCommonData->_cha_try_communication[gunIndex].try2CommunicationFlag = NO;
  5452. }
  5453. else
  5454. ShmDcCommonData->_cha_try_communication[gunIndex].elapsedTime = (_timeBuf / uSEC_VAL);
  5455. }
  5456. }
  5457. void ocpp_lift_profileReq_cmd(byte gunIndex)
  5458. {
  5459. byte acDirIndex = 0;
  5460. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5461. if (gunIndex == 1)
  5462. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5463. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5464. {
  5465. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == NO)
  5466. {
  5467. if (ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq == NO)
  5468. ShmOCPP16Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq = YES;
  5469. }
  5470. }
  5471. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5472. {
  5473. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileConf == NO)
  5474. {
  5475. if (ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq == NO)
  5476. ShmOCPP20Data->CSUMsg.bits[gunIndex + acDirIndex].ChargingProfileReq = YES;
  5477. }
  5478. }
  5479. }
  5480. void ocpp_set_startTransation_cmd(byte gunIndex)
  5481. {
  5482. byte _OcppGunIndex = gunIndex;
  5483. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  5484. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  5485. _OcppGunIndex = 2;
  5486. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5487. {
  5488. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  5489. strcpy((char *)chargingInfo[gunIndex]->StartUserId, (char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  5490. else
  5491. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  5492. PRINTF_FUNC("IdTag = %s \n", chargingInfo[gunIndex]->StartUserId);
  5493. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  5494. }
  5495. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5496. {
  5497. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  5498. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  5499. else
  5500. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)chargingInfo[gunIndex]->StartUserId);
  5501. PRINTF_FUNC("IdTag = %s \n", ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  5502. ShmOCPP20Data->CpMsg.bits[_OcppGunIndex].TransactionEventReq = YES;
  5503. }
  5504. }
  5505. void ocpp_set_stopTransation_cmd(byte gunIndex)
  5506. {
  5507. byte _OcppGunIndex = gunIndex;
  5508. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  5509. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  5510. _OcppGunIndex = 2;
  5511. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5512. {
  5513. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  5514. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  5515. else
  5516. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  5517. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  5518. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  5519. }
  5520. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5521. {
  5522. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  5523. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  5524. else
  5525. strcpy((char *)ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken, (char *)chargingInfo[gunIndex]->StartUserId);
  5526. PRINTF_FUNC("IdTag = %s \n", ShmOCPP20Data->TransactionEvent[_OcppGunIndex].idToken.idToken);
  5527. ShmOCPP20Data->CpMsg.bits[_OcppGunIndex].TransactionEventReq = YES;
  5528. }
  5529. }
  5530. void ocpp_set_noErrorCode_cmd(byte gunIndex, char *errString)
  5531. {
  5532. // 參數 gunIndex 已包含 AC 槍
  5533. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5534. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, errString);
  5535. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5536. {
  5537. }
  5538. }
  5539. void ocpp_set_stopReason_by_cmd(byte gunIndex, char *reason)
  5540. {
  5541. byte acDirIndex = 0;
  5542. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5543. if (gunIndex == 1)
  5544. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5545. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5546. {
  5547. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, "") == EQUAL)
  5548. {
  5549. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex + acDirIndex].StopReason, reason);
  5550. }
  5551. }
  5552. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5553. {
  5554. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, "") == EQUAL)
  5555. {
  5556. strcpy((char *)ShmOCPP20Data->TransactionEvent[gunIndex + acDirIndex].transactionInfo.stoppedReason, reason);
  5557. }
  5558. }
  5559. }
  5560. void ocpp_set_authorizeReq_cmd(byte value)
  5561. {
  5562. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5563. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = value;
  5564. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5565. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = value;
  5566. }
  5567. void ocpp_set_authorizeConf_cmd(byte value)
  5568. {
  5569. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5570. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = value;
  5571. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5572. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = value;
  5573. }
  5574. void ocpp_set_errCode_cmd(byte gunIndex)
  5575. {
  5576. byte acDirIndex = 0;
  5577. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5578. if (gunIndex == 1)
  5579. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5580. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5581. {
  5582. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  5583. {
  5584. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "InternalError");
  5585. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, (char *)chargingInfo[gunIndex]->ConnectorAlarmCode);
  5586. }
  5587. else if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  5588. {
  5589. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "OtherError");
  5590. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, (char *)chargingInfo[gunIndex]->EvConnAlarmCode);
  5591. }
  5592. else
  5593. {
  5594. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError");
  5595. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "");
  5596. }
  5597. }
  5598. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5599. {
  5600. }
  5601. }
  5602. void ocpp_clear_errorCode_cmd(byte gunIndex)
  5603. {
  5604. byte acDirIndex = 0;
  5605. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5606. if (gunIndex == 1)
  5607. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5608. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5609. {
  5610. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError") != EQUAL)
  5611. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].ErrorCode, "NoError");
  5612. if (strcmp((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "") != EQUAL)
  5613. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex + acDirIndex].VendorErrorCode, "");
  5614. }
  5615. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5616. {
  5617. }
  5618. }
  5619. void ocpp_clear_idTag_cmd(byte gun_index)
  5620. {
  5621. byte acDirIndex = 0;
  5622. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5623. if (gun_index == 1)
  5624. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5625. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5626. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index + acDirIndex].ResponseIdTagInfo.Status, "");
  5627. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5628. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].Response_idTokenInfo.status, "");
  5629. }
  5630. void ocpp_clear_stopReason(byte gun_index)
  5631. {
  5632. byte acDirIndex = 0;
  5633. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5634. if (gun_index == 1)
  5635. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5636. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5637. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index + acDirIndex].StopReason, "");
  5638. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5639. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index + acDirIndex].transactionInfo.stoppedReason, "");
  5640. }
  5641. unsigned short _ocpp_get_connect_timeout()
  5642. {
  5643. uint16_t result = (TIMEOUT_SPEC_HANDSHAKING / 1000);
  5644. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5645. {
  5646. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  5647. result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  5648. }
  5649. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5650. {
  5651. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
  5652. result = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  5653. }
  5654. return result;
  5655. }
  5656. bool ocpp_get_authorize_conf()
  5657. {
  5658. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5659. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  5660. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5661. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  5662. return false;
  5663. }
  5664. bool ocpp_get_startTransation_req(byte gunIndex)
  5665. {
  5666. bool result = false;
  5667. byte acDirIndex = 0;
  5668. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5669. if (gunIndex == 1)
  5670. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5671. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  5672. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5673. {
  5674. result = ShmOCPP16Data->CpMsg.bits[gunIndex + acDirIndex].StartTransactionReq;
  5675. }
  5676. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5677. {
  5678. result = ShmOCPP20Data->CpMsg.bits[gunIndex + acDirIndex].TransactionEventReq;
  5679. }
  5680. return result;
  5681. }
  5682. void ocpp_parsing_DT_defaultPrice_cmd()
  5683. {
  5684. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5685. {
  5686. if(strcmp(MyDefaultPriceString, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData) != EQUAL)
  5687. {
  5688. DefaultPriceHandlerOcpp16((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  5689. memcpy(MyDefaultPriceString, ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, 128);
  5690. }
  5691. }
  5692. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5693. {
  5694. //ShmOCPP20Data->CostUpdated
  5695. }
  5696. }
  5697. void ocpp_parsing_DT_userPrice_cmd()
  5698. {
  5699. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5700. {
  5701. if (strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken) > 0 &&
  5702. strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.price) > 0)
  5703. {
  5704. UserPriceHandlerOcpp16((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken,
  5705. (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  5706. //memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  5707. }
  5708. }
  5709. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5710. {
  5711. //ShmOCPP20Data->CostUpdated
  5712. }
  5713. }
  5714. void ocpp_parsing_DT_running_final_cmd()
  5715. {
  5716. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5717. {
  5718. RunningFinalCostHandlerOcpp16();
  5719. }
  5720. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5721. {
  5722. //ShmOCPP20Data->CostUpdated
  5723. }
  5724. }
  5725. void ocpp_chargingProfile_process(byte _index)
  5726. {
  5727. int _time = 0;
  5728. int _startCount = NO_DEFINE;
  5729. int _maxCount = 0;
  5730. byte acDirIndex = 0;
  5731. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1 (只會影響右槍所以考慮右槍狀況)
  5732. if (_index == 1)
  5733. acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  5734. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5735. {
  5736. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingProfileKind, "Absolute") == EQUAL &&
  5737. ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingProfileId == YES)
  5738. {
  5739. _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.StartSchedule);
  5740. _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod);
  5741. _startCount = NO_DEFINE;
  5742. for (byte _count = 0; _count < _maxCount; _count++)
  5743. {
  5744. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  5745. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  5746. {
  5747. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  5748. ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  5749. {
  5750. _startCount = _count;
  5751. }
  5752. }
  5753. }
  5754. PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount);
  5755. if (_startCount < _maxCount)
  5756. {
  5757. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->SmartChargingProfile[_index + acDirIndex].ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  5758. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  5759. chargingInfo[_index]->PresentChargingVoltage > 0)
  5760. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  5761. else
  5762. chargingInfo[_index]->ChargingProfileCurrent = 0;
  5763. }
  5764. else
  5765. {
  5766. chargingInfo[_index]->ChargingProfilePower = -1;
  5767. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5768. }
  5769. }
  5770. else
  5771. {
  5772. chargingInfo[_index]->ChargingProfilePower = -1;
  5773. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5774. }
  5775. if (strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == EQUAL &&
  5776. ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES)
  5777. {
  5778. _time = GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  5779. _maxCount = ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  5780. _startCount = NO_DEFINE;
  5781. for (byte _count = 0; _count < _maxCount; _count++)
  5782. {
  5783. if (_time >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod)
  5784. {
  5785. if ((_count == 0 && ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  5786. ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  5787. {
  5788. _startCount = _count;
  5789. }
  5790. }
  5791. }
  5792. PRINTF_FUNC("Max Profile Limit = %f \n", ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  5793. if (_startCount < _maxCount)
  5794. {
  5795. float maxChargingProfilePow = ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL * ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[_startCount].NumberPhases;
  5796. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  5797. maxChargingProfilePow /= 2;
  5798. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = maxChargingProfilePow;
  5799. }
  5800. else
  5801. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  5802. }
  5803. else
  5804. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  5805. }
  5806. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5807. {
  5808. if (strcmp((char *)ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingProfileKind, "Absolute") == EQUAL &&
  5809. ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].id == YES)
  5810. {
  5811. _time = GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].startSchedule);
  5812. _maxCount = ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod);
  5813. _startCount = NO_DEFINE;
  5814. for (byte _count = 0; _count < _maxCount; _count++)
  5815. {
  5816. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  5817. if (_time >= ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].startPeriod)
  5818. {
  5819. if ((_count == 0 && ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].limit >= MIN_OUTPUT_CUR) ||
  5820. ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_count].limit > MIN_OUTPUT_CUR)
  5821. {
  5822. _startCount = _count;
  5823. }
  5824. }
  5825. }
  5826. PRINTF_FUNC("Gun_%d, Get Profile - Profile Index = %d \n", _index + acDirIndex, _startCount);
  5827. if (_startCount < _maxCount)
  5828. {
  5829. PRINTF_FUNC("Profile Limit = %f \n", ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit);
  5830. chargingInfo[_index]->ChargingProfilePower = ShmOCPP20Data->SmartChargingProfile[_index + acDirIndex].chargingSchedule[0].chargingSchedulePeriod[_startCount].limit * AC_OUTPUT_VOL;
  5831. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 && chargingInfo[_index]->PresentChargingVoltage > 0)
  5832. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  5833. else
  5834. chargingInfo[_index]->ChargingProfileCurrent = 0;
  5835. }
  5836. else
  5837. {
  5838. chargingInfo[_index]->ChargingProfilePower = -1;
  5839. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5840. }
  5841. }
  5842. else
  5843. {
  5844. chargingInfo[_index]->ChargingProfilePower = -1;
  5845. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5846. }
  5847. }
  5848. else
  5849. {
  5850. chargingInfo[_index]->ChargingProfilePower = -1;
  5851. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5852. }
  5853. if (chargingInfo[_index]->ChargingProfilePower > 0 &&
  5854. _ChargingProfilePwBuffer[_index] != chargingInfo[_index]->ChargingProfilePower)
  5855. {
  5856. _ChargingProfilePwBuffer[_index] = chargingInfo[_index]->ChargingProfilePower;
  5857. PRINTF_FUNC("Profile : ChargingProfilePower = %.2f \n", chargingInfo[_index]->ChargingProfilePower);
  5858. PRINTF_FUNC("Profile : ChargingProfileCurrent = %.2f \n", chargingInfo[_index]->ChargingProfileCurrent);
  5859. }
  5860. }
  5861. //===============================================
  5862. // wifi schedule
  5863. //===============================================
  5864. bool WifiScheduleStop(byte gunIndex)
  5865. {
  5866. bool result = false;
  5867. result = chargingInfo[gunIndex]->schedule.isTriggerStop;
  5868. chargingInfo[gunIndex]->schedule.isTriggerStop = NO;
  5869. return result;
  5870. }
  5871. //===============================================
  5872. // SQLite3 related routine
  5873. //===============================================
  5874. int NetworkDB_Open(sqlite3 *db)
  5875. {
  5876. int result = PASS;
  5877. char* errMsg = NULL;
  5878. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  5879. "idx integer primary key AUTOINCREMENT, "
  5880. "occurDatetime text NOT NULL, "
  5881. "isInternet text NOT NULL, "
  5882. "isOcppConnected text NOT NULL, "
  5883. "isEth0Internet text NOT NULL, "
  5884. "isMlan0Internet text NOT NULL, "
  5885. "isPpp0Internet text NOT NULL, "
  5886. "rssiMlan0 text NOT NULL, "
  5887. "rssiPpp0 text NOT NULL"
  5888. ");";
  5889. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  5890. if(sqlite3_open(NETWORK_DB_FILE, &db))
  5891. {
  5892. result = FAIL;
  5893. PRINTF_FUNC( "Can't open database (Net) : %s\n", sqlite3_errmsg(db));
  5894. sqlite3_close(db);
  5895. }
  5896. else
  5897. {
  5898. PRINTF_FUNC( "Local network status record database open successfully.\n");
  5899. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  5900. {
  5901. result = FAIL;
  5902. PRINTF_FUNC( "Create local network status record table error message: %s\n", errMsg);
  5903. }
  5904. else
  5905. {
  5906. PRINTF_FUNC( "Opened local network status record table successfully\n");
  5907. }
  5908. sqlite3_close(db);
  5909. }
  5910. return result;
  5911. }
  5912. int DB_Open(sqlite3 *db)
  5913. {
  5914. int result = PASS;
  5915. char* errMsg = NULL;
  5916. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  5917. "idx integer primary key AUTOINCREMENT, "
  5918. "reservationId text, "
  5919. "transactionId text, "
  5920. "startMethod text, "
  5921. "userId text, "
  5922. "dateTimeStart text, "
  5923. "dateTimeStop text,"
  5924. "socStart text, "
  5925. "socStop text, "
  5926. "chargeEnergy text, "
  5927. "stopReason text,"
  5928. "vincode text"
  5929. ");";
  5930. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  5931. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  5932. "`item` TEXT NOT NULL, "
  5933. "`connector` INTEGER NOT NULL, "
  5934. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  5935. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  5936. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  5937. "`occurDatetime` TEXT NOT NULL, "
  5938. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  5939. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  5940. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  5941. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  5942. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  5943. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  5944. "`connector` INTEGER NOT NULL, "
  5945. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  5946. // char * createTestSql = "CREATE TABLE IF NOT EXISTS `for_test` ( "
  5947. // "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  5948. // "`connector` INTEGER NOT NULL, "
  5949. // "`testID` TEXT, "
  5950. // "`checkStatus` INTEGER NOT NULL "
  5951. // ");";
  5952. if(sqlite3_open(DB_FILE, &db))
  5953. {
  5954. result = FAIL;
  5955. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  5956. sqlite3_close(db);
  5957. }
  5958. else
  5959. {
  5960. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  5961. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  5962. {
  5963. result = FAIL;
  5964. PRINTF_FUNC( "Create local charging record table error message: %s\n", errMsg);
  5965. }
  5966. else
  5967. {
  5968. PRINTF_FUNC( "Opened local charging record table successfully\n");
  5969. }
  5970. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  5971. {
  5972. result = FAIL;
  5973. PRINTF_FUNC( "Create local config table error message: %s\n", errMsg);
  5974. }
  5975. else
  5976. {
  5977. PRINTF_FUNC( "Opened local config table successfully\n");
  5978. }
  5979. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  5980. {
  5981. result = FAIL;
  5982. PRINTF_FUNC( "Create local record table error message: %s\n", errMsg);
  5983. }
  5984. else
  5985. {
  5986. PRINTF_FUNC( "Opened local record table successfully\n");
  5987. }
  5988. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  5989. {
  5990. result = FAIL;
  5991. PRINTF_FUNC( "Create reboot record table error message: %s\n", errMsg);
  5992. }
  5993. else
  5994. {
  5995. PRINTF_FUNC( "Opened reboot record table successfully\n");
  5996. }
  5997. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  5998. {
  5999. result = FAIL;
  6000. PRINTF_FUNC( "Create power consumption record table error message: %s\n", errMsg);
  6001. }
  6002. else
  6003. {
  6004. PRINTF_FUNC( "Opened power consumption record table successfully\n");
  6005. }
  6006. // if (sqlite3_exec(db, createTestSql, 0, 0, &errMsg) != SQLITE_OK)
  6007. // {
  6008. // result = FAIL;
  6009. // printf( "Create test table error message: %s\n", errMsg);
  6010. // }
  6011. // else
  6012. // {
  6013. // printf( "Opened test table successfully\n");
  6014. // }
  6015. sqlite3_close(db);
  6016. }
  6017. return result;
  6018. }
  6019. int DB_Insert_Record(sqlite3 *db, int gun_index)
  6020. {
  6021. int result = PASS;
  6022. char* errMsg = NULL;
  6023. char insertSql[1024];
  6024. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  6025. {
  6026. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, vincode) "
  6027. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  6028. chargingInfo[gun_index]->ReservationId,
  6029. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  6030. chargingInfo[gun_index]->StartMethod,
  6031. chargingInfo[gun_index]->StartUserId,
  6032. chargingInfo[gun_index]->StartDateTime,
  6033. chargingInfo[gun_index]->StopDateTime,
  6034. chargingInfo[gun_index]->EvBatteryStartSoc,
  6035. chargingInfo[gun_index]->EvBatterySoc,
  6036. chargingInfo[gun_index]->PresentChargedEnergy,
  6037. ShmOCPP16Data->StopTransaction[gun_index].StopReason,
  6038. chargingInfo[gun_index]->EVCCID);
  6039. }
  6040. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  6041. {
  6042. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  6043. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  6044. chargingInfo[gun_index]->ReservationId,
  6045. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  6046. chargingInfo[gun_index]->StartMethod,
  6047. chargingInfo[gun_index]->StartUserId,
  6048. chargingInfo[gun_index]->StartDateTime,
  6049. chargingInfo[gun_index]->StopDateTime,
  6050. chargingInfo[gun_index]->EvBatteryStartSoc,
  6051. chargingInfo[gun_index]->EvBatterySoc,
  6052. chargingInfo[gun_index]->PresentChargedEnergy,
  6053. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  6054. }
  6055. if(sqlite3_open(DB_FILE, &db))
  6056. {
  6057. result = FAIL;
  6058. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6059. sqlite3_close(db);
  6060. }
  6061. else
  6062. {
  6063. PRINTF_FUNC( "Local charging record database open successfully.\n");
  6064. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6065. {
  6066. result = FAIL;
  6067. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  6068. }
  6069. else
  6070. {
  6071. PRINTF_FUNC( "Insert local charging record successfully\n");
  6072. }
  6073. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  6074. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6075. {
  6076. result = FAIL;
  6077. PRINTF_FUNC( "delete local charging error message: %s\n", errMsg);
  6078. }
  6079. else
  6080. {
  6081. PRINTF_FUNC( "delete local charging record successfully\n");
  6082. }
  6083. sqlite3_close(db);
  6084. }
  6085. return result;
  6086. }
  6087. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  6088. {
  6089. uint8_t result = false;
  6090. char* errMsg = NULL;
  6091. char sqlStr[1024];
  6092. srand(time(NULL));
  6093. if(sqlite3_open(DB_FILE, &db))
  6094. {
  6095. result = FAIL;
  6096. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6097. sqlite3_close(db);
  6098. }
  6099. else
  6100. {
  6101. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  6102. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  6103. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  6104. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6105. {
  6106. result = FAIL;
  6107. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  6108. }
  6109. else
  6110. {
  6111. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  6112. }
  6113. sqlite3_close(db);
  6114. }
  6115. return result;
  6116. }
  6117. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  6118. {
  6119. uint8_t result = true;
  6120. char* errMsg = NULL;
  6121. char sqlStr[1024];
  6122. char **rs;
  6123. int rows, cols;
  6124. sprintf(sqlStr, "select * from config where item='IsAvailable' and connector=%d;", gun_index);
  6125. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  6126. if(sqlite3_open(DB_FILE, &db))
  6127. {
  6128. result = FAIL;
  6129. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6130. sqlite3_close(db);
  6131. }
  6132. else
  6133. {
  6134. PRINTF_FUNC( "Local config query database open successfully.\r\n");
  6135. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  6136. if(rows>0)
  6137. {
  6138. for(int idxRow=1;idxRow<=rows;idxRow++)
  6139. {
  6140. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  6141. {
  6142. result = false;
  6143. }
  6144. PRINTF_FUNC("Query connector-%d isOperactive: %s\r\n", gun_index, rs[(idxRow*cols)+3]);
  6145. }
  6146. }
  6147. else
  6148. {
  6149. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.\r\n", gun_index);
  6150. }
  6151. sqlite3_free_table(rs);
  6152. sqlite3_close(db);
  6153. }
  6154. return result;
  6155. }
  6156. void DB_Update_PowerConsumption(sqlite3 *db, uint8_t gun_index, float value)
  6157. {
  6158. char* errMsg = NULL;
  6159. char sqlStr[1024];
  6160. srand(time(NULL));
  6161. if(sqlite3_open(DB_FILE, &db))
  6162. {
  6163. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  6164. sqlite3_close(db);
  6165. }
  6166. else
  6167. {
  6168. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  6169. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6170. {
  6171. PRINTF_FUNC( "update power_consumption error message: %s\n", errMsg);
  6172. }
  6173. else
  6174. {
  6175. //PRINTF_FUNC("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  6176. }
  6177. sqlite3_close(db);
  6178. }
  6179. }
  6180. float DB_Get_PowerConsumption(sqlite3 *db, uint8_t gun_index)
  6181. {
  6182. float result = 0;
  6183. char* errMsg = NULL;
  6184. char sqlStr[1024];
  6185. char **rs;
  6186. int rows, cols;
  6187. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  6188. if (sqlite3_open(DB_FILE, &db))
  6189. {
  6190. result = FAIL;
  6191. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  6192. sqlite3_close(db);
  6193. }
  6194. else
  6195. {
  6196. PRINTF_FUNC("Local config query database open successfully.\r\n");
  6197. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  6198. if (rows > 0)
  6199. {
  6200. for (int idxRow = 1; idxRow <= rows; idxRow++)
  6201. {
  6202. result = atof(rs[(idxRow * cols) + 2]);
  6203. }
  6204. }
  6205. else
  6206. {
  6207. PRINTF_FUNC( "Query connector-%d fail, set default value to consumption.\r\n", gun_index);
  6208. }
  6209. sqlite3_free_table(rs);
  6210. sqlite3_close(db);
  6211. }
  6212. return result;
  6213. }
  6214. int DB_Reboot_Record(sqlite3 *db)
  6215. {
  6216. int result = PASS;
  6217. char* errMsg = NULL;
  6218. char insertSql[256];
  6219. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  6220. if(sqlite3_open(DB_FILE, &db))
  6221. {
  6222. result = FAIL;
  6223. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6224. sqlite3_close(db);
  6225. }
  6226. else
  6227. {
  6228. PRINTF_FUNC( "Local charging record database open successfully.\n");
  6229. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6230. {
  6231. result = FAIL;
  6232. PRINTF_FUNC( "Insert reboot record error message: %s\n", errMsg);
  6233. }
  6234. else
  6235. {
  6236. PRINTF_FUNC( "Insert reboot record successfully\n");
  6237. }
  6238. sprintf(insertSql, "delete from reboot_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  6239. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6240. {
  6241. result = FAIL;
  6242. PRINTF_FUNC( "delete reboot record error message: %s\n", errMsg);
  6243. }
  6244. else
  6245. {
  6246. PRINTF_FUNC( "delete reboot record successfully\n");
  6247. }
  6248. sqlite3_close(db);
  6249. }
  6250. return result;
  6251. }
  6252. int DB_Insert_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  6253. {
  6254. int result = PASS;
  6255. char* errMsg = NULL;
  6256. char insertSql[1024];
  6257. sprintf(insertSql, "insert into for_test (connector, testID, checkStatus) values(%d, %s, 0);",
  6258. gun_index, textID);
  6259. if(sqlite3_open(DB_FILE, &db))
  6260. {
  6261. result = FAIL;
  6262. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  6263. sqlite3_close(db);
  6264. }
  6265. else
  6266. {
  6267. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6268. {
  6269. result = FAIL;
  6270. PRINTF_FUNC( "Insert sdlu test error message : %s\n", errMsg);
  6271. }
  6272. else
  6273. {
  6274. PRINTF_FUNC( "Insert sdlu test successfully\n");
  6275. }
  6276. sqlite3_close(db);
  6277. }
  6278. return result;
  6279. }
  6280. int DB_delete_test_Record(sqlite3 *db, uint8_t gun_index, char *textID)
  6281. {
  6282. int result = PASS;
  6283. char* errMsg = NULL;
  6284. char insertSql[1024];
  6285. sprintf(insertSql, "delete from for_test where testID = %s", textID);
  6286. if (sqlite3_open(DB_FILE, &db))
  6287. {
  6288. result = FAIL;
  6289. PRINTF_FUNC("Can't open database: %s\n", sqlite3_errmsg(db));
  6290. sqlite3_close(db);
  6291. }
  6292. else
  6293. {
  6294. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6295. {
  6296. result = FAIL;
  6297. PRINTF_FUNC( "Test message 2: %s\n", errMsg);
  6298. }
  6299. else
  6300. {
  6301. PRINTF_FUNC( "Test message 2 successfully\n");
  6302. }
  6303. sqlite3_close(db);
  6304. }
  6305. return result;
  6306. }
  6307. int DB_sql_test(sqlite3 *db, uint8_t gun_index)
  6308. {
  6309. int result = PASS;
  6310. char* errMsg = NULL;
  6311. char insertSql[1024];
  6312. char valueStr[128];
  6313. char **rs;
  6314. int rows, cols;
  6315. sprintf(insertSql, "select testID from for_test where checkStatus=0;");
  6316. if (sqlite3_open(DB_FILE, &db))
  6317. {
  6318. result = FAIL;
  6319. PRINTF_FUNC("Can't open database: %s\r\n", sqlite3_errmsg(db));
  6320. sqlite3_close(db);
  6321. }
  6322. else
  6323. {
  6324. PRINTF_FUNC("Local for_test query database open successfully.\r\n");
  6325. sqlite3_get_table(db, insertSql, &rs, &rows, &cols, &errMsg);
  6326. if (rows > 0)
  6327. {
  6328. for (int idxRow = 1; idxRow <= rows; idxRow++)
  6329. {
  6330. strcpy(valueStr, rs[(idxRow * cols)]);
  6331. printf("valueStr = %s \n", valueStr);
  6332. }
  6333. }
  6334. else
  6335. {
  6336. PRINTF_FUNC("select testID from for_test fail \r\n");
  6337. }
  6338. sqlite3_free_table(rs);
  6339. sqlite3_close(db);
  6340. }
  6341. return result;
  6342. }
  6343. //===============================================
  6344. // Config process
  6345. //===============================================
  6346. void AddPlugInTimes(byte gunIndex)
  6347. {
  6348. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  6349. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  6350. else if(chargingInfo[gunIndex]->Type == _Type_CCS)
  6351. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  6352. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  6353. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  6354. }
  6355. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  6356. {
  6357. char cmdBuf[32];
  6358. struct timeb csuTime;
  6359. struct tm *tmCSU;
  6360. ftime(&csuTime);
  6361. tmCSU = localtime(&csuTime.time);
  6362. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  6363. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  6364. tmCSU->tm_sec);
  6365. if (isStart)
  6366. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  6367. else
  6368. {
  6369. if (strcmp((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00") != EQUAL)
  6370. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  6371. }
  6372. }
  6373. void zipLogFiles()
  6374. {
  6375. const char* logPath = "/Storage/SystemLog";
  6376. // 獲取目錄
  6377. DIR* pDir = opendir(logPath);
  6378. if (pDir != NULL)
  6379. {
  6380. struct timeb csuTime;
  6381. struct tm *tmCSU;
  6382. ftime(&csuTime);
  6383. tmCSU = localtime(&csuTime.time);
  6384. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  6385. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  6386. // tmCSU->tm_sec);
  6387. // Read items inside the folder
  6388. struct dirent* pEntry = NULL;
  6389. while ((pEntry = readdir(pDir)) != NULL)
  6390. {
  6391. if (strcmp(pEntry->d_name, ".") != 0 &&
  6392. strcmp(pEntry->d_name, "..") != 0 &&
  6393. strncmp(pEntry->d_name, "[", 1) == 0 &&
  6394. strstr(pEntry->d_name, "tar") < 0)
  6395. {
  6396. char yearC[5];
  6397. unsigned short year = 0;
  6398. char monthC[3];
  6399. unsigned short month = 0;
  6400. yearC[4] = '\0';
  6401. strncpy(yearC, pEntry->d_name + 1, 4);
  6402. monthC[2] = '\0';
  6403. strncpy(monthC, pEntry->d_name + 6, 2);
  6404. year = atoi(yearC);
  6405. month = atoi(monthC);
  6406. if (year != 0)
  6407. {
  6408. if (year < tmCSU->tm_year + 1900 ||
  6409. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  6410. {
  6411. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  6412. char file[256];
  6413. memset(file, 0x00, sizeof(file));
  6414. strcat(file, "tar zcvf ");
  6415. strcat(file, logPath);
  6416. strncat(file, "/", 1);
  6417. strcat(file, pEntry->d_name);
  6418. strcat(file, ".tar");
  6419. strncat(file, " ", 1);
  6420. strcat(file, logPath);
  6421. strncat(file, "/", 1);
  6422. strcat(file, pEntry->d_name);
  6423. PRINTF_FUNC("zip = %s \n", file);
  6424. system(file);
  6425. }
  6426. }
  6427. }
  6428. }
  6429. }
  6430. // Close folder
  6431. closedir(pDir);
  6432. }
  6433. void ChangeGunSelectByIndex(byte sel)
  6434. {
  6435. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  6436. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  6437. }
  6438. void CheckIsAlternatvieByModelName()
  6439. {
  6440. if(ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'W')
  6441. {
  6442. // 壁掛
  6443. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  6444. }
  6445. else
  6446. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  6447. }
  6448. void StopProcessingLoop()
  6449. {
  6450. ocpp_process_start();
  6451. for (;;)
  6452. {
  6453. CheckTask();
  6454. CheckFactoryConfigFunction();
  6455. CheckFwUpdateFunction();
  6456. ocpp_chk_reset_cmd(YES);
  6457. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  6458. {
  6459. // 經過底下的 function 後,ShmSysConfigAndInfo->SysWarningInfo.Level 會再被更新一次
  6460. ChkPrimaryStatus();
  6461. ReviewCriticalAlarm();
  6462. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_NORMAL &&
  6463. ShmDcCommonData->rebootCount < 1)
  6464. {
  6465. PRINTF_FUNC("Soft reboot for retry self-test (Primary). \n");
  6466. SetupRebootCount(1);
  6467. KillAllTask();
  6468. sleep(3);
  6469. system("/usr/bin/run_evse_restart.sh");
  6470. return;
  6471. }
  6472. }
  6473. sleep(1);
  6474. }
  6475. }
  6476. void CreateWatchdog()
  6477. {
  6478. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  6479. {
  6480. wtdFd = InitWatchDog();
  6481. if (wtdFd < 0)
  6482. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  6483. }
  6484. }
  6485. bool IsConnectorWholeIdle()
  6486. {
  6487. bool result = true;
  6488. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  6489. {
  6490. if (chargingInfo[count]->SystemStatus != SYS_MODE_IDLE &&
  6491. chargingInfo[count]->SystemStatus != SYS_MODE_RESERVATION)
  6492. {
  6493. result = false;
  6494. break;
  6495. }
  6496. }
  6497. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  6498. {
  6499. if (ac_chargingInfo[count]->SystemStatus != SYS_MODE_IDLE &&
  6500. ac_chargingInfo[count]->IsErrorOccur == NO)
  6501. {
  6502. result = false;
  6503. break;
  6504. }
  6505. }
  6506. return result;
  6507. }
  6508. void ClearAlarmCodeWhenAcOff()
  6509. {
  6510. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  6511. {
  6512. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  6513. }
  6514. }
  6515. //==========================================
  6516. // Check task processing
  6517. //==========================================
  6518. void CheckTask()
  6519. {
  6520. if(reset4gStep == RESET_4G_STEP_NONE &&
  6521. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' ||
  6522. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  6523. {
  6524. if(system("pidof -s Module_4g > /dev/null") != 0)
  6525. {
  6526. DEBUG_ERROR_MSG("Module_4g not running, restart it.\r\n");
  6527. system("/root/Module_4g &");
  6528. }
  6529. }
  6530. if(reset4gStep == RESET_4G_STEP_NONE &&
  6531. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' ||
  6532. ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  6533. {
  6534. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  6535. {
  6536. DEBUG_ERROR_MSG("Module_Wifi not running, restart it.\r\n");
  6537. system("/root/Module_Wifi &");
  6538. }
  6539. }
  6540. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'P')
  6541. {
  6542. if(system("pidof -s Module_DcMeter > /dev/null") != 0)
  6543. {
  6544. DEBUG_ERROR_MSG("Module_DcMeter not running, restart it.\r\n");
  6545. system("/root/Module_DcMeter &");
  6546. }
  6547. }
  6548. ocpp_process_start();
  6549. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  6550. {
  6551. DEBUG_ERROR_MSG("Module_ProduceUtils not running, restart it.\r\n");
  6552. system ("/root/Module_ProduceUtils &");
  6553. }
  6554. }
  6555. void CheckSystemTaskAlive()
  6556. {
  6557. unsigned char lostId = CheckSystemTask(ShmSysConfigAndInfo->SysInfo.SystemPage);
  6558. if (lostId != 0)
  6559. {
  6560. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost == NO)
  6561. {
  6562. EmcOccureThenStopCharging();
  6563. sleep(2);
  6564. if (lostId == _SYSTEM_TASK_LOST_ITEM_MAIN) PRINTF_FUNC("System task lost (CSU). \n");
  6565. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVCOMM)
  6566. {
  6567. PRINTF_FUNC("System task lost (EVComm). \n");
  6568. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  6569. {
  6570. chargingInfo[gun]->ConnectorPlugIn = NO;
  6571. }
  6572. }
  6573. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PSUCOMM) PRINTF_FUNC("System task lost (PSU Task). \n");
  6574. else if (lostId == _SYSTEM_TASK_LOST_ITEM_EVENTLOG) PRINTF_FUNC("System task lost (Event log). \n");
  6575. else if (lostId == _SYSTEM_TASK_LOST_ITEM_PRIMARYCOMM) PRINTF_FUNC("System task lost (Primary). \n");
  6576. else if (lostId == _SYSTEM_TASK_LOST_ITEM_LCMCONTROL) PRINTF_FUNC("System task lost (LCM Comm). \n");
  6577. else if (lostId == _SYSTEM_TASK_LOST_ITEM_INTERCOMM) PRINTF_FUNC("System task lost (Internal Comm). \n");
  6578. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = YES;
  6579. }
  6580. }
  6581. else
  6582. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost = NO;
  6583. }
  6584. void InitialDHCP()
  6585. {
  6586. char tmpbuf[256];
  6587. memset(tmpbuf,0,256);
  6588. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  6589. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  6590. system(tmpbuf);
  6591. }
  6592. void Run4gResetProc()
  6593. {
  6594. if (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == _SYS_4G_MODE_ENABLE ||
  6595. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_STATION)
  6596. {
  6597. if (!ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  6598. !ShmSysConfigAndInfo->SysInfo.InternetConn)
  6599. {
  6600. if (reset4gStep == RESET_4G_STEP_NONE)
  6601. {
  6602. if ((ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES && _4gResetChkFlag >= 12) ||
  6603. _4gResetChkFlag >= 60)
  6604. {
  6605. PRINTF_FUNC("Reset 4G/Wifi module. \n");
  6606. reset4gStep = RESET_4G_STEP_DELETE_TASK;
  6607. }
  6608. else
  6609. _4gResetChkFlag++;
  6610. }
  6611. }
  6612. else
  6613. _4gResetChkFlag = 0;
  6614. switch(reset4gStep)
  6615. {
  6616. case RESET_4G_STEP_DELETE_TASK:
  6617. {
  6618. PRINTF_FUNC("Killall 4G / Wifi task. \n");
  6619. system("killall Module_4g &");
  6620. system("killall Module_Wifi &");
  6621. reset4gStep = RESET_4G_STEP_GPIO_HIGH;
  6622. }
  6623. break;
  6624. case RESET_4G_STEP_GPIO_HIGH:
  6625. {
  6626. PRINTF_FUNC("Disable 4G/Wifi. GPIO-104 -> High. \n");
  6627. system("echo 1 > /sys/class/gpio/gpio104/value");
  6628. reset4gStep = RESET_4G_STEP_GPIO_LOW_COMP;
  6629. }
  6630. break;
  6631. case RESET_4G_STEP_GPIO_LOW_COMP:
  6632. {
  6633. PRINTF_FUNC("Enable 4G/Wifi. GPIO-104 -> Low. \n");
  6634. system("echo 0 > /sys/class/gpio/gpio104/value");
  6635. reset4gStep = RESET_4G_STEP_NONE;
  6636. _4gResetChkFlag = 0;
  6637. }
  6638. break;
  6639. }
  6640. }
  6641. else
  6642. {
  6643. if (reset4gStep != RESET_4G_STEP_NONE)
  6644. system("echo 0 > /sys/class/gpio/gpio104/value");
  6645. reset4gStep = RESET_4G_STEP_NONE;
  6646. _4gResetChkFlag = 0;
  6647. }
  6648. }
  6649. //==========================================
  6650. // Check Smart Charging Profile
  6651. //==========================================
  6652. int GetStartScheduleTime(unsigned char *time)
  6653. {
  6654. int result = -1;
  6655. struct tm tmScheduleStart;
  6656. struct timeb tbScheduleStart;
  6657. 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))
  6658. {
  6659. tmScheduleStart.tm_year -= 1900;
  6660. tmScheduleStart.tm_mon -= 1;
  6661. tbScheduleStart.time = mktime(&tmScheduleStart);
  6662. tbScheduleStart.millitm = 0;
  6663. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  6664. }
  6665. return result;
  6666. }
  6667. //==========================================
  6668. // sub function
  6669. //==========================================
  6670. void CheckReturnToChargingConn()
  6671. {
  6672. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  6673. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  6674. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  6675. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  6676. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  6677. {
  6678. bool isReturnTimeout = false;
  6679. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  6680. {
  6681. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  6682. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  6683. {
  6684. if ((chargingInfo[count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= SYS_MODE_COMPLETE) ||
  6685. (chargingInfo[count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  6686. {
  6687. isReturnTimeout = false;
  6688. break;
  6689. }
  6690. }
  6691. else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  6692. {
  6693. if ((chargingInfo[count]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= SYS_MODE_COMPLETE) ||
  6694. (chargingInfo[count]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[count]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  6695. {
  6696. isReturnTimeout = true;
  6697. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  6698. }
  6699. }
  6700. }
  6701. // AC 槍
  6702. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  6703. {
  6704. // 沒有選中 AC,且 AC 在充電中
  6705. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  6706. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_COMPLETE))
  6707. {
  6708. // 當前 DC 充電槍在 idle 狀態
  6709. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_IDLE ||
  6710. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == SYS_MODE_RESERVATION)
  6711. {
  6712. isReturnTimeout = true;
  6713. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  6714. }
  6715. }
  6716. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  6717. ((chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_COMPLETE) ||
  6718. (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)))
  6719. {
  6720. // 當前 DC 充電槍在 idle 狀態
  6721. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE ||
  6722. ac_chargingInfo[0]->SystemStatus == SYS_MODE_RESERVATION)
  6723. {
  6724. isReturnTimeout = true;
  6725. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  6726. }
  6727. }
  6728. }
  6729. if (!isReturnTimeout)
  6730. StopSystemTimeoutDet();
  6731. }
  6732. }
  6733. bool GetStartChargingByAlterMode(byte _gun)
  6734. {
  6735. bool result = true;
  6736. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2 &&
  6737. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  6738. {
  6739. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select++)
  6740. {
  6741. if (_select != _gun)
  6742. {
  6743. if (chargingInfo[_select]->SystemStatus != SYS_MODE_IDLE &&
  6744. chargingInfo[_select]->SystemStatus != SYS_MODE_RESERVATION)
  6745. {
  6746. result = false;
  6747. break;
  6748. }
  6749. }
  6750. }
  6751. }
  6752. return result;
  6753. }
  6754. void TheEndCharging(byte gun_index)
  6755. {
  6756. chargingInfo[gun_index]->isRemoteStart = NO;
  6757. StopGunInfoTimeoutDet(gun_index);
  6758. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  6759. ChangeStartOrStopDateTime(NO, gun_index);
  6760. DB_Insert_Record(localDb, gun_index);
  6761. }
  6762. void AdjustChargerCurrent()
  6763. {
  6764. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  6765. // 設定的電流~ 如超過可輸出的電流,則 bypass
  6766. if (ShmSysConfigAndInfo->SysConfig.RatingCurrent < ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent ||
  6767. ShmSysConfigAndInfo->SysConfig.RatingCurrent == 0)
  6768. {
  6769. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent = 0;
  6770. }
  6771. PRINTF_FUNC ("PSU : MaxChargingPower = %d, MaxChargingCurrent = %d",
  6772. ShmPsuData->SystemAvailablePower / 10,
  6773. ShmPsuData->SystemAvailableCurrent / 10
  6774. );
  6775. PRINTF_FUNC ("Config : ChargingPower = %d, ChargingCurrent = %d",
  6776. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  6777. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent
  6778. );
  6779. }
  6780. void CheckStatusOfMeter()
  6781. {
  6782. // 橋接如果搭上~ 則兩個電表都是使用狀態
  6783. // 如橋接沒有搭上~ 則視該火線 relay 狀態為電表使用狀態
  6784. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  6785. {
  6786. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++)
  6787. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  6788. }
  6789. else
  6790. {
  6791. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++)
  6792. {
  6793. if (chargingInfo[mIndex]->RelayKPK2Status == YES || chargingInfo[mIndex]->RelayK1K2Status)
  6794. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  6795. else
  6796. ShmCsuMeterData->_meter[mIndex].isCalculation = NO;
  6797. }
  6798. }
  6799. }
  6800. void ReviewCriticalAlarm()
  6801. {
  6802. if (ShmDcCommonData->GunRelayDrivingOccur[0] == YES ||
  6803. ShmDcCommonData->GunRelayDrivingOccur[1] == YES ||
  6804. ShmDcCommonData->GunRelayWeldingOccur[0] == YES ||
  6805. ShmDcCommonData->GunRelayWeldingOccur[1] == YES ||
  6806. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip ||
  6807. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip ||
  6808. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen ||
  6809. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  6810. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm ||
  6811. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemTaskLost)
  6812. {
  6813. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_CRITICAL;
  6814. }
  6815. else
  6816. ShmSysConfigAndInfo->SysWarningInfo.Level = _ALARM_LEVEL_NORMAL;
  6817. }
  6818. void CheckRelayWeldingOrDrivingFault(byte gun_index)
  6819. {
  6820. // relay welding fault then stop the charging process.
  6821. byte faultCode = RELAY_STATUS_ERROR_NONE;
  6822. if (gun_index == 0)
  6823. {
  6824. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  6825. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  6826. {
  6827. faultCode = RELAY_STATUS_ERROR_WELDING;
  6828. }
  6829. else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  6830. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  6831. {
  6832. faultCode = RELAY_STATUS_ERROR_DRIVING;
  6833. }
  6834. }
  6835. else if (gun_index == 1)
  6836. {
  6837. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  6838. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  6839. {
  6840. faultCode = RELAY_STATUS_ERROR_WELDING;
  6841. }
  6842. else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  6843. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  6844. {
  6845. faultCode = RELAY_STATUS_ERROR_DRIVING;
  6846. }
  6847. }
  6848. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2 &&
  6849. !ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  6850. {
  6851. // 橋接
  6852. if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  6853. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING)
  6854. {
  6855. faultCode = RELAY_STATUS_ERROR_WELDING;
  6856. }
  6857. else if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  6858. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING)
  6859. {
  6860. faultCode = RELAY_STATUS_ERROR_DRIVING;
  6861. }
  6862. }
  6863. if (faultCode == RELAY_STATUS_ERROR_WELDING)
  6864. {
  6865. // welding
  6866. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  6867. RecordAlarmCode(gun_index, "011011");
  6868. else if (chargingInfo[gun_index]->Type == _Type_GB)
  6869. RecordAlarmCode(gun_index, "011015");
  6870. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  6871. RecordAlarmCode(gun_index, "011013");
  6872. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = YES;
  6873. EmcOccureThenStopCharging();
  6874. }
  6875. else if (faultCode == RELAY_STATUS_ERROR_DRIVING)
  6876. {
  6877. // driving
  6878. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  6879. RecordAlarmCode(gun_index, "011012");
  6880. else if (chargingInfo[gun_index]->Type == _Type_GB)
  6881. RecordAlarmCode(gun_index, "011016");
  6882. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  6883. RecordAlarmCode(gun_index, "011014");
  6884. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = YES;
  6885. EmcOccureThenStopCharging();
  6886. }
  6887. else
  6888. {
  6889. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = NO;
  6890. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = NO;
  6891. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  6892. {
  6893. ResetChargerAlarmCode(gun_index, "011012");
  6894. ResetChargerAlarmCode(gun_index, "011011");
  6895. }
  6896. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  6897. {
  6898. ResetChargerAlarmCode(gun_index, "011014");
  6899. ResetChargerAlarmCode(gun_index, "011013");
  6900. }
  6901. else if (chargingInfo[gun_index]->Type == _Type_GB)
  6902. {
  6903. ResetChargerAlarmCode(gun_index, "011016");
  6904. ResetChargerAlarmCode(gun_index, "011015");
  6905. }
  6906. }
  6907. }
  6908. bool PrecheckIsPass(byte gun_index)
  6909. {
  6910. bool result = true;
  6911. // relay welding or driving 是反向
  6912. if (result)
  6913. result = !ShmDcCommonData->GunRelayWeldingOccur[gun_index];
  6914. return result;
  6915. }
  6916. void CollectError(byte gun_index)
  6917. {
  6918. errCollect.GunErrMessage |= ShmDcCommonData->ConnectErrList[gun_index].GunErrMessage;
  6919. }
  6920. void AlarmCheck(byte type)
  6921. {
  6922. // type : Cha = 0、CCS = 1、GBT = 2
  6923. if (type == _Type_Chademo)
  6924. {
  6925. // GFD Trip
  6926. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  6927. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  6928. else
  6929. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  6930. // UVP
  6931. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  6932. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  6933. else
  6934. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  6935. // OTP
  6936. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  6937. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = YES;
  6938. else
  6939. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoConnectorOTP = NO;
  6940. // OVP
  6941. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  6942. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = YES;
  6943. else
  6944. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = NO;
  6945. // GFD Warning
  6946. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  6947. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  6948. else
  6949. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  6950. // Relay Welding
  6951. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  6952. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  6953. else
  6954. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = NO;
  6955. // Relay Driving
  6956. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  6957. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  6958. else
  6959. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  6960. // Connect temp Sensor broken
  6961. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  6962. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = YES;
  6963. else
  6964. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoConnectorTempSensorBroken = NO;
  6965. // Output UCP
  6966. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 0))
  6967. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = YES;
  6968. else
  6969. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputUCP = NO;
  6970. }
  6971. else if (type == _Type_CCS)
  6972. {
  6973. // GFD Trip
  6974. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  6975. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  6976. else
  6977. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  6978. // UVP
  6979. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  6980. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  6981. else
  6982. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  6983. // OTP
  6984. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  6985. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = YES;
  6986. else
  6987. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsConnectorOTP = NO;
  6988. // OVP
  6989. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  6990. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = YES;
  6991. else
  6992. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = NO;
  6993. // GFD Warning
  6994. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  6995. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  6996. else
  6997. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  6998. // Relay Welding
  6999. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7000. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  7001. else
  7002. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = NO;
  7003. // Relay Driving
  7004. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7005. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  7006. else
  7007. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  7008. // Connect temp Sensor broken
  7009. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7010. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = YES;
  7011. else
  7012. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsConnectorTempSensorBroken = NO;
  7013. // Output UCP
  7014. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 1))
  7015. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = YES;
  7016. else
  7017. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCCSOutputUCP = NO;
  7018. }
  7019. else if (type == _Type_GB)
  7020. {
  7021. // GFD Trip
  7022. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 0))
  7023. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  7024. else
  7025. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  7026. // UVP
  7027. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 1))
  7028. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  7029. else
  7030. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  7031. // OTP
  7032. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 2))
  7033. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = YES;
  7034. else
  7035. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbConnectorOTP = NO;
  7036. // OVP
  7037. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 3))
  7038. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = YES;
  7039. else
  7040. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = NO;
  7041. // GFD Warning
  7042. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 4))
  7043. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  7044. else
  7045. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  7046. // Relay Welding
  7047. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 5))
  7048. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  7049. else
  7050. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = NO;
  7051. // Relay Driving
  7052. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 6))
  7053. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  7054. else
  7055. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  7056. // Connect temp Sensor broken
  7057. if (DetectBitValue(errCollect.GunErrMessage >> (8 * type), 7))
  7058. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = YES;
  7059. else
  7060. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbConnectorTempSensorBroken = NO;
  7061. // Output UCP
  7062. if (DetectBitValue(errCollect.GunErrMessage >> (8 * 3), 2))
  7063. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = YES;
  7064. else
  7065. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGBTOutputUCP = NO;
  7066. }
  7067. }
  7068. void SavePresentChargedDuration(byte gun_index)
  7069. {
  7070. int _diffTime = 0;
  7071. ftime(&endChargingTime[gun_index]);
  7072. _diffTime = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  7073. // below 0 or over 5 sec is abnormal
  7074. if (_diffTime < 0 ||
  7075. _diffTime > chargingInfo[gun_index]->PresentChargedDuration + 5)
  7076. {
  7077. _presentChargingTimeBuf[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  7078. ftime(&startChargingTime[gun_index]);
  7079. PRINTF_FUNC("TimeZone changed (%d) \n", gun_index);
  7080. }
  7081. else
  7082. chargingInfo[gun_index]->PresentChargedDuration = _presentChargingTimeBuf[gun_index] + _diffTime;
  7083. }
  7084. void ClearEnergyTimePeriod(byte gun_index)
  7085. {
  7086. for (int i = 0; i < 24; i++)
  7087. {
  7088. ShmDcCommonData->energy_time_period[gun_index][i] = 0;
  7089. }
  7090. }
  7091. void ChangeToMaxChargingMode()
  7092. {
  7093. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  7094. {
  7095. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  7096. {
  7097. // 均充 -> 最大充
  7098. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  7099. {
  7100. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  7101. {
  7102. // 切換最大充起始位置
  7103. PRINTF_FUNC("======= Aver -> Max : Preparing. (Step 11) ======= \n");
  7104. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  7105. }
  7106. }
  7107. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  7108. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  7109. {
  7110. PRINTF_FUNC("======= Aver -> Max : Current Balancing. (Step 14) ======= \n");
  7111. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  7112. }
  7113. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  7114. {
  7115. PRINTF_FUNC("======= Aver -> Max : Charging mode = MAX ======= \n");
  7116. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  7117. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7118. }
  7119. }
  7120. else
  7121. {
  7122. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  7123. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7124. }
  7125. }
  7126. else
  7127. {
  7128. // 需考量如果再切換均充過程 ? (預計要進入充電的臨時取消充電)
  7129. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7130. }
  7131. }
  7132. void CheckOcppCostAndPrice()
  7133. {
  7134. ocpp_parsing_DT_defaultPrice_cmd();
  7135. ocpp_parsing_DT_userPrice_cmd();
  7136. ocpp_parsing_DT_running_final_cmd();
  7137. }
  7138. void CheckConnectorOTP(byte _index)
  7139. {
  7140. if (chargingInfo[_index]->Type == _Type_Chademo)
  7141. {
  7142. // if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  7143. // {
  7144. // ResetChargerAlarmCode(_index, "011018");
  7145. // if (chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7146. // RecordAlarmCode(_index, "012229");
  7147. // else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  7148. // chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7149. // ResetChargerAlarmCode(_index, "012229");
  7150. // }
  7151. // else
  7152. // {
  7153. // // 沒接上 Sensor or 異常
  7154. // //RecordAlarmCode(_index, "011018");
  7155. // ResetChargerAlarmCode(_index, "012229");
  7156. // }
  7157. }
  7158. else if (chargingInfo[_index]->Type == _Type_CCS)
  7159. {
  7160. // CCS 不管甚麼輸出都會有槍溫偵測!!~
  7161. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  7162. {
  7163. ResetChargerAlarmCode(_index, "011019");
  7164. if(chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7165. RecordAlarmCode(_index, "012230");
  7166. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  7167. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7168. ResetChargerAlarmCode(_index, "012230");
  7169. }
  7170. else
  7171. {
  7172. // 沒接上 Sensor or 異常
  7173. RecordAlarmCode(_index, "011019");
  7174. ResetChargerAlarmCode(_index, "012230");
  7175. }
  7176. }
  7177. else if (chargingInfo[_index]->Type == _Type_GB &&
  7178. chargingInfo[_index]->ModelType == 'B')
  7179. {
  7180. if (chargingInfo[_index]->ConnectorTemp != UNDEFINED_TEMP)
  7181. {
  7182. ResetChargerAlarmCode(_index, "011020");
  7183. if(chargingInfo[_index]->ConnectorTemp >= GUN_OTP_VALUE)
  7184. RecordAlarmCode(_index, "012231");
  7185. else if (chargingInfo[_index]->ConnectorTemp != 0 &&
  7186. chargingInfo[_index]->ConnectorTemp < GUN_OTP_RECOVERY)
  7187. ResetChargerAlarmCode(_index, "012231");
  7188. }
  7189. else
  7190. {
  7191. // 沒接上 Sensor or 異常
  7192. RecordAlarmCode(_index, "011020");
  7193. ResetChargerAlarmCode(_index, "012231");
  7194. }
  7195. }
  7196. }
  7197. void ChkConnectorBoardStop(byte index)
  7198. {
  7199. if (isEvBoardStopChargeFlag(index) == YES)
  7200. {
  7201. // 板端要求停止 (錯誤)
  7202. ocpp_set_stopReason_by_cmd(index, "EVDisconnected");
  7203. ChargingAlarmProcess(index);
  7204. }
  7205. else if (isEvBoardNormalStopChargeFlag(index) == YES)
  7206. {
  7207. // 板端要求停止 (正常)
  7208. ocpp_set_stopReason_by_cmd(index, "EVDisconnected");
  7209. ChargingTerminalProcess(index);
  7210. }
  7211. }
  7212. void ChkBackendStop(byte index)
  7213. {
  7214. if (ocpp_chk_invalid_id_cmd(index))
  7215. {
  7216. // Start Transaction Stop
  7217. ocpp_set_stopReason_by_cmd(index, "DeAuthorized");
  7218. ChargingTerminalProcess(index);
  7219. }
  7220. else if (ocpp_chk_remoteStop_cmd(index) == YES ||
  7221. WifiScheduleStop(index))
  7222. {
  7223. // 後臺要求停止
  7224. ChargingTerminalProcess(index);
  7225. }
  7226. }
  7227. void ChkConfigurationStop(byte index)
  7228. {
  7229. if (CheckBackendChargingTimeout(index) ||
  7230. CheckBackendChargingEnergy(index))
  7231. {
  7232. // 後臺要求停止
  7233. ChargingTerminalProcess(index);
  7234. }
  7235. }
  7236. void RecordGfdResult(byte index)
  7237. {
  7238. if (chargingInfo[index]->Type == _Type_Chademo)
  7239. {
  7240. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  7241. {
  7242. // GFD 錯誤停止
  7243. RecordAlarmCode(index, "012234");
  7244. }
  7245. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  7246. {
  7247. // GFD 警告
  7248. RecordAlarmCode(index, "012296");
  7249. }
  7250. }
  7251. else if (chargingInfo[index]->Type == _Type_GB)
  7252. {
  7253. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  7254. {
  7255. // GFD 錯誤停止
  7256. RecordAlarmCode(index, "012236");
  7257. }
  7258. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  7259. {
  7260. // GFD 警告
  7261. RecordAlarmCode(index, "012298");
  7262. }
  7263. }
  7264. else if (chargingInfo[index]->Type == _Type_CCS)
  7265. {
  7266. if (chargingInfo[index]->GroundFaultStatus == GFD_FAIL)
  7267. {
  7268. // GFD 錯誤停止
  7269. RecordAlarmCode(index, "012235");
  7270. }
  7271. else if (chargingInfo[index]->GroundFaultStatus == GFD_WARNING)
  7272. {
  7273. // GFD 警告
  7274. RecordAlarmCode(index, "012297");
  7275. }
  7276. }
  7277. }
  7278. int main(void)
  7279. {
  7280. if(CreateShareMemory() == 0)
  7281. {
  7282. #ifdef SystemLogMessage
  7283. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  7284. #endif
  7285. if(ShmStatusCodeData!=NULL)
  7286. {
  7287. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  7288. }
  7289. sleep(5);
  7290. system("reboot -f");
  7291. sleep(5);
  7292. system("reboot -f");
  7293. }
  7294. PRINTF_FUNC("===== Boot and Initialization start ===== \n");
  7295. if (!InitialSystemDefaultConfig())
  7296. {
  7297. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  7298. StopProcessingLoop();
  7299. }
  7300. PRINTF_FUNC("CheckConnectorTypeStatus and CheckIsAlternatvie \n");
  7301. CheckGunTypeFromHw();
  7302. CheckIsAlternatvieByModelName();
  7303. InitialShareMemoryInfo();
  7304. PRINTF_FUNC("InitialShareMemoryInfo OK. \n");
  7305. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  7306. PRINTF_FUNC("ChargerType (IEC or UL) = IEC \n");
  7307. else
  7308. PRINTF_FUNC("ChargerType (IEC or UL) = UL \n");
  7309. ChangeLcmByIndex(_LCM_INIT);
  7310. if (!CheckConnectorTypeStatus())
  7311. isModelNameMatch = false;
  7312. Initialization();
  7313. PRINTF_FUNC("===== Initial Complete ===== \n");
  7314. PRINTF_FUNC("===== Spawn all Task and self test start ===== \n");
  7315. SpawnTask();
  7316. if (!isModelNameMatch)
  7317. {
  7318. // Module Name 與硬體對應不正確
  7319. PRINTF_FUNC("*** Module Name & HW info none match : Please check if the Model name and HW are matched. ***\n");
  7320. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  7321. ChangeLcmByIndex(_LCM_FIX);
  7322. sleep(3);
  7323. KillTask();
  7324. StopProcessingLoop();
  7325. }
  7326. PRINTF_FUNC("Module Name & HW info correct. \n");
  7327. CreateTimeoutFork();
  7328. if (!DEBUG_ALSTON)
  7329. {
  7330. PRINTF_FUNC("Self test run..\n");
  7331. SelfTestRun();
  7332. }
  7333. else
  7334. {
  7335. sleep(3);
  7336. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  7337. }
  7338. StopSystemTimeoutDet();
  7339. if (!DEBUG_ALSTON &&
  7340. (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  7341. ShmPsuData->Work_Step == _NO_WORKING ||
  7342. ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES ||
  7343. strcmp((char *)ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ") == EQUAL))
  7344. {
  7345. if (ShmSysConfigAndInfo->SysWarningInfo.Level != _ALARM_LEVEL_CRITICAL)
  7346. {
  7347. if (!DisplaySelfTestFailReason() &&
  7348. ShmDcCommonData->rebootCount < 1)
  7349. {
  7350. PRINTF_FUNC("=======Soft reboot for retry self-test======= \n");
  7351. SetupRebootCount(1);
  7352. sleep(3);
  7353. KillAllTask();
  7354. system("/usr/bin/run_evse_restart.sh");
  7355. }
  7356. else if (ShmDcCommonData->rebootCount < 1)
  7357. {
  7358. PRINTF_FUNC("=======Soft reboot for retry self-test (Normal)======= \n");
  7359. SetupRebootCount(1);
  7360. sleep(3);
  7361. KillAllTask();
  7362. system("/usr/bin/run_evse_restart.sh");
  7363. }
  7364. else
  7365. SetupRebootCount(0);
  7366. }
  7367. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7368. {
  7369. ChargingAlarmProcess(gun_index);
  7370. }
  7371. //ChangeLcmByIndex(_LCM_FIX);
  7372. sleep(3);
  7373. //if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  7374. //{
  7375. KillTaskExceptDetectTask();
  7376. //}
  7377. //else
  7378. // KillTask();
  7379. StopProcessingLoop();
  7380. }
  7381. else
  7382. {
  7383. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7384. {
  7385. setChargerMode(gun_index, SYS_MODE_IDLE);
  7386. }
  7387. SetupRebootCount(0);
  7388. }
  7389. PRINTF_FUNC("===== Self Test Complete ===== \n");
  7390. PRINTF_FUNC("===== Finally Create DB ===== \n");
  7391. // Local DB
  7392. if(DB_Open(localDb) != PASS)
  7393. {
  7394. PRINTF_FUNC("DB_Open fail. \n");
  7395. isDb_ready = false;
  7396. }
  7397. else
  7398. {
  7399. isDb_ready = true;
  7400. for (int _acIndex = 0; _acIndex < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _acIndex++)
  7401. {
  7402. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  7403. ac_chargingInfo[0]->IsAvailable = DB_Get_Operactive(localDb, _acIndex + hasDc);
  7404. }
  7405. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  7406. {
  7407. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  7408. chargingInfo[_index]->PowerConsumption = DB_Get_PowerConsumption(localDb, _index);
  7409. }
  7410. DB_Reboot_Record(localDb);
  7411. }
  7412. if (NetworkDB_Open(networkDb) != PASS)
  7413. {
  7414. PRINTF_FUNC("NetworkDB_Open fail. \n");
  7415. }
  7416. ChangeLcmByIndex(_LCM_IDLE);
  7417. sleep(1);
  7418. CreateRfidFork();
  7419. sleep(1);
  7420. if (!DEBUG_ALSTON)
  7421. {
  7422. CreateCheckSystemTaskFork();
  7423. // Create Watchdog
  7424. CreateWatchdog();
  7425. }
  7426. PRINTF_FUNC("===== Create DB End ===== \n");
  7427. PRINTF_FUNC("===== Charger info ===== ");
  7428. PRINTF_FUNC("SW Version = %s", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  7429. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  7430. CheckFwSlotStatusLog();
  7431. AdjustChargerCurrent();
  7432. GetTimespecFunc(&_cmdMainPriority_time);
  7433. _ocppProfileChkFlag_1 = 0;
  7434. _ocppProfileChkFlag_2 = 0;
  7435. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") == EQUAL)
  7436. {
  7437. DEBUG_ERROR_MSG("URL is empty kill Module_OcppBackend...\n");
  7438. system ("pkill OcppBackend");
  7439. }
  7440. int _mainTimebuf = 0;
  7441. for (;;)
  7442. {
  7443. if (ShmDcCommonData->_isAutoRunTest != _isTestRun)
  7444. {
  7445. if (ShmDcCommonData->_isAutoRunTest == YES)
  7446. {
  7447. GetTimespecFunc(&_autoRuntestTime);
  7448. }
  7449. _isTestRun = ShmDcCommonData->_isAutoRunTest;
  7450. }
  7451. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  7452. {
  7453. ocpp_ac_chk_availability_cmd();
  7454. ocpp_ac_chk_unlock_cmd();
  7455. if ((ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING) ||
  7456. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7457. {
  7458. CheckAcSmartChargeProfile();
  7459. }
  7460. }
  7461. ocpp_auto_response_BootNotification();
  7462. ocpp_chk_reset_cmd(NO);
  7463. if (!DEBUG_ALSTON)
  7464. ChkPrimaryStatus();
  7465. if ((IsConnectorWholeIdle() ||
  7466. ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX))
  7467. {
  7468. CheckFactoryConfigFunction();
  7469. CheckFwUpdateFunction();
  7470. }
  7471. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  7472. {
  7473. sleep(1);
  7474. continue;
  7475. }
  7476. // OCPP 邏輯
  7477. ocpp_chk_remoteStart_cmd();
  7478. // 讀卡邏輯
  7479. ScannerCardProcess();
  7480. // 當 AC 沒有搭上時,清除一些錯誤碼
  7481. ClearAlarmCodeWhenAcOff();
  7482. // 確認是否要回到充電中的槍畫面邏輯判斷
  7483. CheckReturnToChargingConn();
  7484. if ((_acgunIndex > 0 && isDetectPlugin() && !isCardScan))
  7485. {
  7486. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  7487. }
  7488. _mainTimebuf = GetTimeoutValue(&_cmdMainPriority_time);
  7489. if (_mainTimebuf < 0)
  7490. GetTimespecFunc(&_cmdMainPriority_time);
  7491. if (GetTimeoutValue(&_cmdMainPriority_time) >= 5)
  7492. {
  7493. CheckTask();
  7494. Run4gResetProc();
  7495. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7496. {
  7497. if ((chargingInfo[_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  7498. (chargingInfo[_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7499. {
  7500. if (chargingInfo[_index]->SystemStatus == SYS_MODE_CHARGING &&
  7501. ((_index == 0 && _ocppProfileChkFlag_1 == 12) ||
  7502. (_index == 1 && _ocppProfileChkFlag_2 == 12)))
  7503. {
  7504. ocpp_lift_profileReq_cmd(_index);
  7505. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  7506. if (_index == 0)
  7507. _ocppProfileChkFlag_1 = 0;
  7508. else if (_index == 1)
  7509. _ocppProfileChkFlag_2 = 0;
  7510. }
  7511. else if (chargingInfo[_index]->SystemStatus != SYS_MODE_CHARGING)
  7512. {
  7513. ocpp_lift_profileReq_cmd(_index);
  7514. if (_index == 0)
  7515. _ocppProfileChkFlag_1 = 0;
  7516. else if (_index == 1)
  7517. _ocppProfileChkFlag_2 = 0;
  7518. }
  7519. else
  7520. {
  7521. if (_index == 0)
  7522. _ocppProfileChkFlag_1++;
  7523. else if (_index == 1)
  7524. _ocppProfileChkFlag_2++;
  7525. }
  7526. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  7527. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  7528. {
  7529. DB_Update_PowerConsumption(localDb, _index, chargingInfo[_index]->PowerConsumption);
  7530. }
  7531. // Charging Profile
  7532. ocpp_chargingProfile_process(_index);
  7533. }
  7534. if (chargingInfo[_index]->SystemStatus == SYS_MODE_RESERVATION)
  7535. {
  7536. if (opcc_chk_reserve_expired(_index))
  7537. {
  7538. chargingInfo[_index]->SystemStatus = SYS_MODE_IDLE;
  7539. }
  7540. }
  7541. // OTP
  7542. CheckConnectorOTP(_index);
  7543. }
  7544. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  7545. {
  7546. if ((ac_chargingInfo[_index]->SystemStatus >= SYS_MODE_PREPARING &&
  7547. ac_chargingInfo[_index]->SystemStatus <= SYS_MODE_CHARGING))
  7548. {
  7549. if (ac_chargingInfo[_index]->SystemStatus == SYS_MODE_CHARGING && _ac_ocppProfileChkFlag == 12)
  7550. {
  7551. ocpp_ac_lift_profileReq_cmd(_index);
  7552. _ac_ocppProfileChkFlag = 0;
  7553. }
  7554. else if (ac_chargingInfo[_index]->SystemStatus != SYS_MODE_CHARGING)
  7555. {
  7556. ocpp_ac_lift_profileReq_cmd(_index);
  7557. _ac_ocppProfileChkFlag = 0;
  7558. }
  7559. else
  7560. _ac_ocppProfileChkFlag++;
  7561. }
  7562. }
  7563. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling &&
  7564. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  7565. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  7566. CheckOcppCostAndPrice();
  7567. GetTimespecFunc(&_cmdMainPriority_time);
  7568. }
  7569. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
  7570. ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L')
  7571. CheckStatusOfMeter();
  7572. // 確認當前錯誤 Level = 2 ?
  7573. ReviewCriticalAlarm();
  7574. errCollect.GunErrMessage = 0;
  7575. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7576. {
  7577. CheckGpioInStatus();
  7578. // 確認系統的錯誤狀態
  7579. CheckSystemErrorFunction(gun_index);
  7580. // 確認 Relay Welding or Driving Fault
  7581. CheckRelayWeldingOrDrivingFault(gun_index);
  7582. // 收集各槍的錯誤狀態
  7583. CollectError(gun_index);
  7584. ocpp_chk_reserved_cmd(gun_index);
  7585. ocpp_chk_availability_cmd(gun_index);
  7586. ocpp_chk_unlock_cmd(gun_index);
  7587. if ((chargingInfo[gun_index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= SYS_MODE_CHARGING) ||
  7588. (chargingInfo[gun_index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[gun_index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  7589. {
  7590. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_CHARGING)
  7591. {
  7592. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  7593. if (chargingInfo[gun_index]->PresentChargingVoltage >= gunOutputVol[gun_index] + 10 ||
  7594. chargingInfo[gun_index]->PresentChargingVoltage <= gunOutputVol[gun_index] - 10)
  7595. {
  7596. if (gun_index == 0)
  7597. _ocppProfileChkFlag_1 = 12;
  7598. else if (gun_index == 1)
  7599. _ocppProfileChkFlag_2 = 12;
  7600. gunOutputVol[gun_index] = chargingInfo[gun_index]->PresentChargingVoltage;
  7601. }
  7602. }
  7603. ocpp_chk_profileConf_cmd(gun_index);
  7604. }
  7605. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  7606. switch(chargingInfo[gun_index]->SystemStatus)
  7607. {
  7608. case SYS_MODE_IDLE:
  7609. case SYS_MODE_RESERVATION:
  7610. case SYS_MODE_MAINTAIN:
  7611. case SYS_MODE_FAULT:
  7612. {
  7613. if ((chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE || chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION) &&
  7614. isModeChange(gun_index))
  7615. {
  7616. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  7617. {
  7618. PRINTF_FUNC("================= SYS_MODE_IDLE (%x) ================ \n", gun_index);
  7619. chargingInfo[gun_index]->ReservationId = -1;
  7620. }
  7621. else if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION)
  7622. {
  7623. PRINTF_FUNC("================= SYS_MODE_RESERVATION (%x) ================ \n", gun_index);
  7624. PRINTF_FUNC("================= Id : %s ================ \n", ShmDcCommonData->_reserved_UserId[gun_index]);
  7625. }
  7626. chargingInfo[gun_index]->PresentChargedDuration = 0;
  7627. chargingInfo[gun_index]->RemainChargingDuration = 0;
  7628. chargingInfo[gun_index]->PresentChargingVoltage = 0;
  7629. chargingInfo[gun_index]->PresentChargingCurrent = 0;
  7630. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "00:00:00");
  7631. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "00:00:00");
  7632. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  7633. strcpy((char *)chargingInfo[gun_index]->EVCCID, "");
  7634. ClearEnergyTimePeriod(gun_index);
  7635. gunOutputVol[gun_index] = 0.0;
  7636. ocpp_clear_stopReason(gun_index);
  7637. ocpp_auto_response_StartTransationConf(gun_index);
  7638. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_WAIT;
  7639. }
  7640. if (chargingInfo[gun_index]->IsAvailable == NO)
  7641. {
  7642. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  7643. }
  7644. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL)
  7645. {
  7646. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7647. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  7648. else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_IDLE &&
  7649. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_RESERVATION &&
  7650. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_MAINTAIN &&
  7651. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != SYS_MODE_FAULT)
  7652. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7653. ClearDetectPluginFlag();
  7654. ocpp_set_errCode_cmd(gun_index);
  7655. setChargerMode(gun_index, SYS_MODE_FAULT);
  7656. }
  7657. else
  7658. {
  7659. ChangeToMaxChargingMode();
  7660. if (PrecheckIsPass(gun_index))
  7661. {
  7662. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_FAULT)
  7663. {
  7664. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7665. setChargerMode(gun_index, SYS_MODE_IDLE);
  7666. }
  7667. // if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  7668. // {
  7669. // if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  7670. // ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  7671. // }
  7672. // else
  7673. {
  7674. // Idle 正常程序起點
  7675. // 判斷是否有啟用檢查插槍
  7676. if(isDetectPlugin())
  7677. {
  7678. // 卡號驗證成功後,等待充電槍插入充電車
  7679. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  7680. {
  7681. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  7682. chargingInfo[gun_index]->IsAvailable)
  7683. {
  7684. PRINTF_FUNC("Conn %d Start Charging : Remote \n", gun_index);
  7685. chargingInfo[gun_index]->RemoteStartFlag = NO;
  7686. chargingInfo[gun_index]->isRemoteStart = YES;
  7687. ChangeGunSelectByIndex(gun_index);
  7688. AddPlugInTimes(gun_index);
  7689. setChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  7690. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  7691. ClearDetectPluginFlag();
  7692. continue;
  7693. }
  7694. }
  7695. else if (chargingInfo[gun_index]->ReservedStartFlag == YES)
  7696. {
  7697. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  7698. chargingInfo[gun_index]->IsAvailable)
  7699. {
  7700. chargingInfo[gun_index]->ReservedStartFlag = NO;
  7701. ChangeGunSelectByIndex(gun_index);
  7702. AddPlugInTimes(gun_index);
  7703. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmDcCommonData->_reserved_UserId[gun_index]);
  7704. PRINTF_FUNC("Conn %d Start Charging : Reserved CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  7705. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  7706. strcpy((char *)ShmDcCommonData->_reserved_UserId[gun_index], "");
  7707. // 當前操作的槍號,進入 Preparing
  7708. setChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  7709. ClearDetectPluginFlag();
  7710. continue;
  7711. }
  7712. }
  7713. else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE)
  7714. {
  7715. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  7716. chargingInfo[gun_index]->IsAvailable &&
  7717. chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE)
  7718. {
  7719. ChangeGunSelectByIndex(gun_index);
  7720. AddPlugInTimes(gun_index);
  7721. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  7722. PRINTF_FUNC("Conn %d Start Charging : RFID CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  7723. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  7724. // 當前操作的槍號,進入 Preparing
  7725. setChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  7726. ClearDetectPluginFlag();
  7727. continue;
  7728. }
  7729. }
  7730. if (!isCardScan &&
  7731. gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7732. {
  7733. // LCM => Waiting for plugging
  7734. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  7735. }
  7736. }
  7737. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  7738. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  7739. {
  7740. PRINTF_FUNC("Conn %d Start Charging : Plug and charing \n");
  7741. bool isCanStartChargingFlag = GetStartChargingByAlterMode(gun_index);
  7742. if (isCanStartChargingFlag)
  7743. {
  7744. ChangeGunSelectByIndex(gun_index);
  7745. AddPlugInTimes(gun_index);
  7746. setChargerMode(gun_index, SYS_MODE_MODE_REASSIGN_CHECK);
  7747. ClearDetectPluginFlag();
  7748. strncpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char *)ShmSysConfigAndInfo->SysConfig.SerialNumber, sizeof(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  7749. continue;
  7750. }
  7751. }
  7752. else
  7753. {
  7754. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  7755. {
  7756. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  7757. {
  7758. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  7759. }
  7760. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE &&
  7761. ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_WAIT_FOR_PLUG)
  7762. {
  7763. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7764. }
  7765. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  7766. }
  7767. }
  7768. } // Idle 正常程序終點
  7769. }
  7770. else
  7771. {
  7772. setChargerMode(gun_index, SYS_MODE_FAULT);
  7773. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  7774. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  7775. }
  7776. ReleaseSysAlarmCode(gun_index);
  7777. }
  7778. }
  7779. break;
  7780. case SYS_MODE_MODE_REASSIGN_CHECK:
  7781. {
  7782. if (isModeChange(gun_index))
  7783. {
  7784. PRINTF_FUNC("================= SYS_MODE_MODE_REASSIGN_CHECK (%x) ================ \n", gun_index);
  7785. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7786. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  7787. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  7788. StopSystemTimeoutDet();
  7789. }
  7790. bool isRessign = false;
  7791. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  7792. {
  7793. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  7794. {
  7795. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  7796. {
  7797. if (chargingInfo[index]->SystemStatus != SYS_MODE_MODE_REASSIGN_CHECK)
  7798. {
  7799. if (((chargingInfo[index]->SystemStatus >= SYS_MODE_PREPARING && chargingInfo[index]->SystemStatus <= SYS_MODE_CHARGING) ||
  7800. (chargingInfo[index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingInfo[index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1)) &&
  7801. chargingInfo[index]->SystemStatus != SYS_MODE_MAINTAIN &&
  7802. chargingInfo[index]->SystemStatus != SYS_MODE_RESERVATION)
  7803. {
  7804. // A 槍在充電過程中 - 進行分配動作
  7805. PRINTF_FUNC("======= Max -> Aver : Preparing. (Step 1) ======= \n");
  7806. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  7807. isRessign = true;
  7808. break;
  7809. }
  7810. else
  7811. {
  7812. // A 槍已結束充電 - 直接回 B 槍最大充
  7813. PRINTF_FUNC("======= Max -> Aver : Charging mode = MAX (Keep) ======= \n");
  7814. //ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  7815. break;
  7816. }
  7817. }
  7818. }
  7819. }
  7820. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  7821. {
  7822. // 如果在切換最大充的過程中,需等待最大充切換完成後,再走均充流程
  7823. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  7824. {
  7825. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  7826. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  7827. {
  7828. PRINTF_FUNC("======= Aver -> Max : Aver -> Max : Current Balancing. (Step 14) ======= \n");
  7829. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  7830. }
  7831. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  7832. {
  7833. PRINTF_FUNC("======= Aver -> Max : Charging mode = MAX ======= \n");
  7834. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  7835. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7836. continue;
  7837. }
  7838. }
  7839. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  7840. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  7841. continue;
  7842. }
  7843. }
  7844. if (isRessign)
  7845. setChargerMode(gun_index, SYS_MODE_REASSIGN);
  7846. else
  7847. setChargerMode(gun_index, SYS_MODE_PREPARING);
  7848. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  7849. {
  7850. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  7851. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7852. }
  7853. }
  7854. break;
  7855. case SYS_MODE_REASSIGN:
  7856. {
  7857. if (isModeChange(gun_index))
  7858. {
  7859. PRINTF_FUNC("================== SYS_MODE_REASSIGN (%x) ================ \n", gun_index);
  7860. }
  7861. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  7862. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  7863. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A &&
  7864. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  7865. {
  7866. PRINTF_FUNC("======= Max -> Aver : Charging mode = AVER ======= \n");
  7867. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  7868. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7869. }
  7870. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  7871. {
  7872. if ((ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO &&
  7873. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER) ||
  7874. (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES &&
  7875. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX))
  7876. {
  7877. setChargerMode(gun_index, SYS_MODE_PREPARING);
  7878. }
  7879. }
  7880. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  7881. {
  7882. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  7883. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7884. }
  7885. }
  7886. break;
  7887. case SYS_MODE_PREPARING:
  7888. {
  7889. if (isModeChange(gun_index))
  7890. {
  7891. PRINTF_FUNC("=================== SYS_MODE_PREPARING (%x) =============== \n", gun_index);
  7892. StopGunInfoTimeoutDet(gun_index);
  7893. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  7894. }
  7895. if (ShmPsuData->SystemPresentPsuQuantity > 0 && ShmPsuData->SystemAvailablePower > 10 &&
  7896. ShmPsuData->Work_Step == _WORK_CHARGING &&
  7897. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 5)
  7898. {
  7899. setChargerMode(gun_index, SYS_MODE_PREPARE_FOR_EV);
  7900. }
  7901. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2)
  7902. {
  7903. bool oughtAver = true;
  7904. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  7905. {
  7906. // 共同進入充電邏輯
  7907. if (chargingInfo[index]->SystemStatus != SYS_MODE_PREPARING)
  7908. {
  7909. oughtAver = false;
  7910. break;
  7911. }
  7912. }
  7913. if (oughtAver)
  7914. {
  7915. PRINTF_FUNC("********* Smart Charging : _REASSIGNED_NONE (AVER) ********** \n");
  7916. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  7917. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  7918. }
  7919. }
  7920. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  7921. {
  7922. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  7923. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7924. }
  7925. }
  7926. break;
  7927. case SYS_MODE_PREPARE_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  7928. {
  7929. if (isModeChange(gun_index))
  7930. {
  7931. PRINTF_FUNC("================== SYS_MODE_PREPARE_FOR_EV (%x) ================ \n", gun_index);
  7932. _presentChargingTimeBuf[gun_index] = 0;
  7933. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  7934. StopGunInfoTimeoutDet(gun_index);
  7935. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  7936. // Start Transation
  7937. ocpp_set_startTransation_cmd(gun_index);
  7938. GetTimespecFunc(&_startTransation_time[gun_index]);
  7939. }
  7940. ShmDcCommonData->startTransactionFlag[gun_index] = ocpp_is_resPass_StartTransactionConf(gun_index);
  7941. if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_PASS)
  7942. {
  7943. if (ocpp_chk_invalid_id_cmd(gun_index))
  7944. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_FAIL;
  7945. }
  7946. else
  7947. {
  7948. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  7949. {
  7950. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING ||
  7951. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  7952. !ocpp_get_startTransation_req(gun_index) ||
  7953. strcmp((char *)chargingInfo[gun_index]->StartUserId, "") != EQUAL ||
  7954. _isTestRun)
  7955. {
  7956. ShmDcCommonData->startTransactionFlag[gun_index] = START_TRANSATION_STATUS_PASS;
  7957. }
  7958. }
  7959. }
  7960. // 小板停止
  7961. ChkConnectorBoardStop(gun_index);
  7962. if (ocpp_chk_remoteStop_cmd(gun_index) == YES ||
  7963. WifiScheduleStop(gun_index) ||
  7964. ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_FAIL ||
  7965. (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_WAIT && GetTimeoutValue(&_startTransation_time[gun_index]) > 45))
  7966. {
  7967. // 後臺要求停止
  7968. ChargingTerminalProcess(gun_index);
  7969. if (ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_FAIL ||
  7970. ShmDcCommonData->startTransactionFlag[gun_index] == START_TRANSATION_STATUS_WAIT)
  7971. {
  7972. PRINTF_FUNC("StartTransaction req fail. (%d) \n", gun_index);
  7973. }
  7974. }
  7975. // 檢查車端的槍鎖是否為鎖上
  7976. if (isEvGunLocked(gun_index) == YES)
  7977. {
  7978. setChargerMode(gun_index, SYS_MODE_PREPARE_FOR_EVSE);
  7979. }
  7980. // LCM => Pre-charging
  7981. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  7982. {
  7983. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  7984. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7985. }
  7986. }
  7987. break;
  7988. case SYS_MODE_PREPARE_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  7989. {
  7990. if (isModeChange(gun_index))
  7991. {
  7992. PRINTF_FUNC("================== SYS_MODE_PREPARE_FOR_EVSE (%x) ================ \n", gun_index);
  7993. StopGunInfoTimeoutDet(gun_index);
  7994. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  7995. }
  7996. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  7997. {
  7998. // 檢查樁端的 GFD 結果
  7999. if (isPrechargeStatus_chademo(gun_index) == 8)
  8000. {
  8001. // 當前操作的槍號,進入 Charging
  8002. setChargerMode(gun_index, SYS_MODE_CHARGING);
  8003. }
  8004. }
  8005. else if (chargingInfo[gun_index]->Type == _Type_GB)
  8006. {
  8007. // 檢查樁端的 GFD 結果
  8008. if (isPrechargeStatus_gb(gun_index) > 9)
  8009. {
  8010. setChargerMode(gun_index, SYS_MODE_CHARGING);
  8011. }
  8012. }
  8013. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  8014. {
  8015. // 檢查樁端的 GFD 結果
  8016. if ((chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  8017. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING))
  8018. {
  8019. setChargerMode(gun_index, SYS_MODE_CCS_PRECHARGE_STEP0);
  8020. }
  8021. }
  8022. RecordGfdResult(gun_index);
  8023. // 小板停止
  8024. ChkConnectorBoardStop(gun_index);
  8025. // 後台要求停止
  8026. ChkBackendStop(gun_index);
  8027. // LCM => Pre-charging
  8028. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8029. {
  8030. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8031. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8032. }
  8033. }
  8034. break;
  8035. case SYS_MODE_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  8036. {
  8037. if (isModeChange(gun_index))
  8038. {
  8039. PRINTF_FUNC("================== SYS_MODE_CHARGING (%x) ================ \n", gun_index);
  8040. StopGunInfoTimeoutDet(gun_index);
  8041. ftime(&startChargingTime[gun_index]);
  8042. ocpp_clear_idTag_cmd(gun_index);
  8043. ChangeStartOrStopDateTime(YES, gun_index);
  8044. if (_isTestRun == YES)
  8045. {
  8046. GetTimespecFunc(&_autoRuntestTime);
  8047. }
  8048. }
  8049. if (_isTestRun == YES)
  8050. {
  8051. if (GetTimeoutValue(&_autoRuntestTime) > 60)
  8052. {
  8053. ChargingTerminalProcess(gun_index);
  8054. }
  8055. }
  8056. SavePresentChargedDuration(gun_index);
  8057. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  8058. {
  8059. if (ShmDcCommonData->minOutputCur != 0 &&
  8060. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  8061. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  8062. {
  8063. // UCP
  8064. RecordAlarmCode(gun_index, "012320");
  8065. ChargingTerminalProcess(gun_index);
  8066. }
  8067. }
  8068. else if (chargingInfo[gun_index]->Type == _Type_GB)
  8069. {
  8070. if (ShmDcCommonData->minOutputCur != 0 &&
  8071. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  8072. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  8073. {
  8074. // UCP
  8075. RecordAlarmCode(gun_index, "012322");
  8076. ChargingTerminalProcess(gun_index);
  8077. }
  8078. }
  8079. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  8080. {
  8081. if (ShmDcCommonData->minOutputCur != 0 &&
  8082. chargingInfo[gun_index]->PresentChargedDuration >= 10 &&
  8083. chargingInfo[gun_index]->PresentChargingCurrent < ShmDcCommonData->minOutputCur)
  8084. {
  8085. // UCP
  8086. RecordAlarmCode(gun_index, "012321");
  8087. ChargingTerminalProcess(gun_index);
  8088. }
  8089. }
  8090. RecordGfdResult(gun_index);
  8091. // 小板停止
  8092. ChkConnectorBoardStop(gun_index);
  8093. // 後台要求停止
  8094. ChkBackendStop(gun_index);
  8095. // 設定停止
  8096. ChkConfigurationStop(gun_index);
  8097. // LCM => Charging
  8098. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8099. {
  8100. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  8101. if (!stopChargingChkByCard)
  8102. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8103. }
  8104. }
  8105. break;
  8106. case SYS_MODE_ALARM:
  8107. case SYS_MODE_TERMINATING:
  8108. {
  8109. if (isModeChange(gun_index))
  8110. {
  8111. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip ||
  8112. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  8113. ocpp_set_stopReason_by_cmd(gun_index, "EmergencyStop");
  8114. else
  8115. ocpp_set_stopReason_by_cmd(gun_index, "Local");
  8116. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  8117. {
  8118. PRINTF_FUNC("================== SYS_MODE_ALARM (%x) ================ \n", gun_index);
  8119. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  8120. {
  8121. ocpp_auto_response_StartTransationConf(gun_index);
  8122. ocpp_set_stopTransation_cmd(gun_index);
  8123. }
  8124. TheEndCharging(gun_index);
  8125. }
  8126. else
  8127. {
  8128. ocpp_set_errCode_cmd(gun_index);
  8129. PRINTF_FUNC("================== SYS_MODE_TERMINATING (%x) ================ \n", gun_index);
  8130. }
  8131. StopGunInfoTimeoutDet(gun_index);
  8132. }
  8133. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  8134. ChangeToMaxChargingMode();
  8135. if (chargingInfo[gun_index]->Type == _Type_Chademo) { }
  8136. else if (chargingInfo[gun_index]->Type == _Type_GB) { }
  8137. else if (chargingInfo[gun_index]->Type == _Type_CCS) { }
  8138. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  8139. {
  8140. if(chargingInfo[gun_index]->ConnectorPlugIn == NO &&
  8141. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  8142. //if(GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  8143. {
  8144. setChargerMode(gun_index, SYS_MODE_IDLE);
  8145. }
  8146. else if (_isTestRun == YES &&
  8147. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  8148. {
  8149. setChargerMode(gun_index, SYS_MODE_IDLE);
  8150. }
  8151. }
  8152. else
  8153. {
  8154. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  8155. {
  8156. if (isEvGunLocked(gun_index) == NO ||
  8157. isPrechargeStatus_chademo(gun_index) <= 0)
  8158. {
  8159. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  8160. }
  8161. }
  8162. else if (chargingInfo[gun_index]->Type == _Type_GB)
  8163. {
  8164. if (isEvGunLocked(gun_index) == NO ||
  8165. isPrechargeStatus_gb(gun_index) <= 0)
  8166. {
  8167. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  8168. }
  8169. }
  8170. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  8171. {
  8172. if (isEvGunLocked(gun_index) == NO &&
  8173. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  8174. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14))
  8175. {
  8176. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  8177. }
  8178. }
  8179. }
  8180. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8181. {
  8182. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  8183. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8184. }
  8185. }
  8186. break;
  8187. case SYS_MODE_COMPLETE:
  8188. {
  8189. if (isModeChange(gun_index))
  8190. {
  8191. PRINTF_FUNC("================== SYS_MODE_COMPLETE (%x) ================ \n", gun_index);
  8192. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  8193. {
  8194. ocpp_set_errCode_cmd(gun_index);
  8195. ocpp_auto_response_StartTransationConf(gun_index);
  8196. ocpp_set_stopTransation_cmd(gun_index);
  8197. }
  8198. TheEndCharging(gun_index);
  8199. }
  8200. ChangeToMaxChargingMode();
  8201. if(chargingInfo[gun_index]->ConnectorPlugIn == NO &&
  8202. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  8203. //if(GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  8204. {
  8205. setChargerMode(gun_index, SYS_MODE_IDLE);
  8206. }
  8207. else if (_isTestRun == YES &&
  8208. GetTimeoutValue(&chargingInfo[gun_index]->ConnectorTimeout) >= 10)
  8209. {
  8210. setChargerMode(gun_index, SYS_MODE_IDLE);
  8211. }
  8212. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8213. {
  8214. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  8215. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8216. }
  8217. }
  8218. break;
  8219. case SYS_MODE_CCS_PRECHARGE_STEP0:
  8220. {
  8221. if (isModeChange(gun_index))
  8222. {
  8223. PRINTF_FUNC("================== SYS_MODE_CCS_PRECHARGE_STEP0 (%x) ================ \n", gun_index);
  8224. StopGunInfoTimeoutDet(gun_index);
  8225. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  8226. }
  8227. RecordGfdResult(gun_index);
  8228. // 小板停止
  8229. ChkConnectorBoardStop(gun_index);
  8230. // 後台要求停止
  8231. ChkBackendStop(gun_index);
  8232. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  8233. // 切換 D+ Relay to Precharge Relay
  8234. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  8235. {
  8236. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  8237. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  8238. {
  8239. if (isPrechargeStatus_ccs(gun_index) == 39)
  8240. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PreChargeResponse (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  8241. else if (isPrechargeStatus_ccs(gun_index) == 40)
  8242. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PowerDeliveryRequest start (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  8243. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  8244. }
  8245. }
  8246. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  8247. {
  8248. setChargerMode(gun_index, SYS_MODE_CCS_PRECHARGE_STEP1);
  8249. }
  8250. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8251. {
  8252. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8253. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8254. }
  8255. break;
  8256. }
  8257. case SYS_MODE_CCS_PRECHARGE_STEP1:
  8258. {
  8259. if (isModeChange(gun_index))
  8260. {
  8261. PRINTF_FUNC("================== SYS_MODE_CCS_PRECHARGE_STEP1 (%x) ================ \n", gun_index);
  8262. }
  8263. RecordGfdResult(gun_index);
  8264. // 小板停止
  8265. ChkConnectorBoardStop(gun_index);
  8266. // 後台要求停止
  8267. ChkBackendStop(gun_index);
  8268. // 等待小板通知進入充電
  8269. // 切換 D+ Relay to Precharge Relay
  8270. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  8271. {
  8272. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  8273. setChargerMode(gun_index, SYS_MODE_CHARGING);
  8274. }
  8275. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  8276. {
  8277. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  8278. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  8279. }
  8280. break;
  8281. }
  8282. }
  8283. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_IDLE ||
  8284. chargingInfo[gun_index]->SystemStatus == SYS_MODE_RESERVATION ||
  8285. chargingInfo[gun_index]->SystemStatus == SYS_MODE_MAINTAIN ||
  8286. chargingInfo[gun_index]->SystemStatus == SYS_MODE_FAULT)
  8287. {
  8288. ocpp_chk_notificationMessage_cmd(gun_index);
  8289. }
  8290. }
  8291. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  8292. {
  8293. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  8294. }
  8295. else
  8296. {
  8297. bool dcPageRun = false;
  8298. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  8299. {
  8300. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE)
  8301. ChangeLcmByIndex(_LCM_IDLE);
  8302. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_PREPARING)
  8303. ChangeLcmByIndex(_LCM_PRE_CHARGE);
  8304. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  8305. ChangeLcmByIndex(_LCM_CHARGING);
  8306. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_TERMINATING ||
  8307. ac_chargingInfo[0]->SystemStatus == SYS_MODE_COMPLETE)
  8308. ChangeLcmByIndex(_LCM_COMPLETE);
  8309. else
  8310. dcPageRun = true;
  8311. }
  8312. else
  8313. dcPageRun = true;
  8314. if (dcPageRun)
  8315. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  8316. }
  8317. bool isCharging = false;
  8318. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8319. {
  8320. AlarmCheck(chargingInfo[gun_index]->Type);
  8321. if (_isTestRun == YES)
  8322. {
  8323. if (IsGunUsing(gun_index))
  8324. {
  8325. isCharging = true;
  8326. if (chargingInfo[gun_index]->SystemStatus == SYS_MODE_COMPLETE ||
  8327. chargingInfo[gun_index]->SystemStatus == SYS_MODE_ALARM)
  8328. {
  8329. GetTimespecFunc(&_autoRuntestTime);
  8330. }
  8331. }
  8332. }
  8333. }
  8334. if (_isTestRun == YES)
  8335. {
  8336. if (!isCharging)
  8337. {
  8338. if (GetTimeoutValue(&_autoRuntestTime) >= 60)
  8339. {
  8340. DetectPluginStart();
  8341. GetTimespecFunc(&_autoRuntestTime);
  8342. }
  8343. }
  8344. else
  8345. {
  8346. ClearDetectPluginFlag();
  8347. }
  8348. }
  8349. write(wtdFd, "a", 1);
  8350. usleep(whileLoopTime);
  8351. }
  8352. return FAIL;
  8353. }