main.c 335 KB

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