main.c 229 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856
  1. /*
  2. * main.c
  3. *
  4. * Created on: 2020年9月10日
  5. * Author: Wendell
  6. */
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/time.h>
  10. #include <sys/timeb.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/mman.h>
  17. #include <linux/wireless.h>
  18. #include <arpa/inet.h>
  19. #include <netinet/in.h>
  20. #include <unistd.h>
  21. #include <stdarg.h>
  22. #include <stdio.h> /*標準輸入輸出定義*/
  23. #include <stdlib.h> /*標準函數庫定義*/
  24. #include <unistd.h> /*Unix 標準函數定義*/
  25. #include <fcntl.h> /*檔控制定義*/
  26. #include <termios.h> /*PPSIX 終端控制定義*/
  27. #include <errno.h> /*錯誤號定義*/
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <math.h>
  35. #include "../../define.h"
  36. #include "Config.h"
  37. #include <stdbool.h>
  38. #include <dirent.h>
  39. #include "timeout.h"
  40. #include "Module_EvComm.h"
  41. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  42. #define PASS 1
  43. #define FAIL -1
  44. #define BUFFER_SIZE 128
  45. #define YES 1
  46. #define NO 0
  47. #define NORMAL 0
  48. #define ABNORMAL 1
  49. #define EQUAL 0
  50. #define BTN_RELEASE 0
  51. #define BTN_PRESS 1
  52. #define MAX_BUF 64
  53. #define MtdBlockSize 0x600000
  54. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  55. #define UPGRADE_FAN 0x02
  56. #define UPGRADE_RB 0x03
  57. #define UPGRADE_PRI 0x04
  58. #define UPGRADE_AC 0x05
  59. #define UPGRADE_LED 0x06
  60. #define SYSTEM_MIN_VOL 150
  61. #define MIN_OUTPUT_CUR 0
  62. #define AC_OUTPUT_VOL 220
  63. #define NO_DEFINE 255
  64. #define DEFAULT_AC_INDEX 2
  65. #define PSU_MIN_CUR 100
  66. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  67. #define uSEC_VAL 1000000
  68. #define SELFTEST_TIMEOUT 45
  69. #define AUTHORIZE_TIMEOUT 30
  70. #define AUTHORIZE_COMP_TIMEOUT 1
  71. #define AUTHORIZE_FAIL_TIMEOUT 3
  72. #define AUTHORIZE_STOP_TIMEOUT 30
  73. #define RETURN_TO_CHARGING_PAGE 30
  74. #define GUN_PREPARE_TIMEOUT 30
  75. #define GUN_EV_WAIT_TIMEOUT 120
  76. #define GUN_EVSE_WAIT_TIMEOUT 60
  77. #define GUN_COMP_WAIT_TIMEOUT 10
  78. #define GUN_PRECHARGING_TIMEOUT 60
  79. #define REBOOT_TIMEOUT 10
  80. #define RESET_TIMEOUT 10
  81. #define OCPP_COST_REQ_INTERVAL 5
  82. #define DISPENSER_RECONNECTE_TIME 30
  83. #define FORCE_BALANCE_TIME 5
  84. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  85. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  86. int whileLoopTime = 10000; // 10 ms
  87. int wtdFd = -1;
  88. byte _authorizeIndex = NO_DEFINE;
  89. bool IsAuthorizingMode();
  90. void ClearAuthorizedFlag();
  91. bool isDetectPlugin();
  92. void ClearDetectPluginFlag();
  93. int mystrcmp(unsigned char *p1, unsigned char *p2);
  94. long long DiffTimebWithNow(struct timeb ST);
  95. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  96. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  97. void ChargingTerminalProcess(byte gunIndex);
  98. void ChkPrimaryStatus();
  99. void StartSystemTimeoutDet(unsigned char flag);
  100. void StopSystemTimeoutDet();
  101. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  102. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  103. int StoreLogMsg_1(const char *fmt, ...);
  104. unsigned long GetTimeoutValue(struct timeval _sour_time);
  105. void gpio_set_value(unsigned int gpio, unsigned int value);
  106. void PRINTF_FUNC(char *string, ...);
  107. void ChangeGunSelectByIndex(byte sel);
  108. void InformOcppErrOccur(byte codeType);
  109. void RecordAlarmCode(byte gunIndex, char *code);
  110. void RecordWarningCode(byte gunIndex, char *code);
  111. void ReleaseWarningCodeByString(byte gunIndex, char *code);
  112. void ReleaseAlarmCode(byte gunIndex);
  113. int DB_Open(sqlite3 *db);
  114. int DB_Insert_Record(sqlite3 *db, int gun_index);
  115. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  116. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  117. void InitialDHCP();
  118. void InitialDispenserDhcpServerConfig(void);
  119. void StartDispenserDhcpServer(void);
  120. void _SelfTestTimeout(void);
  121. void CheckConnectionTimeout(void);
  122. #define DEBUG_INFO_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  123. #define DEBUG_WARN_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  124. #define DEBUG_ERROR_MSG(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  125. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  126. struct StatusCodeData *ShmStatusCodeData;
  127. struct PsuData *ShmPsuData;
  128. struct CHAdeMOData *ShmCHAdeMOData;
  129. struct GBTData *ShmGBTData;
  130. struct CcsData *ShmCcsData;
  131. struct PrimaryMcuData *ShmPrimaryMcuData;
  132. struct FanModuleData *ShmFanModuleData;
  133. struct RelayModuleData *ShmRelayModuleData[2];
  134. struct LedModuleData *ShmLedModuleData;
  135. struct OCPP16Data *ShmOCPP16Data;
  136. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  137. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  138. struct timeb startChargingTime[CONNECTOR_QUANTITY];
  139. struct timeb endChargingTime[CONNECTOR_QUANTITY];
  140. struct timeval _ConnectorAuthorizing_Time[CONNECTOR_QUANTITY];
  141. struct timeval _DispenserAuthorizing_Time[CONNECTOR_QUANTITY];
  142. int chargingTime[CONNECTOR_QUANTITY];
  143. // for initial index to check EV board type is correct
  144. byte _gunIndex = 0;
  145. byte _acgunIndex = 0;
  146. byte _chademoIndex = 0;
  147. byte _ccsIndex = 0;
  148. byte _gb_Index = 0;
  149. byte _ac_Index = 0;
  150. //byte bd0_1_status = 0;
  151. //byte bd0_2_status = 0;
  152. //byte bd1_1_status = 0;
  153. //byte bd1_2_status = 0;
  154. bool isCardScan = false;
  155. bool isModelNameMatch = true;
  156. //int rfidFd = -1;
  157. //char* rfidPortName = "/dev/ttyS2";
  158. char* fwVersion = "D0.06.00.0000.00";
  159. sqlite3 *localDb;
  160. bool isDb_ready;
  161. //================================================
  162. // initial can-bus
  163. //================================================
  164. int InitCanBus()
  165. {
  166. int s0,nbytes;
  167. struct timeval tv;
  168. struct ifreq ifr0;
  169. struct sockaddr_can addr0;
  170. system("/sbin/ip link set can0 down");
  171. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  172. system("/sbin/ip link set can0 up");
  173. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  174. tv.tv_sec = 0;
  175. tv.tv_usec = 10000;
  176. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  177. {
  178. #ifdef SystemLogMessage
  179. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  180. #endif
  181. }
  182. nbytes=40960;
  183. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  184. {
  185. #ifdef SystemLogMessage
  186. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  187. #endif
  188. }
  189. nbytes=40960;
  190. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  191. {
  192. #ifdef SystemLogMessage
  193. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  194. #endif
  195. }
  196. strcpy(ifr0.ifr_name, "can0" );
  197. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  198. addr0.can_family = AF_CAN;
  199. addr0.can_ifindex = ifr0.ifr_ifindex;
  200. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  201. return s0;
  202. }
  203. //================================================
  204. // initial uart port
  205. //================================================
  206. char *_priPortName = "/dev/ttyS1";
  207. char *_485PortName = "/dev/ttyS5";
  208. int InitComPort(byte target)
  209. {
  210. int fd;
  211. struct termios tios;
  212. if(target == UPGRADE_PRI)
  213. fd = open(_priPortName, O_RDWR);
  214. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  215. fd = open(_485PortName, O_RDWR);
  216. if(fd<=0)
  217. {
  218. #ifdef SystemLogMessage
  219. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  220. #endif
  221. return -1;
  222. }
  223. ioctl (fd, TCGETS, &tios);
  224. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  225. tios.c_lflag = 0;
  226. tios.c_iflag = 0;
  227. tios.c_oflag = 0;
  228. tios.c_cc[VMIN]=0;
  229. tios.c_cc[VTIME]=(unsigned char)1;
  230. tios.c_lflag=0;
  231. tcflush(fd, TCIFLUSH);
  232. ioctl (fd, TCSETS, &tios);
  233. return fd;
  234. }
  235. //=================================
  236. // Common routine
  237. //=================================
  238. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  239. {
  240. strncpy(dest, src + start, cnt);
  241. dest[cnt] = 0;
  242. }
  243. int InitWatchDog()
  244. {
  245. int fd;
  246. system("/usr/bin/fuser -k /dev/watchdog");
  247. sleep(1);
  248. system("echo V > /dev/watchdog");
  249. sleep(1);
  250. fd=open("/dev/watchdog", O_RDWR);
  251. if(fd<=0)
  252. {
  253. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  254. }
  255. return fd;
  256. }
  257. int StoreLogMsg_1(const char *fmt, ...)
  258. {
  259. char Buf[4096+256];
  260. char buffer[4096];
  261. va_list args;
  262. struct timeb SeqEndTime;
  263. struct tm *tm;
  264. va_start(args, fmt);
  265. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  266. va_end(args);
  267. memset(Buf,0,sizeof(Buf));
  268. ftime(&SeqEndTime);
  269. SeqEndTime.time = time(NULL);
  270. tm=localtime(&SeqEndTime.time);
  271. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  272. {
  273. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  274. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  275. printf("%s \n", Buf);
  276. }
  277. else
  278. {
  279. sprintf(Buf,"echo \'%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\' >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  280. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  281. buffer,
  282. tm->tm_year+1900,tm->tm_mon+1);
  283. system(Buf);
  284. }
  285. return rc;
  286. }
  287. unsigned long GetTimeoutValue(struct timeval _sour_time)
  288. {
  289. struct timeval _end_time;
  290. gettimeofday(&_end_time, NULL);
  291. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  292. }
  293. int mystrcmp(unsigned char *p1, unsigned char *p2)
  294. {
  295. while(*p1==*p2)
  296. {
  297. if(*p1=='\0' || *p2=='\0')
  298. break;
  299. p1++;
  300. p2++;
  301. }
  302. if(*p1=='\0' && *p2=='\0')
  303. return(PASS);
  304. else
  305. return(FAIL);
  306. }
  307. int DiffTimeb(struct timeb ST, struct timeb ET)
  308. {
  309. //return milli-second
  310. unsigned int StartTime,StopTime;
  311. StartTime=(unsigned int)ST.time;
  312. StopTime=(unsigned int)ET.time;
  313. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  314. return (StopTime-StartTime);
  315. }
  316. bool CheckTimeOut(struct timeb ST)
  317. {
  318. struct timeb ET;
  319. unsigned int StartTime, StopTime;
  320. ftime(&ET);
  321. StartTime = (unsigned int) ST.time;
  322. StopTime = (unsigned int) ET.time;
  323. return (StopTime > StartTime)? YES : NO;
  324. }
  325. void setChargerMode(byte gun_index, byte mode)
  326. {
  327. chargingInfo[gun_index]->SystemStatus = mode;
  328. }
  329. void PRINTF_FUNC(char *string, ...)
  330. {
  331. va_list args;
  332. char buffer[4096];
  333. va_start(args, string);
  334. vsnprintf(buffer, sizeof(buffer), string, args);
  335. va_end(args);
  336. DEBUG_INFO_MSG("%s \n", buffer);
  337. }
  338. long long DiffTimebWithNow(struct timeb ST)
  339. {
  340. //return milli-second
  341. struct timeb ET;
  342. long long StartTime,StopTime;
  343. ftime(&ET);
  344. StartTime=(long long)ST.time;
  345. StopTime=(long long)ET.time;
  346. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  347. }
  348. /*
  349. //==========================================
  350. // Check interface status
  351. //==========================================
  352. int isInterfaceUp(const char *interface)
  353. {
  354. int result = FAIL;
  355. FILE *fp;
  356. char cmd[256];
  357. char buf[512];
  358. strcpy(cmd, "ifconfig");
  359. fp = popen(cmd, "r");
  360. if(fp != NULL)
  361. {
  362. while(fgets(buf, sizeof(buf), fp) != NULL)
  363. {
  364. if(strstr(buf, interface) > 0)
  365. {
  366. result = PASS;
  367. }
  368. }
  369. }
  370. pclose(fp);
  371. return result;
  372. }
  373. */
  374. //=================================
  375. // Create all share memory
  376. //=================================
  377. int CreateShareMemory()
  378. {
  379. int MeterSMId;
  380. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  381. {
  382. #ifdef SystemLogMessage
  383. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  384. #endif
  385. return 0;
  386. }
  387. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  388. {
  389. #ifdef SystemLogMessage
  390. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  391. #endif
  392. return 0;
  393. }
  394. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  395. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  396. {
  397. #ifdef SystemLogMessage
  398. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  399. #endif
  400. return 0;
  401. }
  402. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  403. {
  404. #ifdef SystemLogMessage
  405. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  406. #endif
  407. return 0;
  408. }
  409. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  410. //creat ShmPsuData
  411. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  412. {
  413. #ifdef SystemLogMessage
  414. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  415. #endif
  416. return 0;
  417. }
  418. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  419. {
  420. #ifdef SystemLogMessage
  421. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  422. #endif
  423. return 0;
  424. }
  425. memset(ShmPsuData, 0, sizeof(struct PsuData));
  426. /*
  427. if(CHAdeMO_QUANTITY > 0)
  428. {
  429. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  430. {
  431. #ifdef SystemLogMessage
  432. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  433. #endif
  434. return 0;
  435. }
  436. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  437. {
  438. #ifdef SystemLogMessage
  439. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  440. #endif
  441. return 0;
  442. }
  443. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  444. }
  445. if(GB_QUANTITY > 0)
  446. {
  447. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  448. {
  449. #ifdef SystemLogMessage
  450. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  451. #endif
  452. return 0;
  453. }
  454. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  455. {
  456. #ifdef SystemLogMessage
  457. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  458. #endif
  459. return 0;
  460. }
  461. memset(ShmGBTData, 0, sizeof(struct GBTData));
  462. }
  463. //creat ShmCcsData
  464. if(CCS_QUANTITY > 0)
  465. {
  466. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  467. {
  468. #ifdef SystemLogMessage
  469. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  470. #endif
  471. return 0;
  472. }
  473. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  474. {
  475. #ifdef SystemLogMessage
  476. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  477. #endif
  478. return 0;
  479. }
  480. memset(ShmCcsData, 0, sizeof(struct CcsData));
  481. }
  482. */
  483. //creat ShmPrimaryMcuData
  484. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  485. {
  486. #ifdef SystemLogMessage
  487. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  488. #endif
  489. return 0;
  490. }
  491. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  492. {
  493. #ifdef SystemLogMessage
  494. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  495. #endif
  496. return 0;
  497. }
  498. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  499. //creat ShmFanModuleData
  500. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  501. {
  502. #ifdef SystemLogMessage
  503. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  504. #endif
  505. return 0;
  506. }
  507. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  508. {
  509. #ifdef SystemLogMessage
  510. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  511. #endif
  512. return 0;
  513. }
  514. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  515. //creat ShmRelayModuleData
  516. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  517. {
  518. #ifdef SystemLogMessage
  519. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData[0] NG \n");
  520. #endif
  521. return 0;
  522. }
  523. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  524. {
  525. #ifdef SystemLogMessage
  526. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData[0] NG \n");
  527. #endif
  528. return 0;
  529. }
  530. memset(ShmRelayModuleData[0], 0, sizeof(struct RelayModuleData));
  531. // DO360 RC2
  532. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  533. {
  534. #ifdef SystemLogMessage
  535. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData[1] NG \n");
  536. #endif
  537. return 0;
  538. }
  539. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  540. {
  541. #ifdef SystemLogMessage
  542. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData[1] NG \n");
  543. #endif
  544. return 0;
  545. }
  546. memset(ShmRelayModuleData[1], 0, sizeof(struct RelayModuleData));
  547. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  548. {
  549. #ifdef SystemLogMessage
  550. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmLedModuleData NG \n");
  551. #endif
  552. return 0;
  553. }
  554. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  555. {
  556. #ifdef SystemLogMessage
  557. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmLedModuleData NG \n");
  558. #endif
  559. return 0;
  560. }
  561. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  562. //creat ShmOCPP16Data
  563. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  564. {
  565. #ifdef SystemLogMessage
  566. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  567. #endif
  568. return 0;
  569. }
  570. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  571. {
  572. #ifdef SystemLogMessage
  573. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  574. #endif
  575. return 0;
  576. }
  577. // memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  578. return 1;
  579. }
  580. //=================================
  581. // LCM Page
  582. //=================================
  583. void ChangeLcmByIndex(byte page_index)
  584. {
  585. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2 ||
  586. page_index == _LCM_COMPLETE || page_index == _LCM_FIX)
  587. {
  588. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  589. }
  590. }
  591. //======================================================
  592. // Peripheral initial
  593. //======================================================
  594. void InitGPIO()
  595. {
  596. /*****************0~3, 4 bank, bank x 32+ num*********************/
  597. /***************************************************************/
  598. /*************** GPIO 0 ***************************************/
  599. /***************************************************************/
  600. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/ // no use
  601. system("echo 22 > /sys/class/gpio/export");
  602. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  603. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/ // no use
  604. system("echo 23 > /sys/class/gpio/export");
  605. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  606. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/ // no use
  607. system("echo 26 > /sys/class/gpio/export");
  608. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  609. system("echo 1 > /sys/class/gpio/gpio26/value");
  610. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/ // no use
  611. system("echo 27 > /sys/class/gpio/export");
  612. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  613. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  614. system("echo 29 > /sys/class/gpio/export");
  615. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  616. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/ // no use
  617. system("echo 19 > /sys/class/gpio/export");
  618. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  619. system("echo 1 > /sys/class/gpio/gpio19/value");
  620. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/ // no use
  621. system("echo 20 > /sys/class/gpio/export");
  622. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  623. /***************************************************************/
  624. /*************** GPIO 1 ***************************************/
  625. /***************************************************************/
  626. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/ // no use
  627. system("echo 44 > /sys/class/gpio/export");
  628. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  629. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/ // no use
  630. system("echo 45 > /sys/class/gpio/export");
  631. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  632. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/ // no use
  633. system("echo 46 > /sys/class/gpio/export");
  634. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  635. system("echo 0 > /sys/class/gpio/gpio46/value");
  636. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/ // no use
  637. system("echo 47 > /sys/class/gpio/export");
  638. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  639. /***************************************************************/
  640. /*************** GPIO 2 ***************************************/
  641. /***************************************************************/
  642. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  643. system("echo 89 > /sys/class/gpio/export");
  644. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  645. system("echo 1 > /sys/class/gpio/gpio89/value");
  646. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  647. system("echo 87 > /sys/class/gpio/export");
  648. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  649. system("echo 0 > /sys/class/gpio/gpio87/value");
  650. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/ // no use
  651. system("echo 88 > /sys/class/gpio/export");
  652. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  653. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/ // no use
  654. system("echo 86 > /sys/class/gpio/export");
  655. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  656. /***************************************************************/
  657. /*************** GPIO 3 ***************************************/
  658. /***************************************************************/
  659. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  660. system("echo 111 > /sys/class/gpio/export");
  661. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  662. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  663. system("echo 114 > /sys/class/gpio/export");
  664. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  665. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  666. system("echo 113 > /sys/class/gpio/export");
  667. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  668. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  669. system("echo 110 > /sys/class/gpio/export");
  670. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  671. system("echo 0 > /sys/class/gpio/gpio110/value");
  672. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/ // no use
  673. system("echo 115 > /sys/class/gpio/export");
  674. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  675. system("echo 0 > /sys/class/gpio/gpio115/value");
  676. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  677. system("echo 112 > /sys/class/gpio/export");
  678. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  679. system("echo 1 > /sys/class/gpio/gpio112/value");
  680. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/ // no use
  681. system("echo 116 > /sys/class/gpio/export");
  682. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  683. system("echo 0 > /sys/class/gpio/gpio116/value");
  684. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  685. system("echo 103 > /sys/class/gpio/export");
  686. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  687. system("echo 0 > /sys/class/gpio/gpio103/value");
  688. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  689. system("echo 104 > /sys/class/gpio/export");
  690. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  691. system("echo 0 > /sys/class/gpio/gpio104/value");
  692. #ifdef SystemLogMessage
  693. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK");
  694. #endif
  695. }
  696. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  697. {
  698. int fd,wrd;
  699. unsigned char *buf;
  700. unsigned int ChkSum,ChkSumOrg;
  701. if((buf=malloc(MtdBlockSize))==NULL)
  702. {
  703. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  704. if(ShmStatusCodeData!=NULL)
  705. {
  706. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  707. }
  708. sleep(5);
  709. system("reboot -f");
  710. sleep(5);
  711. system("reboot -f");
  712. }
  713. memset(buf, 0, MtdBlockSize);
  714. //================================================
  715. // Load configuration from mtdblock10
  716. //================================================
  717. fd = open("/dev/mtdblock10", O_RDWR);
  718. if (fd < 0)
  719. {
  720. free(buf);
  721. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  722. if(ShmStatusCodeData!=NULL)
  723. {
  724. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  725. }
  726. sleep(5);
  727. system("reboot -f");
  728. sleep(5);
  729. system("reboot -f");
  730. }
  731. wrd=read(fd, buf, MtdBlockSize);
  732. close(fd);
  733. if(wrd<MtdBlockSize)
  734. {
  735. free(buf);
  736. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  737. if(ShmStatusCodeData!=NULL)
  738. {
  739. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  740. }
  741. sleep(5);
  742. system("reboot -f");
  743. sleep(5);
  744. system("reboot -f");
  745. }
  746. ChkSum=0;
  747. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  748. {
  749. ChkSum+=buf[wrd];
  750. }
  751. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  752. //================================================
  753. // Load configuration from mtdblock11
  754. //================================================
  755. if(ChkSum!=ChkSumOrg)
  756. {
  757. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  758. fd = open("/dev/mtdblock11", O_RDWR);
  759. if (fd < 0)
  760. {
  761. free(buf);
  762. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  763. if(ShmStatusCodeData!=NULL)
  764. {
  765. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  766. }
  767. sleep(5);
  768. system("reboot -f");
  769. sleep(5);
  770. system("reboot -f");
  771. }
  772. memset(buf, 0, MtdBlockSize);
  773. wrd=read(fd, buf,MtdBlockSize);
  774. close(fd);
  775. if(wrd<MtdBlockSize)
  776. {
  777. free(buf);
  778. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  779. if(ShmStatusCodeData!=NULL)
  780. {
  781. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  782. }
  783. sleep(5);
  784. system("reboot -f");
  785. sleep(5);
  786. system("reboot -f");
  787. }
  788. ChkSum=0;
  789. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  790. {
  791. ChkSum+=buf[wrd];
  792. }
  793. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  794. //================================================
  795. // Load configuration from mtdblock12 (Factory default)
  796. //================================================
  797. if(ChkSum!=ChkSumOrg)
  798. {
  799. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  800. fd = open("/dev/mtdblock12", O_RDWR);
  801. if (fd < 0)
  802. {
  803. free(buf);
  804. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  805. if(ShmStatusCodeData!=NULL)
  806. {
  807. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  808. }
  809. sleep(5);
  810. system("reboot -f");
  811. sleep(5);
  812. system("reboot -f");
  813. }
  814. memset(buf, 0, MtdBlockSize);
  815. wrd=read(fd, buf,MtdBlockSize);
  816. close(fd);
  817. if(wrd<MtdBlockSize)
  818. {
  819. free(buf);
  820. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  821. if(ShmStatusCodeData!=NULL)
  822. {
  823. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  824. }
  825. sleep(5);
  826. system("reboot -f");
  827. sleep(5);
  828. system("reboot -f");
  829. }
  830. ChkSum=0;
  831. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  832. {
  833. ChkSum+=buf[wrd];
  834. }
  835. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  836. if(ChkSum!=ChkSumOrg)
  837. {
  838. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  839. free(buf);
  840. system("cd /root;./FactoryConfig -m");
  841. system("sync");
  842. sleep(5);
  843. system("reboot -f");
  844. sleep(5);
  845. system("reboot -f");
  846. return FAIL;
  847. }
  848. }
  849. }
  850. //load OK
  851. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  852. free(buf);
  853. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  854. return PASS;
  855. }
  856. /*
  857. byte GetCurEthUsage()
  858. {
  859. byte result = _ETHERNET_USAGE_NONE;
  860. FILE *fp;
  861. char cmd[256];
  862. char buf[512];
  863. byte targetC = 2;
  864. strcpy(cmd, "route -n");
  865. fp = popen(cmd, "r");
  866. if (fp != NULL)
  867. {
  868. while(fgets(buf, sizeof(buf), fp) != NULL)
  869. {
  870. if (targetC-- == 0)
  871. {
  872. if (strstr(buf, "eth0") > 0)
  873. result = _ETHERNET_USAGE_LAN;
  874. else if (strstr(buf, "mlan0") > 0)
  875. result = _ETHERNET_USAGE_WIFI;
  876. else if (strstr(buf, "ppp0") > 0)
  877. result = _ETHERNET_USAGE_3G_4g;
  878. break;
  879. }
  880. // if (strstr(buf, "inet addr:") > 0)
  881. // {
  882. // }
  883. }
  884. }
  885. pclose(fp);
  886. memset(buf, 0x00, sizeof(buf));
  887. return result;
  888. }
  889. */
  890. int isRouteFail()
  891. {
  892. int result = YES;
  893. FILE *fp;
  894. char buf[512];
  895. fp = popen("route -n", "r");
  896. if(fp != NULL)
  897. {
  898. while(fgets(buf, sizeof(buf), fp) != NULL)
  899. {
  900. if(strstr(buf, "eth0") != NULL)
  901. result = NO;
  902. }
  903. }
  904. pclose(fp);
  905. return result;
  906. }
  907. int isReachableInternet()
  908. {
  909. int result = FAIL;
  910. FILE *fp;
  911. char cmd[256];
  912. char buf[512];
  913. char tmp[512];
  914. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  915. // {
  916. // result = PASS;
  917. // }
  918. // else
  919. {
  920. strcpy(cmd, "ifconfig eth0");
  921. fp = popen(cmd, "r");
  922. if (fp != NULL)
  923. {
  924. while(fgets(buf, sizeof(buf), fp) != NULL)
  925. {
  926. if (strstr(buf, "inet addr:") > 0)
  927. {
  928. sscanf(buf, "%*s%s", tmp);
  929. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  930. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  931. {
  932. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  933. }
  934. }
  935. }
  936. }
  937. pclose(fp);
  938. memset(buf, 0x00, sizeof(buf));
  939. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  940. {
  941. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  942. fp = popen(cmd, "r");
  943. if(fp != NULL)
  944. {
  945. while(fgets(buf, sizeof(buf), fp) != NULL)
  946. {
  947. if(strstr(buf, "transmitted") > 0)
  948. {
  949. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  950. if(strstr(buf,"100%") != NULL)
  951. {
  952. }
  953. else
  954. {
  955. result = PASS;
  956. }
  957. //DEBUG_INFO("%s",buf);
  958. //DEBUG_INFO("%s\n",tmp);
  959. }
  960. }
  961. }
  962. pclose(fp);
  963. }
  964. }
  965. return result;
  966. }
  967. void InitEthernet()
  968. {
  969. system("ifconfig eth0 down");
  970. system("ifconfig eth1 down");
  971. sleep(2);
  972. char tmpbuf[256];
  973. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  974. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  975. sleep(2);
  976. //Init Eth0 for internet
  977. memset(tmpbuf,0,256);
  978. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  979. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  980. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  981. system(tmpbuf);
  982. memset(tmpbuf,0,256);
  983. sprintf(tmpbuf,"route add default gw %s eth0 ",
  984. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  985. system(tmpbuf);
  986. //system("ifconfig lo up");
  987. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  988. //Init Eth1 for administrator tool
  989. memset(tmpbuf,0,256);
  990. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  991. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  992. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  993. system(tmpbuf);
  994. //Run DHCP client if enabled
  995. system("killall udhcpc");
  996. system("rm -rf /etc/resolv.conf");
  997. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  998. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  999. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1000. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1001. {
  1002. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1003. system(tmpbuf);
  1004. }
  1005. //Upgrade system id to /etc/hostname
  1006. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1007. system(tmpbuf);
  1008. // DO360 DHCP Server
  1009. InitialDispenserDhcpServerConfig();
  1010. StartDispenserDhcpServer();
  1011. pid_t pid = fork();
  1012. uint8_t cnt_pingDNS_Fail;
  1013. if(pid == 0)
  1014. {
  1015. for(;;)
  1016. {
  1017. if (isRouteFail())
  1018. {
  1019. PRINTF_FUNC("eth0 not in route, restart eht0. \n");
  1020. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1021. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1022. {
  1023. InitialDHCP();
  1024. }
  1025. }
  1026. if(isReachableInternet() == PASS)
  1027. {
  1028. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet= NO;
  1029. cnt_pingDNS_Fail = 0;
  1030. }
  1031. else
  1032. {
  1033. if(++cnt_pingDNS_Fail > 3)
  1034. {
  1035. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1036. }
  1037. }
  1038. bool ethResult = YES;
  1039. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  1040. ethResult = NO;
  1041. else
  1042. {
  1043. system("/sbin/ifmetric eth0 0");
  1044. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') ||
  1045. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1046. system("/sbin/ifmetric mlan0 1");
  1047. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') ||
  1048. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1049. system("/sbin/ifmetric ppp0 2");
  1050. }
  1051. if (!ethResult &&
  1052. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1053. {
  1054. ethResult = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi;
  1055. if (ethResult)
  1056. {
  1057. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1058. {
  1059. system("/sbin/ifmetric eth0 1");
  1060. system("/sbin/ifmetric mlan0 0");
  1061. }
  1062. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1063. system("/sbin/ifmetric ppp0 2");
  1064. }
  1065. }
  1066. if (!ethResult &&
  1067. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1068. {
  1069. ethResult = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi;
  1070. if (ethResult)
  1071. {
  1072. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1073. system("/sbin/ifmetric mlan0 2");
  1074. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1075. {
  1076. system("/sbin/ifmetric eth0 1");
  1077. system("/sbin/ifmetric ppp0 0");
  1078. }
  1079. }
  1080. }
  1081. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1082. ShmStatusCodeData->InfoCode.InfoEvents.bits.WiFiDisable = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == false ? true : false;
  1083. ShmStatusCodeData->InfoCode.InfoEvents.bits.Telocom4GModuleDisable = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == false ? true : false;
  1084. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet = ShmOCPP16Data->OcppConnStatus == false ? true : false;
  1085. sleep(5);
  1086. }
  1087. }
  1088. #ifdef SystemLogMessage
  1089. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK. \n");
  1090. #endif
  1091. }
  1092. /*
  1093. int InitialRfidPort()
  1094. {
  1095. int uartO2 = open(rfidPortName, O_RDWR);
  1096. struct termios tios;
  1097. if (uartO2 != FAIL)
  1098. {
  1099. ioctl (uartO2, TCGETS, &tios);
  1100. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1101. tios.c_lflag = 0;
  1102. tios.c_iflag = 0;
  1103. tios.c_oflag = 0;
  1104. tios.c_cc[VMIN] = 0;
  1105. tios.c_cc[VTIME] = (unsigned char) 1;
  1106. tios.c_lflag = 0;
  1107. tcflush(uartO2, TCIFLUSH);
  1108. ioctl(uartO2, TCSETS, &tios);
  1109. }
  1110. if (uartO2 < 0)
  1111. {
  1112. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1113. }
  1114. return uartO2;
  1115. }
  1116. */
  1117. void GetMacAddress()
  1118. {
  1119. for (byte index = 0; index < 2; index++)
  1120. {
  1121. int fd;
  1122. struct ifreq ifr;
  1123. char tarEth[5];
  1124. char Mac[18];
  1125. sprintf(tarEth,"eth%d",index);
  1126. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1127. ifr.ifr_addr.sa_family = AF_INET;
  1128. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1129. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1130. close(fd);
  1131. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1132. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1133. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1134. if (index == 0)
  1135. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1136. else
  1137. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1138. }
  1139. }
  1140. void GetFirmwareVersion()
  1141. {
  1142. // Get CSU root file system version
  1143. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1144. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1145. for(uint8_t idx=0;idx<3;idx++)
  1146. {
  1147. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  1148. {
  1149. chademo++;
  1150. count++;
  1151. }
  1152. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1153. {
  1154. gb++;
  1155. count++;
  1156. }
  1157. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1158. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  1159. {
  1160. ccs++;
  1161. count++;
  1162. }
  1163. }
  1164. if (count == 1)
  1165. {
  1166. if (chademo > 0)
  1167. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1168. else if (ccs > 0)
  1169. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1170. else if (gb > 0)
  1171. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1172. }
  1173. else
  1174. {
  1175. if (chademo > 0 && ccs > 0)
  1176. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1177. else if (chademo > 0 && gb > 0)
  1178. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1179. else if (ccs > 0 && gb > 0)
  1180. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1181. }
  1182. // Get network option from model name
  1183. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1184. {
  1185. case 'B':
  1186. case 'U':
  1187. //Blue tooth
  1188. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1189. break;
  1190. case 'W':
  1191. // WIFI
  1192. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1193. break;
  1194. case 'T':
  1195. // 3G/4G
  1196. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1197. break;
  1198. default:
  1199. // LAN
  1200. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1201. break;
  1202. }
  1203. // Get rating power from model name
  1204. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1205. // Get IEC or UL
  1206. char _buf[3] = {0};
  1207. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1208. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL)
  1209. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1210. else if (strcmp(_buf, "WU") == EQUAL)
  1211. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1212. }
  1213. void InitialShareMemoryInfo()
  1214. {
  1215. FILE *fp;
  1216. char cmd[512];
  1217. char buf[512];
  1218. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1219. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1220. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1221. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1222. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1223. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1224. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1225. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1226. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1227. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1228. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1229. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1230. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1231. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1232. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1233. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1234. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1235. sprintf(cmd, "/bin/uname -r");
  1236. fp = popen(cmd, "r");
  1237. if(fp == NULL)
  1238. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1239. else
  1240. {
  1241. while(fgets(buf, sizeof(buf), fp) != NULL)
  1242. {
  1243. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1244. }
  1245. }
  1246. // 雙槍 CCS + Chademo
  1247. GetFirmwareVersion();
  1248. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1249. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1250. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1251. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1252. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1253. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1254. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1255. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1256. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1257. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1258. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1259. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1260. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1261. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, " "); //DO360 RC2
  1262. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, " "); //DO360 RC2
  1263. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1264. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, " ");
  1265. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector1FwRev, " ");
  1266. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector2FwRev, " ");
  1267. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1268. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1269. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1270. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1271. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  1272. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  1273. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1274. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1275. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1276. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1277. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1278. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1279. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1280. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1281. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1282. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1283. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1284. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev)); //DO360 RC2
  1285. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1286. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  1287. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  1288. ShmFanModuleData->SelfTest_Comp = NO;
  1289. ShmLedModuleData->SelfTest_Comp = NO;
  1290. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1291. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1292. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1293. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1294. ShmFanModuleData->TestFanSpeed = 0;
  1295. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1296. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1297. char EvsePower[2];
  1298. EvsePower[2] = '\0';
  1299. unsigned short buf_pow = 0;
  1300. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 6)
  1301. {
  1302. strncpy(EvsePower, (char *)(ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  1303. if (strcmp(EvsePower, "15") == EQUAL)
  1304. buf_pow = 150;
  1305. else if (strcmp(EvsePower, "30") == EQUAL)
  1306. buf_pow = 30;
  1307. else if (strcmp(EvsePower, "60") == EQUAL)
  1308. buf_pow = 60;
  1309. else if (strcmp(EvsePower, "12") == EQUAL)
  1310. buf_pow = 120;
  1311. else if (strcmp(EvsePower, "18") == EQUAL)
  1312. buf_pow = 180;
  1313. else if (strcmp(EvsePower, "36") == EQUAL)
  1314. buf_pow = 360;
  1315. ShmSysConfigAndInfo->SysConfig.RatingCurrent = (buf_pow / 30) * PSU_MIN_CUR;
  1316. if(ShmSysConfigAndInfo->SysConfig.MaxChargingPower == 0 ||
  1317. ShmSysConfigAndInfo->SysConfig.MaxChargingPower > buf_pow)
  1318. ShmSysConfigAndInfo->SysConfig.MaxChargingPower = buf_pow;
  1319. }
  1320. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1321. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1322. }
  1323. int Initialization()
  1324. {
  1325. // 初始化卡號驗證的 Flag
  1326. ClearAuthorizedFlag();
  1327. // 初始化插槍驗證的 Flag
  1328. ClearDetectPluginFlag();
  1329. // UART 2 for Rfid
  1330. //rfidFd = InitialRfidPort();
  1331. // int pinOut[2] = { 116, 115 };
  1332. for (byte count = 0; count < CONNECTOR_QUANTITY; count++)
  1333. {
  1334. chargingInfo[count]->RemoteStartFlag = NO;
  1335. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartRequest = false;
  1336. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopRequest = false;
  1337. //
  1338. // if (chargingInfo[count]->Type == _Type_Chademo)
  1339. // {
  1340. // gpio_set_value(pinOut[count], 0x00);
  1341. // ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1342. // }
  1343. // else if (chargingInfo[count]->Type == _Type_GB)
  1344. // {
  1345. // gpio_set_value(pinOut[count], 0x00);
  1346. // ShmGBTData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1347. // }
  1348. // else if (chargingInfo[count]->Type == _Type_CCS_2)
  1349. // {
  1350. // if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1351. // {
  1352. // if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1353. // gpio_set_value(pinOut[1], 0x01);
  1354. // else
  1355. // gpio_set_value(pinOut[count], 0x01);
  1356. // ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1357. // }
  1358. // }
  1359. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  1360. }
  1361. // for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  1362. // {
  1363. // ac_chargingInfo[count]->RemoteStartFlag = NO;
  1364. // if (ac_chargingInfo[count]->Type == _Type_AC)
  1365. // {
  1366. // ac_chargingInfo[count]->SelfTest_Comp = NO;
  1367. // strcpy((char *)ShmOCPP16Data->StatusNotification[count + ShmSysConfigAndInfo->SysConfig.TotalConnectorCount].ErrorCode, "NoError");
  1368. // }
  1369. // }
  1370. PRINTF_FUNC("Initialization OK \n");
  1371. return PASS;
  1372. }
  1373. bool InitialSystemDefaultConfig()
  1374. {
  1375. bool result = true;
  1376. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1377. InitGPIO();
  1378. InitEthernet();
  1379. GetMacAddress();
  1380. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1381. // sleep(3);
  1382. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1383. // sleep(1);
  1384. // system("/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up");
  1385. return result;
  1386. }
  1387. /*
  1388. bool DisplaySelfTestFailReason()
  1389. {
  1390. bool result = false;
  1391. // RB、FB、407、EV 小板中有些板子無回應
  1392. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1393. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; result = true; }
  1394. if (ShmFanModuleData->SelfTest_Comp == NO)
  1395. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; result = true; }
  1396. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1397. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; result = true; }
  1398. // if (ShmLedModuleData->SelfTest_Comp == NO)
  1399. // { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES; }
  1400. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1401. {
  1402. if (chargingInfo[index]->Type == _Type_Chademo)
  1403. {
  1404. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1405. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; result = true; }
  1406. }
  1407. else if (chargingInfo[index]->Type == _Type_GB)
  1408. {
  1409. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1410. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; result = true; }
  1411. }
  1412. else if (chargingInfo[index]->Type == _Type_CCS_2)
  1413. {
  1414. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1415. {
  1416. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1417. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; result = true; }
  1418. }
  1419. }
  1420. }
  1421. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1422. {
  1423. // 先借 GBT 顯示
  1424. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  1425. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; result = true; }
  1426. }
  1427. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1428. {
  1429. // AC Contact 未搭上
  1430. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1431. result = true;
  1432. }
  1433. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  1434. {
  1435. // PSU 通訊問題
  1436. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1437. result = true;
  1438. }
  1439. return result;
  1440. }
  1441. */
  1442. void SelfTestRun()
  1443. {
  1444. struct timeval _selfTest_time;
  1445. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  1446. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  1447. ShmFanModuleData->SelfTest_Comp = NO;
  1448. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1449. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1450. gettimeofday(&_selfTest_time, NULL);
  1451. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  1452. {
  1453. ChkPrimaryStatus();
  1454. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  1455. {
  1456. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1457. PRINTF_FUNC("Self Test Fail: WarningInfo Level 2");
  1458. return;
  1459. }
  1460. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  1461. {
  1462. if (ShmPsuData->Work_Step == _NO_WORKING ||
  1463. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  1464. {
  1465. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1466. PRINTF_FUNC("Self Test Fail: PSU No Working");
  1467. return;
  1468. }
  1469. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  1470. {
  1471. case _STEST_VERSION:
  1472. {
  1473. if (ShmFanModuleData->SelfTest_Comp &&
  1474. ShmRelayModuleData[0]->SelfTest_Comp && ShmRelayModuleData[0]->SelfTest_Comp &&
  1475. ShmPrimaryMcuData->SelfTest_Comp)
  1476. {
  1477. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  1478. PRINTF_FUNC("Self test version check ok");
  1479. }
  1480. }
  1481. break;
  1482. case _STEST_AC_CONTACTOR:
  1483. {
  1484. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  1485. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  1486. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  1487. {
  1488. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1489. PRINTF_FUNC("Communication board pass. \n");
  1490. }
  1491. }
  1492. break;
  1493. case _STEST_PSU_DETECT:
  1494. {
  1495. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  1496. {
  1497. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1498. }
  1499. }
  1500. break;
  1501. case _STEST_PSU_CAP:
  1502. {
  1503. // 此測試是要確認當前總輸出能力
  1504. // 如果沒有 PSU 模組請 bypass
  1505. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  1506. {
  1507. sleep(1);
  1508. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1509. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1510. }
  1511. }
  1512. break;
  1513. }
  1514. }
  1515. else
  1516. break;
  1517. usleep(100000);
  1518. if(GetTimeoutValue(_selfTest_time) / uSEC_VAL >= SELFTEST_TIMEOUT)
  1519. {
  1520. _SelfTestTimeout();
  1521. break;
  1522. }
  1523. }
  1524. }
  1525. int SpawnTask()
  1526. {
  1527. sleep(2);
  1528. system("/root/Module_EventLogging &");
  1529. system("/root/Module_PrimaryComm &");
  1530. system("/root/Module_EvComm &");
  1531. //system("/root/Module_LcmControl &");
  1532. system("/root/Module_InternalComm &");
  1533. system("/root/Module_PsuComm &");
  1534. system("/root/Module_ProduceUtils &");
  1535. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  1536. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  1537. {
  1538. system("/root/OcppBackend &");
  1539. }
  1540. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1541. {
  1542. system("/root/Module_4g &");
  1543. }
  1544. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1545. {
  1546. system("/root/Module_Wifi &");
  1547. }
  1548. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  1549. {
  1550. system("/root/Module_4g &");
  1551. system("/root/Module_Wifi &");
  1552. }
  1553. return PASS;
  1554. }
  1555. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1556. {
  1557. int result = PASS;
  1558. int fd,wrd;
  1559. unsigned int i,Chk;
  1560. unsigned char *ptr, *BufTmp;
  1561. Chk=0;
  1562. ptr=(unsigned char *)UsrData;
  1563. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  1564. {
  1565. memset(BufTmp, 0, MtdBlockSize);
  1566. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  1567. for(i=0; i<MtdBlockSize-4; i++)
  1568. Chk+=*(ptr+i);
  1569. memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
  1570. fd = open("/dev/mtdblock10", O_RDWR);
  1571. if (fd > 0)
  1572. {
  1573. wrd=write(fd, BufTmp, MtdBlockSize);
  1574. close(fd);
  1575. if(wrd >= MtdBlockSize)
  1576. {
  1577. fd = open("/dev/mtdblock11", O_RDWR);
  1578. if (fd > 0)
  1579. {
  1580. wrd=write(fd, BufTmp, MtdBlockSize);
  1581. close(fd);
  1582. if(wrd < MtdBlockSize)
  1583. {
  1584. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  1585. result = FAIL;
  1586. }
  1587. }
  1588. else
  1589. {
  1590. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  1591. result = FAIL;
  1592. }
  1593. }
  1594. else
  1595. {
  1596. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  1597. result = FAIL;
  1598. }
  1599. }
  1600. else
  1601. {
  1602. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  1603. result = FAIL;
  1604. }
  1605. }
  1606. else
  1607. {
  1608. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  1609. result = FAIL;
  1610. }
  1611. if(BufTmp != NULL)
  1612. free(BufTmp);
  1613. return result;
  1614. }
  1615. //===============================================
  1616. // Common Detect Chk - Stop Charging ?
  1617. //===============================================
  1618. bool isEvBoardStopChargeFlag(byte gunIndex)
  1619. {
  1620. return chargingInfo[gunIndex]->StopChargeFlag;
  1621. }
  1622. //===============================================
  1623. // 掃描插槍狀況
  1624. //===============================================
  1625. void ClearDetectPluginFlag()
  1626. {
  1627. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  1628. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  1629. {
  1630. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  1631. chargingInfo[gun_index]->RemoteStartFlag = NO;
  1632. }
  1633. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  1634. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1635. }
  1636. void DetectPluginStart()
  1637. {
  1638. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  1639. }
  1640. bool isDetectPlugin()
  1641. {
  1642. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  1643. return YES;
  1644. return NO;
  1645. }
  1646. //===============================================
  1647. // Common Detect Chk - Chademo
  1648. //===============================================
  1649. bool isEvGunLocked_chademo(byte gunIndex)
  1650. {
  1651. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1652. }
  1653. bool isEvContactorWelding_chademo(byte gunIndex)
  1654. {
  1655. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  1656. }
  1657. bool isEvStopReq_chademo(byte gunIndex)
  1658. {
  1659. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  1660. }
  1661. bool isEvStopCharging_chademo(byte gunIndex)
  1662. {
  1663. if (isEvGunLocked_chademo(gunIndex) == NO)
  1664. {
  1665. // 無鎖槍 = 停止
  1666. PRINTF_FUNC("gun locked none (%d) \n", gunIndex);
  1667. return YES;
  1668. }
  1669. return NO;
  1670. }
  1671. byte isPrechargeStatus_chademo(byte gunIndex)
  1672. {
  1673. byte result = 0x00;
  1674. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1675. return result;
  1676. }
  1677. //===============================================
  1678. // Common Detect Chk - GB
  1679. //===============================================
  1680. bool isEvGunLocked_gb(byte gunIndex)
  1681. {
  1682. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1683. }
  1684. bool isEvStopCharging_gb(byte gunIndex)
  1685. {
  1686. if (isEvGunLocked_gb(gunIndex) == NO)
  1687. {
  1688. // 無鎖槍 = 停止
  1689. PRINTF_FUNC("gun locked none. \n");
  1690. return YES;
  1691. }
  1692. return NO;
  1693. }
  1694. byte isPrechargeStatus_gb(byte gunIndex)
  1695. {
  1696. byte result = 0x00;
  1697. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1698. return result;
  1699. }
  1700. //===============================================
  1701. // Common Detect Chk - CCS
  1702. //===============================================
  1703. bool isEvGunLocked_ccs(byte gunIndex)
  1704. {
  1705. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1706. }
  1707. byte isPrechargeStatus_ccs(byte gunIndex)
  1708. {
  1709. byte result = 0x00;
  1710. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1711. {
  1712. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1713. }
  1714. return result;
  1715. }
  1716. bool isEvStopCharging_ccs(byte gunIndex)
  1717. {
  1718. if (isEvGunLocked_ccs(gunIndex) == NO)
  1719. {
  1720. // 無鎖槍 = 停止
  1721. PRINTF_FUNC("gun locked none. \n");
  1722. return YES;
  1723. }
  1724. return NO;
  1725. }
  1726. //===============================================
  1727. // Callback
  1728. //===============================================
  1729. void DisplayChargingInfo()
  1730. {
  1731. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  1732. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  1733. {
  1734. if (chargingInfo[i]->SystemStatus != S_IDLE &&
  1735. chargingInfo[i]->SystemStatus != S_RESERVATION)
  1736. {
  1737. ChangeGunSelectByIndex(i);
  1738. return;
  1739. }
  1740. }
  1741. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  1742. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  1743. ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE)
  1744. {
  1745. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1746. }
  1747. usleep(50000);
  1748. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1749. }
  1750. void _AutoReturnTimeout()
  1751. {
  1752. PRINTF_FUNC("*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  1753. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  1754. {
  1755. ClearDetectPluginFlag();
  1756. }
  1757. // else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  1758. // {
  1759. // DetectPluginStart();
  1760. // }
  1761. usleep(50000);
  1762. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1763. }
  1764. void _SelfTestTimeout(void)
  1765. {
  1766. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  1767. {
  1768. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  1769. {
  1770. setChargerMode(gun_index, MODE_ALARM);
  1771. }
  1772. }
  1773. ShmPsuData->Work_Step = _NO_WORKING;
  1774. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1775. PRINTF_FUNC("Self test timeout. \n");
  1776. }
  1777. void _AuthorizedTimeout()
  1778. {
  1779. if(IsAuthorizingMode())
  1780. {
  1781. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  1782. isCardScan = false;
  1783. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1784. ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1785. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1786. ClearAuthorizedFlag();
  1787. }
  1788. }
  1789. void _DetectPlugInTimeout()
  1790. {
  1791. PRINTF_FUNC("*********** _DetectPlugInTimeout *********** \n");
  1792. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1793. ClearDetectPluginFlag();
  1794. usleep(50000);
  1795. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1796. }
  1797. void _DetectEvChargingEnableTimeout(byte gunIndex)
  1798. {
  1799. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1800. {
  1801. if(!isEvGunLocked_chademo(gunIndex))
  1802. {
  1803. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  1804. }
  1805. }
  1806. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  1807. {
  1808. if(!isEvGunLocked_ccs(gunIndex))
  1809. {
  1810. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  1811. }
  1812. }
  1813. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1814. {
  1815. if(!isEvGunLocked_ccs(gunIndex))
  1816. {
  1817. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  1818. }
  1819. }
  1820. ChargingTerminalProcess(gunIndex);
  1821. _AutoReturnTimeout();
  1822. }
  1823. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  1824. {
  1825. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  1826. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  1827. {
  1828. setChargerMode(gunIndex, MODE_IDLE);
  1829. _AutoReturnTimeout();
  1830. }
  1831. }
  1832. void _PrepareTimeout(byte gunIndex)
  1833. {
  1834. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  1835. setChargerMode(gunIndex, MODE_IDLE);
  1836. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  1837. _AutoReturnTimeout();
  1838. }
  1839. void _CcsPrechargeTimeout(byte gunIndex)
  1840. {
  1841. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  1842. setChargerMode(gunIndex, MODE_IDLE);
  1843. }
  1844. //===============================================
  1845. // 取得卡號與卡號驗證
  1846. //===============================================
  1847. bool canStartCharging()
  1848. {
  1849. char buf2[16] = "";
  1850. memset(buf2, 0, ARRAY_SIZE(buf2));
  1851. for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++)
  1852. {
  1853. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  1854. }
  1855. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  1856. // 因為無法得知實際的長度,所以只能用搜尋的方式
  1857. if(strcmp(buf2, "Accepted") == EQUAL)
  1858. return true;
  1859. else
  1860. {
  1861. }
  1862. return false;
  1863. }
  1864. void AuthorizingStart()
  1865. {
  1866. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1867. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  1868. }
  1869. void ClearAuthorizedFlag()
  1870. {
  1871. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1872. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  1873. }
  1874. bool isAuthorizedComplete()
  1875. {
  1876. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
  1877. return false;
  1878. return true;
  1879. }
  1880. bool IsAuthorizingMode()
  1881. {
  1882. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  1883. return false;
  1884. return true;
  1885. }
  1886. //===============================================
  1887. // 紀錄 Alarm Code
  1888. //===============================================
  1889. void RecordAlarmCode(byte gunIndex, char *code)
  1890. {
  1891. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  1892. if (strcmp(code, "012234") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  1893. if (strcmp(code, "012235") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  1894. if (strcmp(code, "012236") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  1895. if (strcmp(code, "012288") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  1896. if (strcmp(code, "012289") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  1897. if (strcmp(code, "012290") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  1898. }
  1899. void RecordWarningCode(byte gunIndex, char *code)
  1900. {
  1901. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, code, 6);
  1902. if (strcmp(code, "012296") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  1903. if (strcmp(code, "012297") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  1904. if (strcmp(code, "012298") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  1905. }
  1906. void ReleaseAlarmCode(byte gunIndex)
  1907. {
  1908. bool isCleanCheck = false;
  1909. char code[7];
  1910. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1911. {
  1912. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL &&
  1913. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  1914. {
  1915. memcpy(code, "012234", 6);
  1916. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1917. isCleanCheck = true;
  1918. }
  1919. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL &&
  1920. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail == YES)
  1921. {
  1922. memcpy(code, "012289", 6);
  1923. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1924. isCleanCheck = true;
  1925. }
  1926. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES)
  1927. {
  1928. ReleaseWarningCodeByString(gunIndex, "012296");
  1929. }
  1930. }
  1931. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  1932. {
  1933. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL &&
  1934. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  1935. {
  1936. memcpy(code, "012236", 6);
  1937. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1938. isCleanCheck = true;
  1939. }
  1940. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL &&
  1941. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail == YES)
  1942. {
  1943. memcpy(code, "012290", 6);
  1944. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1945. isCleanCheck = true;
  1946. }
  1947. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES)
  1948. {
  1949. ReleaseWarningCodeByString(gunIndex, "012298");
  1950. }
  1951. }
  1952. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1953. {
  1954. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL &&
  1955. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  1956. {
  1957. memcpy(code, "012235", 6);
  1958. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1959. isCleanCheck = true;
  1960. }
  1961. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL &&
  1962. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail == YES)
  1963. {
  1964. memcpy(code, "012288", 6);
  1965. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1966. isCleanCheck = true;
  1967. }
  1968. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES)
  1969. {
  1970. ReleaseWarningCodeByString(gunIndex, "012297");
  1971. }
  1972. }
  1973. if (isCleanCheck)
  1974. {
  1975. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1976. {
  1977. if (index != gunIndex || ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1978. {
  1979. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, code, 6) != EQUAL)
  1980. {
  1981. if (strncmp(code, "012234", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  1982. if (strncmp(code, "012289", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  1983. if (strncmp(code, "012236", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  1984. if (strncmp(code, "012290", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  1985. if (strncmp(code, "012235", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  1986. if (strncmp(code, "012288", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  1987. }
  1988. }
  1989. }
  1990. }
  1991. }
  1992. void ReleaseWarningCodeByString(byte gunIndex, char *code)
  1993. {
  1994. bool isCleanCheck = false;
  1995. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1996. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES)
  1997. {
  1998. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1999. isCleanCheck = true;
  2000. }
  2001. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  2002. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES)
  2003. {
  2004. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  2005. isCleanCheck = true;
  2006. }
  2007. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  2008. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES)
  2009. {
  2010. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  2011. isCleanCheck = true;
  2012. }
  2013. if (isCleanCheck)
  2014. {
  2015. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2016. {
  2017. if (index != gunIndex || ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2018. {
  2019. if (strncmp((char *)chargingInfo[index]->ConnectorWarningCode, code, 6) != EQUAL)
  2020. {
  2021. if (strncmp(code, "012296", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  2022. if (strncmp(code, "012297", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  2023. if (strncmp(code, "012298", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  2024. }
  2025. }
  2026. }
  2027. }
  2028. }
  2029. //===============================================
  2030. // EmergencyStop and Charging Stop
  2031. //===============================================
  2032. void ChargingTerminalProcess(byte gunIndex)
  2033. {
  2034. setChargerMode(gunIndex, MODE_TERMINATING);
  2035. }
  2036. void AcChargingTerminalProcess()
  2037. {
  2038. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2039. }
  2040. void StopChargingProcessByString(byte level)
  2041. {
  2042. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2043. {
  2044. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2045. }
  2046. }
  2047. void ReleaseChargingProcessByString(byte level)
  2048. {
  2049. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2050. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2051. }
  2052. // 一般錯誤停止充電處理函式
  2053. void BoardErrOccurByString(byte index, char *code)
  2054. {
  2055. byte level = 1;
  2056. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2057. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2058. {
  2059. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2060. {
  2061. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2062. }
  2063. ChargingTerminalProcess(index);
  2064. }
  2065. StopChargingProcessByString(level);
  2066. }
  2067. // 急停狀況的停止充電處理函式
  2068. void EmcOccureByString(char *code)
  2069. {
  2070. byte level = 2;
  2071. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2072. // 其錯誤等級為 2
  2073. if (strncmp(code, "012251", 6) == EQUAL || strncmp(code, "012252", 6) == EQUAL ||
  2074. strncmp(code, "012238", 6) == EQUAL)
  2075. {
  2076. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2077. {
  2078. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2079. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2080. {
  2081. ChargingTerminalProcess(gun);
  2082. }
  2083. }
  2084. StopChargingProcessByString(level);
  2085. InformOcppErrOccur(4);
  2086. }
  2087. }
  2088. void ReleaseBoardErrOccurByString(byte index, char *code)
  2089. {
  2090. bool isTrigger = false;
  2091. byte level = 1;
  2092. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  2093. {
  2094. isTrigger = true;
  2095. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2096. }
  2097. if (isTrigger)
  2098. {
  2099. ReleaseChargingProcessByString(level);
  2100. }
  2101. }
  2102. void ReleaseEmsOccureByString(byte index, char *code)
  2103. {
  2104. bool isTrigger = false;
  2105. byte level = 2;
  2106. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2107. {
  2108. isTrigger = true;
  2109. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2110. }
  2111. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2112. {
  2113. isTrigger = true;
  2114. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2115. }
  2116. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2117. {
  2118. isTrigger = true;
  2119. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2120. }
  2121. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2122. {
  2123. isTrigger = true;
  2124. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2125. }
  2126. if (isTrigger)
  2127. {
  2128. ReleaseChargingProcessByString(level);
  2129. InformOcppErrOccur(6);
  2130. }
  2131. }
  2132. //===============================================
  2133. // 確認硬體 (按鈕) 狀態
  2134. //===============================================
  2135. bool leftBtnPush = false;
  2136. bool rightBtnPush = false;
  2137. void ChkPrimaryStatus()
  2138. {
  2139. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2140. {
  2141. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2142. EmcOccureByString("012251");
  2143. }
  2144. else
  2145. ReleaseEmsOccureByString(0, "012251");
  2146. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2147. {
  2148. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2149. EmcOccureByString("012238");
  2150. }
  2151. else
  2152. ReleaseEmsOccureByString(0, "012238");
  2153. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2154. {
  2155. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2156. }
  2157. else
  2158. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2159. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2160. {
  2161. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2162. EmcOccureByString("012252");
  2163. }
  2164. else
  2165. ReleaseEmsOccureByString(0, "012252");
  2166. /*
  2167. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  2168. {
  2169. if(!leftBtnPush)
  2170. {
  2171. leftBtnPush = true;
  2172. PRINTF_FUNC("left btn down............................... \n");
  2173. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2174. {
  2175. switch(ac_chargingInfo[0]->SystemStatus)
  2176. {
  2177. case S_IDLE:
  2178. {
  2179. if(isDetectPlugin())
  2180. {
  2181. _DetectPlugInTimeout();
  2182. StopSystemTimeoutDet();
  2183. }
  2184. }
  2185. break;
  2186. case S_REASSIGN_CHECK:
  2187. case S_REASSIGN:
  2188. case S_PREPARNING:
  2189. case S_PREPARING_FOR_EV:
  2190. case S_PREPARING_FOR_EVSE:
  2191. case S_CCS_PRECHARGE_ST0:
  2192. case S_CCS_PRECHARGE_ST1:
  2193. {
  2194. // 取消充電
  2195. AcChargingTerminalProcess();
  2196. }
  2197. break;
  2198. case S_CHARGING:
  2199. {
  2200. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  2201. {
  2202. // 停止充電
  2203. AcChargingTerminalProcess();
  2204. }
  2205. }
  2206. break;
  2207. case S_COMPLETE:
  2208. {}
  2209. break;
  2210. }
  2211. }
  2212. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2213. {
  2214. case S_IDLE:
  2215. {
  2216. if(isDetectPlugin())
  2217. {
  2218. _DetectPlugInTimeout();
  2219. StopSystemTimeoutDet();
  2220. }
  2221. }
  2222. break;
  2223. case S_REASSIGN_CHECK:
  2224. case S_REASSIGN:
  2225. case S_PREPARNING:
  2226. case S_PREPARING_FOR_EV:
  2227. case S_PREPARING_FOR_EVSE:
  2228. case S_CCS_PRECHARGE_ST0:
  2229. case S_CCS_PRECHARGE_ST1:
  2230. {
  2231. // 取消充電
  2232. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2233. AcChargingTerminalProcess();
  2234. else
  2235. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2236. }
  2237. break;
  2238. case S_CHARGING:
  2239. {
  2240. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  2241. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->isRemoteStart == NO)
  2242. {
  2243. // 停止充電
  2244. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2245. }
  2246. }
  2247. break;
  2248. case S_COMPLETE:
  2249. {
  2250. // 回 IDLE
  2251. //PRINTF_FUNC("right btn down.................S_COMPLETE \n");
  2252. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_IDLE;
  2253. }
  2254. break;
  2255. }
  2256. }
  2257. }
  2258. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  2259. {
  2260. if(leftBtnPush)
  2261. {
  2262. leftBtnPush = false;
  2263. PRINTF_FUNC("left btn up............................... \n");
  2264. }
  2265. }
  2266. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  2267. {
  2268. if(!rightBtnPush)
  2269. {
  2270. rightBtnPush = true;
  2271. PRINTF_FUNC("right btn down............................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2272. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2273. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  2274. {
  2275. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2276. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2277. }
  2278. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2279. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)
  2280. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2281. else if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2282. {
  2283. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  2284. {
  2285. if (chargingInfo[_index]->SystemStatus != S_BOOTING &&
  2286. chargingInfo[_index]->SystemStatus != S_IDLE &&
  2287. chargingInfo[_index]->SystemStatus != S_RESERVATION)
  2288. {
  2289. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  2290. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2291. return;
  2292. }
  2293. }
  2294. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2295. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2296. }
  2297. else
  2298. {
  2299. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2300. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2301. }
  2302. }
  2303. }
  2304. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  2305. {
  2306. if(rightBtnPush)
  2307. {
  2308. rightBtnPush = false;
  2309. PRINTF_FUNC("right btn up............................... \n");
  2310. }
  2311. }
  2312. */
  2313. }
  2314. //===============================================
  2315. // 確認各小板偵測的錯誤狀況
  2316. //===============================================
  2317. void CheckErrorOccurStatus(byte index)
  2318. {
  2319. // 小板
  2320. if (chargingInfo[index]->Type == _Type_Chademo)
  2321. {
  2322. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
  2323. BoardErrOccurByString(index, "011012");
  2324. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  2325. // BoardErrOccurByString(index, "012234");
  2326. }
  2327. else if (chargingInfo[index]->Type == _Type_GB)
  2328. {
  2329. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault == YES)
  2330. BoardErrOccurByString(index, "011016");
  2331. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  2332. // BoardErrOccurByString(index, "012236");
  2333. }
  2334. else if (chargingInfo[index]->Type == _Type_CCS_2)
  2335. {
  2336. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
  2337. BoardErrOccurByString(index, "011014");
  2338. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  2339. // BoardErrOccurByString(index, "012235");
  2340. }
  2341. // RB
  2342. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2343. {
  2344. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2345. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2346. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2347. {
  2348. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2349. {
  2350. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2351. StopChargingProcessByString(2);
  2352. InformOcppErrOccur(13);
  2353. }
  2354. }
  2355. else
  2356. {
  2357. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2358. {
  2359. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2360. ReleaseChargingProcessByString(2);
  2361. InformOcppErrOccur(6);
  2362. }
  2363. }
  2364. }
  2365. else
  2366. {
  2367. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2368. {
  2369. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2370. ReleaseChargingProcessByString(2);
  2371. InformOcppErrOccur(6);
  2372. }
  2373. }
  2374. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2375. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2376. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2377. {
  2378. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2379. {
  2380. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2381. StopChargingProcessByString(2);
  2382. InformOcppErrOccur(14);
  2383. }
  2384. }
  2385. else
  2386. {
  2387. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2388. {
  2389. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2390. ReleaseChargingProcessByString(2);
  2391. InformOcppErrOccur(6);
  2392. }
  2393. }
  2394. }
  2395. /*
  2396. //===============================================
  2397. // 確認 GPIO 狀態
  2398. //===============================================
  2399. void gpio_set_value(unsigned int gpio, unsigned int value)
  2400. {
  2401. int fd;
  2402. char buf[MAX_BUF];
  2403. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2404. fd = open(buf, O_WRONLY);
  2405. if (fd < 0)
  2406. {
  2407. perror("gpio/set-value");
  2408. return;
  2409. }
  2410. if (value)
  2411. write(fd, "1", 2);
  2412. else
  2413. write(fd, "0", 2);
  2414. close(fd);
  2415. }
  2416. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2417. {
  2418. int fd;
  2419. char buf[MAX_BUF];
  2420. char ch;
  2421. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2422. fd = open(buf, O_RDONLY);
  2423. if (fd < 0) {
  2424. perror("gpio/get-value");
  2425. return fd;
  2426. }
  2427. read(fd, &ch, 1);
  2428. if (ch != '0') {
  2429. *value = 1;
  2430. } else {
  2431. *value = 0;
  2432. }
  2433. close(fd);
  2434. return 0;
  2435. }
  2436. void CheckGunTypeFromHw()
  2437. {
  2438. int pinIn[4] = { 22, 23, 44, 45 };
  2439. unsigned int gpioValue = 0;
  2440. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  2441. gpio_get_value(pinIn[i], &gpioValue);
  2442. {
  2443. switch (pinIn[i])
  2444. {
  2445. case 22:
  2446. bd1_1_status = gpioValue;
  2447. break;
  2448. case 23:
  2449. bd1_2_status = gpioValue;
  2450. break;
  2451. case 44:
  2452. bd0_1_status = gpioValue;
  2453. break;
  2454. case 45:
  2455. bd0_2_status = gpioValue;
  2456. break;
  2457. }
  2458. }
  2459. }
  2460. }
  2461. void CheckGpioInStatus()
  2462. {
  2463. int pinIn[2] = { 27, 47 };
  2464. unsigned int gpioValue = 0;
  2465. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  2466. {
  2467. gpio_get_value(pinIn[i], &gpioValue);
  2468. if (gpioValue == 0x01)
  2469. {
  2470. switch(pinIn[i])
  2471. {
  2472. // 小板緊急停止
  2473. case 47:
  2474. {
  2475. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2476. {
  2477. if (chargingInfo[i]->slotsIndex == 1)
  2478. {
  2479. if (chargingInfo[i]->Type == _Type_Chademo)
  2480. BoardErrOccurByString(i, "023730");
  2481. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2482. BoardErrOccurByString(i, "013627");
  2483. break;
  2484. }
  2485. }
  2486. }
  2487. break;
  2488. case 27:
  2489. {
  2490. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2491. {
  2492. if (chargingInfo[i]->slotsIndex == 3)
  2493. {
  2494. if (chargingInfo[i]->Type == _Type_Chademo)
  2495. BoardErrOccurByString(i, "023730");
  2496. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2497. BoardErrOccurByString(i, "013627");
  2498. break;
  2499. }
  2500. }
  2501. }
  2502. break;
  2503. }
  2504. }
  2505. else
  2506. {
  2507. switch (pinIn[i])
  2508. {
  2509. // 小板解除緊急停止
  2510. case 47:
  2511. {
  2512. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2513. {
  2514. if (chargingInfo[i]->slotsIndex == 1)
  2515. {
  2516. if (chargingInfo[i]->Type == _Type_Chademo)
  2517. ReleaseBoardErrOccurByString(i, "023730");
  2518. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2519. ReleaseBoardErrOccurByString(i, "013627");
  2520. break;
  2521. }
  2522. }
  2523. }
  2524. break;
  2525. case 27:
  2526. {
  2527. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2528. {
  2529. if (chargingInfo[i]->slotsIndex == 3)
  2530. {
  2531. if (chargingInfo[i]->Type == _Type_Chademo)
  2532. ReleaseBoardErrOccurByString(i, "023730");
  2533. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2534. ReleaseBoardErrOccurByString(i, "013627");
  2535. break;
  2536. }
  2537. }
  2538. }
  2539. break;
  2540. }
  2541. }
  2542. }
  2543. }
  2544. */
  2545. //===============================================
  2546. // Main process
  2547. //===============================================
  2548. // 檢查 Byte 中某個 Bit 的值
  2549. // _byte : 欲改變的 byte
  2550. // _bit : 該 byte 的第幾個 bit
  2551. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  2552. {
  2553. return ( _byte & mask_table[_bit] ) != 0x00;
  2554. }
  2555. // 設定 Byte 中某個 Bit的值
  2556. // _byte : 欲改變的 byte
  2557. // _bit : 該 byte 的第幾個 bit
  2558. // value : 修改的值為 0 or 1
  2559. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  2560. {
  2561. if(value == 1)
  2562. *_byte |= (1 << _bit);
  2563. else if (value == 0)
  2564. *_byte ^= (1 << _bit);
  2565. }
  2566. void UserScanFunction()
  2567. {
  2568. bool idleReq = false;
  2569. unsigned char idleIndex = 255;
  2570. // unsigned char stopReq = 255;
  2571. // 當前非驗證的狀態
  2572. if(!IsAuthorizingMode())
  2573. {
  2574. // 先判斷現在是否可以提供刷卡
  2575. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  2576. // 2. 停止充電
  2577. // if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX)
  2578. // {
  2579. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2580. // return;
  2581. // }
  2582. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2583. {
  2584. // if (chargingInfo[i]->SystemStatus == S_CHARGING)
  2585. // {
  2586. // stopReq = i;
  2587. // }
  2588. // if ((chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable == YES) ||
  2589. // (_acgunIndex > 0 && ac_chargingInfo[0]->SystemStatus == S_IDLE && ac_chargingInfo[0]->IsAvailable == YES))
  2590. // {
  2591. // idleReq = true;
  2592. // idleIndex = i;
  2593. // }
  2594. if(chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable == YES)
  2595. {
  2596. idleReq = true;
  2597. idleIndex = i;
  2598. }
  2599. }
  2600. // if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2601. // ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  2602. // {
  2603. // stopReq = DEFAULT_AC_INDEX;
  2604. // }
  2605. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  2606. {
  2607. // if (_acgunIndex > 0 && stopReq == DEFAULT_AC_INDEX && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  2608. // {
  2609. // char value[32];
  2610. // PRINTF_FUNC("ac stop charging \n");
  2611. // PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  2612. // ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2613. // memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId,
  2614. // ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  2615. // if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  2616. // {
  2617. // AcChargingTerminalProcess();
  2618. // }
  2619. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2620. // }
  2621. /*
  2622. if (stopReq < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2623. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING &&
  2624. (_acgunIndex <= 0 || (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)))
  2625. {
  2626. char value[32];
  2627. PRINTF_FUNC("stop charging \n");
  2628. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  2629. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2630. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  2631. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  2632. // 同一張卡直接停掉
  2633. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  2634. {
  2635. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2636. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2637. }
  2638. else
  2639. {
  2640. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2641. // // 進驗證
  2642. // if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  2643. // {
  2644. // _authorizeIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc;
  2645. // }
  2646. // else
  2647. // {
  2648. // _authorizeIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  2649. // }
  2650. //
  2651. // StartSystemTimeoutDet(Timeout_AuthorizingForStop);
  2652. // AuthorizingStart();
  2653. }
  2654. }
  2655. */
  2656. if (idleReq)
  2657. {
  2658. // if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  2659. // stopReq != 255 &&
  2660. // ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2661. // {
  2662. // idleReq = false;
  2663. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2664. // }
  2665. if ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  2666. (idleIndex != 255 && chargingInfo[idleIndex]->SystemStatus == S_IDLE))
  2667. {
  2668. PRINTF_FUNC("// LCM => Authorizing \n");
  2669. // LCM => Authorizing
  2670. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  2671. // 進入確認卡號狀態
  2672. AuthorizingStart();
  2673. }
  2674. else
  2675. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2676. }
  2677. else
  2678. {
  2679. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2680. }
  2681. }
  2682. }
  2683. else
  2684. {
  2685. // 透過後臺停止充電的判斷
  2686. if (isAuthorizedComplete() ||
  2687. (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  2688. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  2689. {
  2690. // 判斷後台回覆狀態
  2691. if(canStartCharging() ||
  2692. (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  2693. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  2694. {
  2695. if (_authorizeIndex != NO_DEFINE)
  2696. {
  2697. // 先找 AC
  2698. if (_authorizeIndex == DEFAULT_AC_INDEX)
  2699. {
  2700. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  2701. strcmp((char *)chargingInfo[_authorizeIndex]->StartUserId, "") != EQUAL)
  2702. {
  2703. AcChargingTerminalProcess();
  2704. }
  2705. }
  2706. else
  2707. {
  2708. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  2709. strcmp((char *)chargingInfo[_authorizeIndex]->StartUserId, "") != EQUAL)
  2710. {
  2711. ChargingTerminalProcess(_authorizeIndex);
  2712. }
  2713. }
  2714. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2715. _authorizeIndex = NO_DEFINE;
  2716. }
  2717. }
  2718. else
  2719. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2720. PRINTF_FUNC("ClearAuthorizedFlag");
  2721. ClearAuthorizedFlag();
  2722. }
  2723. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  2724. {
  2725. // 白名單驗證
  2726. for (int i = 0; i < 10; i++)
  2727. {
  2728. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  2729. {
  2730. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  2731. {
  2732. ChargingTerminalProcess(_authorizeIndex);
  2733. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2734. ClearAuthorizedFlag();
  2735. break;
  2736. }
  2737. }
  2738. }
  2739. }
  2740. }
  2741. }
  2742. unsigned char isModeChange(unsigned char gun_index)
  2743. {
  2744. unsigned char result = NO;
  2745. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  2746. {
  2747. result = YES;
  2748. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  2749. }
  2750. return result;
  2751. }
  2752. /*
  2753. void ScannerCardProcess()
  2754. {
  2755. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != 2 &&
  2756. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  2757. {
  2758. isCardScan = true;
  2759. // 處理刷卡及驗證卡號的動作
  2760. UserScanFunction();
  2761. }
  2762. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  2763. {
  2764. StartSystemTimeoutDet(Timeout_Authorizing);
  2765. // 確認驗證卡號完成沒
  2766. if (isAuthorizedComplete() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2767. {
  2768. StopSystemTimeoutDet();
  2769. // 判斷後台回覆狀態
  2770. if(canStartCharging() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2771. {
  2772. // LCM => Authorize complete
  2773. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2774. PRINTF_FUNC("User ID Authorized Completed");
  2775. }
  2776. else
  2777. {
  2778. // LCM => Authorize fail
  2779. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2780. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2781. }
  2782. ClearAuthorizedFlag();
  2783. }
  2784. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  2785. {
  2786. // 白名單驗證
  2787. for (int i = 0; i < 10; i++)
  2788. {
  2789. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  2790. {
  2791. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  2792. {
  2793. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2794. ClearAuthorizedFlag();
  2795. break;
  2796. }
  2797. }
  2798. }
  2799. }
  2800. }
  2801. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  2802. {
  2803. StartSystemTimeoutDet(Timeout_VerifyFail);
  2804. isCardScan = false;
  2805. }
  2806. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2807. {
  2808. StartSystemTimeoutDet(Timeout_VerifyComp);
  2809. }
  2810. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2811. {
  2812. StartSystemTimeoutDet(Timeout_WaitPlug);
  2813. }
  2814. else
  2815. isCardScan = false;
  2816. }
  2817. */
  2818. struct timeval _StartAuthorizing_Time;
  2819. void PowerCabinetAuthorizingSettingInitial(void)
  2820. {
  2821. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize = false;
  2822. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = false;
  2823. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeTargetIndex = 0;
  2824. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.BackendAuthorized = false;
  2825. }
  2826. void FouceAnnounceAccountBalance(unsigned char index, unsigned char AuthorizeSrc)
  2827. {
  2828. switch(AuthorizeSrc)
  2829. {
  2830. case _AuthorizeSrc_Local:
  2831. for(int i = 0; i < 2; i++)
  2832. {
  2833. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] != 0)
  2834. {
  2835. unsigned char ConnectorIndex = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  2836. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AnnounceBalance)
  2837. {
  2838. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AnnounceBalance = true;
  2839. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].AccountBalance = 0;
  2840. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AccountBalanceRequest = true;
  2841. PRINTF_FUNC("Force Announce Account Balance To Dispenser %d Connector %d", index + 1, ConnectorIndex + 1);
  2842. }
  2843. }
  2844. }
  2845. break;
  2846. case _AuthorizeSrc_Remote:
  2847. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  2848. {
  2849. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  2850. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = 0;
  2851. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AccountBalanceRequest = true;
  2852. PRINTF_FUNC("Force Announce Account Balance To Connector %d", index + 1);
  2853. }
  2854. break;
  2855. }
  2856. }
  2857. void AuthorizingSettingInitial(unsigned char index, unsigned char AuthorizeSrc)
  2858. {
  2859. char *str_auth_src[] = {AUTHORIZE_SRC_NONE_DEV, AUTHORIZE_SRC_LOCAL_DEV, AUTHORIZE_SRC_REMOTE_DEV};
  2860. PRINTF_FUNC("*********** %s id %d Authorize Initial ***********", str_auth_src[AuthorizeSrc], index + 1);
  2861. switch(AuthorizeSrc)
  2862. {
  2863. case _AuthorizeSrc_Local:
  2864. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].UserId, 0x00, 32);
  2865. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthorizeStatus = _AuthorizeStatus_Idle;
  2866. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthorizeRequest = false;
  2867. for(int i = 0; i < 2; i++)
  2868. {
  2869. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] != 0)
  2870. {
  2871. unsigned char ConnectorIndex = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  2872. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AuthorizeRequestType != _AuthorizeSrc_None)
  2873. {
  2874. PRINTF_FUNC("*********** Connector id %d AuthorizeReqClean ***********", ConnectorIndex + 1);
  2875. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AuthorizeRequestType = _AuthorizeSrc_None;
  2876. if(chargingInfo[ConnectorIndex]->SystemStatus == S_IDLE)
  2877. {
  2878. PRINTF_FUNC("*********** Connector id %d Clean UserId & UserPrice ***********", ConnectorIndex + 1);
  2879. memset(chargingInfo[ConnectorIndex]->StartUserId, 0x00, 32);
  2880. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].UserPrice = 0;
  2881. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].AccountBalance = -6553500;
  2882. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AnnounceBalance = false;
  2883. }
  2884. }
  2885. }
  2886. }
  2887. break;
  2888. case _AuthorizeSrc_Remote:
  2889. memset(ShmOCPP16Data->RemoteStartTransaction[index].IdTag, 0x00, 20);
  2890. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthorizeStatus = _AuthorizeStatus_Idle;
  2891. ShmOCPP16Data->CsMsg.bits[index].RemoteStartTransactionReq = false;
  2892. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.RemoteStartRequest = false;
  2893. chargingInfo[index]->RemoteStartFlag = false;
  2894. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AuthorizeRequestType != _AuthorizeSrc_None)
  2895. {
  2896. PRINTF_FUNC("*********** Connector id %d AuthorizeReqClean ***********", index + 1);
  2897. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AuthorizeRequestType = _AuthorizeSrc_None;
  2898. if(chargingInfo[index]->SystemStatus == S_IDLE)
  2899. {
  2900. PRINTF_FUNC("*********** Connector id %d Clean UserId & UserPrice ***********", index + 1);
  2901. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  2902. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  2903. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -6553500;
  2904. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  2905. }
  2906. }
  2907. break;
  2908. }
  2909. }
  2910. void SetAuthorizingTarget(unsigned char index, unsigned char AuthorizeSrc)
  2911. {
  2912. BOOL valid = false;
  2913. switch(AuthorizeSrc)
  2914. {
  2915. case _AuthorizeSrc_Local:
  2916. valid = true;
  2917. PRINTF_FUNC("*********** Dispenser id %d Local Authorize ***********", index + 1);
  2918. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, 32);
  2919. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].UserId, 32);
  2920. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthorizeStatus = _AuthorizeStatus_Wait;
  2921. break;
  2922. case _AuthorizeSrc_Remote:
  2923. valid = true;
  2924. PRINTF_FUNC("*********** Connector id %d Remote Authorize ***********", index + 1);
  2925. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, 32);
  2926. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[index].IdTag, 20);
  2927. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthorizeStatus = _AuthorizeStatus_Wait;
  2928. break;
  2929. }
  2930. if(valid)
  2931. {
  2932. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize = true;
  2933. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = false;
  2934. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeTargetIndex = index;
  2935. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc = AuthorizeSrc;
  2936. }
  2937. }
  2938. void SetConnectorAuthorizeOK(unsigned char index, unsigned char AuthorizeSrc)
  2939. {
  2940. switch(AuthorizeSrc)
  2941. {
  2942. case _AuthorizeSrc_Local:
  2943. for(int i = 0; i < 2; i++)
  2944. {
  2945. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] != 0)
  2946. {
  2947. unsigned char ConnectorIndex = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  2948. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].GeneralChargingData.SystemStatus == S_IDLE)
  2949. {
  2950. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AuthorizeRequestType = _AuthorizeSrc_Local;
  2951. memcpy(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].GeneralChargingData.StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  2952. PRINTF_FUNC("*********** Connector id %d LocalAuthorize OK ***********", ConnectorIndex + 1);
  2953. if(!ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.BackendAuthorized)
  2954. {
  2955. // authorized without backend need to trigger announce AccountBalance
  2956. //ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].AccountBalance = 0;
  2957. //ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AccountBalanceRequest = true;
  2958. }
  2959. }
  2960. }
  2961. }
  2962. break;
  2963. case _AuthorizeSrc_Remote:
  2964. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.SystemStatus == S_IDLE)
  2965. {
  2966. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AuthorizeRequestType = _AuthorizeSrc_Remote;
  2967. memcpy(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  2968. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.RemoteStartRequest = true;
  2969. PRINTF_FUNC("*********** Connector id %d RemoteAuthorize OK***********", index + 1);
  2970. }
  2971. break;
  2972. }
  2973. }
  2974. void OCPPAuthorizingStart(void)
  2975. {
  2976. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  2977. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  2978. }
  2979. void AuthorizeTimeoutProcess(void)
  2980. {
  2981. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  2982. {
  2983. switch(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthorizeStatus)
  2984. {
  2985. case _AuthorizeStatus_Pass:
  2986. if(GetTimeoutValue(_ConnectorAuthorizing_Time[index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  2987. {
  2988. FouceAnnounceAccountBalance(index, _AuthorizeSrc_Remote);
  2989. }
  2990. if(GetTimeoutValue(_ConnectorAuthorizing_Time[index]) / uSEC_VAL >= _connectionTimeout)
  2991. {
  2992. PRINTF_FUNC("*********** Connector id %d Plug In Timeout ***********\n", index + 1);
  2993. AuthorizingSettingInitial(index, _AuthorizeSrc_Remote);
  2994. }
  2995. break;
  2996. case _AuthorizeStatus_Fail:
  2997. FouceAnnounceAccountBalance(index, _AuthorizeSrc_Remote);
  2998. if(GetTimeoutValue(_ConnectorAuthorizing_Time[index]) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT)
  2999. {
  3000. PRINTF_FUNC("*********** Connector id %d Authorized Return ***********", index + 1);
  3001. AuthorizingSettingInitial(index, _AuthorizeSrc_Remote);
  3002. }
  3003. break;
  3004. default:
  3005. // do nothing
  3006. break;
  3007. }
  3008. switch(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthorizeStatus)
  3009. {
  3010. case _AuthorizeStatus_Pass:
  3011. if(GetTimeoutValue(_DispenserAuthorizing_Time[index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  3012. {
  3013. FouceAnnounceAccountBalance(index, _AuthorizeSrc_Local);
  3014. }
  3015. if(GetTimeoutValue(_DispenserAuthorizing_Time[index]) / uSEC_VAL >= _connectionTimeout)
  3016. {
  3017. PRINTF_FUNC("*********** Dispenser id %d Plug In Timeout ***********\n", index + 1);
  3018. AuthorizingSettingInitial(index, _AuthorizeSrc_Local);
  3019. }
  3020. break;
  3021. case _AuthorizeStatus_Fail:
  3022. FouceAnnounceAccountBalance(index, _AuthorizeSrc_Local);
  3023. if(GetTimeoutValue(_DispenserAuthorizing_Time[index]) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT)
  3024. {
  3025. PRINTF_FUNC("*********** Dispenser id %d Authorized Return ***********", index + 1);
  3026. AuthorizingSettingInitial(index, _AuthorizeSrc_Local);
  3027. }
  3028. break;
  3029. default:
  3030. // do nothing
  3031. break;
  3032. }
  3033. }
  3034. }
  3035. void PowerCabinetAuthorizeProcess(void)
  3036. {
  3037. char *str_auth_src[] = {AUTHORIZE_SRC_NONE_DEV, AUTHORIZE_SRC_LOCAL_DEV, AUTHORIZE_SRC_REMOTE_DEV};
  3038. BOOL FindRemoteStartRequest = false;
  3039. AuthorizeTimeoutProcess();
  3040. if(!ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize)
  3041. {
  3042. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  3043. {
  3044. if(ShmOCPP16Data->CsMsg.bits[index].RemoteStartTransactionReq == true)
  3045. {
  3046. SetAuthorizingTarget(index, _AuthorizeSrc_Remote);
  3047. ShmOCPP16Data->CsMsg.bits[index].RemoteStartTransactionReq = false;
  3048. chargingInfo[index]->RemoteStartFlag = true;
  3049. FindRemoteStartRequest = true;
  3050. break;
  3051. }
  3052. }
  3053. if(!FindRemoteStartRequest)
  3054. {
  3055. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  3056. {
  3057. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthorizeStatus == _AuthorizeStatus_Idle)
  3058. {
  3059. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthorizeRequest &&
  3060. strlen((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].UserId) > 0)
  3061. {
  3062. SetAuthorizingTarget(index, _AuthorizeSrc_Local);
  3063. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthorizeRequest = false;
  3064. break;
  3065. }
  3066. }
  3067. }
  3068. }
  3069. }
  3070. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize == true &&
  3071. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted == false)
  3072. {
  3073. unsigned char target = ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeTargetIndex;
  3074. unsigned char *AuthorizeStatus;
  3075. switch(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc)
  3076. {
  3077. case _AuthorizeSrc_Local:
  3078. AuthorizeStatus = &ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[target].AuthorizeStatus;
  3079. break;
  3080. case _AuthorizeSrc_Remote:
  3081. AuthorizeStatus = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[target].AuthorizeStatus;
  3082. break;
  3083. default:
  3084. break;
  3085. }
  3086. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc != _AuthorizeSrc_None)
  3087. {
  3088. switch(*AuthorizeStatus)
  3089. {
  3090. case _AuthorizeStatus_Wait:
  3091. *AuthorizeStatus = _AuthorizeStatus_Busy;
  3092. PRINTF_FUNC("*********** %s id %d Start Authorizing ***********",
  3093. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3094. if(ShmOCPP16Data->OcppConnStatus)
  3095. {
  3096. PRINTF_FUNC("*********** %s id %d OPCC AuthorizeReq ***********",
  3097. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3098. OCPPAuthorizingStart();
  3099. }
  3100. // update start authorizing timeout
  3101. gettimeofday(&_StartAuthorizing_Time, NULL);
  3102. break;
  3103. case _AuthorizeStatus_Busy:
  3104. if(GetTimeoutValue(_StartAuthorizing_Time) / uSEC_VAL >= AUTHORIZE_TIMEOUT)
  3105. {
  3106. PRINTF_FUNC("*********** %s ID %d Authorize Timeout ***********",
  3107. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3108. *AuthorizeStatus = _AuthorizeStatus_Fail;
  3109. }
  3110. else
  3111. {
  3112. if(ShmOCPP16Data->OcppConnStatus)
  3113. {
  3114. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf)
  3115. {
  3116. BOOL accept = false;
  3117. if(strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted") == EQUAL)
  3118. {
  3119. accept = true;
  3120. }
  3121. PRINTF_FUNC("*********** %s id %d OCPP Authorize %s ***********",
  3122. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1, accept ? "OK" : "NG");
  3123. *AuthorizeStatus = accept ? _AuthorizeStatus_Pass : _AuthorizeStatus_Fail;
  3124. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.BackendAuthorized = true;
  3125. }
  3126. }
  3127. else
  3128. {
  3129. if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3130. {
  3131. PRINTF_FUNC("*********** %s id %d No Charging ***********",
  3132. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3133. *AuthorizeStatus = _AuthorizeStatus_Fail;
  3134. }
  3135. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  3136. {
  3137. // set authorize pass when offline policy is free charge
  3138. PRINTF_FUNC("*********** %s id %d Free Charging OK ***********",
  3139. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3140. *AuthorizeStatus = _AuthorizeStatus_Pass;
  3141. }
  3142. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3143. {
  3144. BOOL find = false;
  3145. // check white list
  3146. for(int i = 0; i < 10; i++)
  3147. {
  3148. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  3149. {
  3150. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  3151. {
  3152. find = true;
  3153. break;
  3154. }
  3155. }
  3156. }
  3157. PRINTF_FUNC("*********** %s id %d White Card %s ***********",
  3158. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1, find ? "OK" : "NG");
  3159. *AuthorizeStatus = find ? _AuthorizeStatus_Pass : _AuthorizeStatus_Fail;
  3160. }
  3161. else
  3162. {
  3163. PRINTF_FUNC("*********** %s id %d Invalid Policy ***********",
  3164. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3165. *AuthorizeStatus = _AuthorizeStatus_Fail;
  3166. }
  3167. }
  3168. }
  3169. if(*AuthorizeStatus != _AuthorizeStatus_Busy)
  3170. {
  3171. // authorize completed
  3172. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = true;
  3173. if(*AuthorizeStatus == _AuthorizeStatus_Pass)
  3174. {
  3175. SetConnectorAuthorizeOK(target, ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc);
  3176. }
  3177. switch(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc)
  3178. {
  3179. case _AuthorizeSrc_Local:
  3180. // update dispenser authorizing timeout (local start)
  3181. gettimeofday(&_DispenserAuthorizing_Time[target], NULL);
  3182. break;
  3183. case _AuthorizeSrc_Remote:
  3184. // update connector authorizing timeout (remote start)
  3185. gettimeofday(&_ConnectorAuthorizing_Time[target], NULL);
  3186. break;
  3187. default:
  3188. break;
  3189. }
  3190. // update authorizing completed timeout
  3191. gettimeofday(&_StartAuthorizing_Time, NULL);
  3192. CheckConnectionTimeout();
  3193. }
  3194. break;
  3195. case _AuthorizeStatus_Pass:
  3196. case _AuthorizeStatus_Fail:
  3197. break;
  3198. default:
  3199. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize = false;
  3200. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = false;
  3201. break;
  3202. }
  3203. }
  3204. else
  3205. {
  3206. PRINTF_FUNC("*********** PowerCabinet Authorize None Src ***********");
  3207. // authorize completed
  3208. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = true;
  3209. // update authorizing completed timeout
  3210. gettimeofday(&_StartAuthorizing_Time, NULL);
  3211. }
  3212. }
  3213. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted)
  3214. {
  3215. if(GetTimeoutValue(_StartAuthorizing_Time) / uSEC_VAL >= AUTHORIZE_COMP_TIMEOUT)
  3216. {
  3217. PRINTF_FUNC("*********** PowerCabinet Authorize Completed ***********");
  3218. PowerCabinetAuthorizingSettingInitial();
  3219. }
  3220. else
  3221. {
  3222. // do nothing
  3223. }
  3224. }
  3225. }
  3226. BOOL IsConnectorAuthorizeSuccess(unsigned char ConnectorIndex)
  3227. {
  3228. BOOL success = false;
  3229. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AuthorizeRequestType != _AuthorizeSrc_None)
  3230. {
  3231. success = true;
  3232. }
  3233. return success;
  3234. }
  3235. /*
  3236. bool AddGunInfoByConnector(byte typeValue, byte slots)
  3237. {
  3238. bool result = true;
  3239. switch (typeValue)
  3240. {
  3241. case '0': // none
  3242. break;
  3243. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  3244. break;
  3245. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  3246. break;
  3247. case '3': // IEC 62196-2 Type 2 Plug
  3248. case '4': // IEC 62196-2 Type 2 Socket
  3249. if (AC_QUANTITY > _ac_Index)
  3250. {
  3251. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  3252. // AC 固定 index
  3253. ac_chargingInfo[_acgunIndex]->Index = 0;
  3254. ac_chargingInfo[_acgunIndex]->ReservationId = -1;
  3255. ac_chargingInfo[_acgunIndex]->SystemStatus = S_IDLE;
  3256. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  3257. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  3258. _ac_Index++;
  3259. _acgunIndex++;
  3260. }
  3261. else
  3262. result = false;
  3263. break;
  3264. case '5': // GB/T AC Plug
  3265. break;
  3266. case '6': // GB/T AC Socket
  3267. break;
  3268. case 'J': // CHAdeMO
  3269. {
  3270. if (CHAdeMO_QUANTITY > _chademoIndex)
  3271. {
  3272. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  3273. chargingInfo[_gunIndex]->Index = _gunIndex;
  3274. chargingInfo[_gunIndex]->ReservationId = -1;
  3275. chargingInfo[_gunIndex]->slotsIndex = slots;
  3276. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3277. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  3278. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  3279. chargingInfo[_gunIndex]->IsAvailable = YES;
  3280. _chademoIndex++;
  3281. _gunIndex++;
  3282. }
  3283. else
  3284. result = false;
  3285. }
  3286. break;
  3287. case 'U': // CCS1 combo
  3288. case 'E': // CCS2 combo
  3289. {
  3290. if (CCS_QUANTITY > _ccsIndex)
  3291. {
  3292. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  3293. chargingInfo[_gunIndex]->Index = _gunIndex;
  3294. chargingInfo[_gunIndex]->ReservationId = -1;
  3295. chargingInfo[_gunIndex]->slotsIndex = slots;
  3296. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3297. chargingInfo[_gunIndex]->Type = _Type_CCS_2;
  3298. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  3299. chargingInfo[_gunIndex]->IsAvailable = YES;
  3300. // 現階段預設為走 DIN70121
  3301. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  3302. _ccsIndex++;
  3303. _gunIndex++;
  3304. }
  3305. else
  3306. result = false;
  3307. }
  3308. break;
  3309. case 'G': // GBT DC
  3310. {
  3311. if (GB_QUANTITY > _gb_Index)
  3312. {
  3313. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  3314. chargingInfo[_gunIndex]->Index = _gunIndex;
  3315. chargingInfo[_gunIndex]->ReservationId = -1;
  3316. chargingInfo[_gunIndex]->slotsIndex = slots;
  3317. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3318. chargingInfo[_gunIndex]->Type = _Type_GB;
  3319. chargingInfo[_gunIndex]->type_index = _gb_Index;
  3320. chargingInfo[_gunIndex]->IsAvailable = YES;
  3321. _gb_Index++;
  3322. _gunIndex++;
  3323. }
  3324. else
  3325. result = false;
  3326. }
  3327. break;
  3328. case 'D': // GBT DC x 2
  3329. break;
  3330. }
  3331. return result;
  3332. }
  3333. */
  3334. bool CheckConnectorTypeStatus()
  3335. {
  3336. bool result = true;
  3337. // PRINTF_FUNC("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  3338. // bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
  3339. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  3340. {
  3341. // byte slots = 1;
  3342. // for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  3343. // {
  3344. // if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  3345. // {
  3346. // return false;
  3347. // }
  3348. // slots++;
  3349. // }
  3350. // AC index 接在 DC 後面
  3351. // if (AC_QUANTITY > 0)
  3352. // ac_chargingInfo[0]->Index += _gunIndex;
  3353. // DO360 for Audi, two gun only
  3354. _gunIndex = 2;
  3355. _acgunIndex = 0;
  3356. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3357. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  3358. PRINTF_FUNC("DC _gunCount = %d, AC _gunCount = %d \n",
  3359. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  3360. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  3361. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  3362. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  3363. result = false;
  3364. //DO360 charging info default setting
  3365. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3366. {
  3367. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  3368. chargingInfo[i]->Index = 0xFF;
  3369. chargingInfo[i]->Evboard_id = 0x00;
  3370. chargingInfo[i]->ReservationId = -1;
  3371. chargingInfo[i]->slotsIndex = 0;
  3372. chargingInfo[i]->SystemStatus = S_BOOTING;
  3373. chargingInfo[i]->Type = _Type_Unknown;
  3374. chargingInfo[i]->type_index = 0;
  3375. chargingInfo[i]->IsAvailable = YES;
  3376. }
  3377. /*
  3378. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3379. {
  3380. chargingInfo[0]->Evboard_id = 0x01;
  3381. PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", 0, chargingInfo[0]->Type, chargingInfo[0]->Evboard_id);
  3382. }
  3383. else
  3384. {
  3385. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  3386. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  3387. {
  3388. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  3389. {
  3390. // 與硬體相同 type : Chademo
  3391. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3392. {
  3393. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3394. }
  3395. }
  3396. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  3397. {
  3398. // 與硬體相同 type : CCS
  3399. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  3400. {
  3401. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3402. }
  3403. }
  3404. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  3405. {
  3406. // 與硬體相同 type : GB
  3407. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3408. {
  3409. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3410. }
  3411. }
  3412. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  3413. {
  3414. // 與硬體相同 type : Chademo
  3415. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3416. {
  3417. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3418. }
  3419. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3420. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3421. }
  3422. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  3423. {
  3424. // 與硬體相同 type : CCS
  3425. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  3426. {
  3427. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3428. }
  3429. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3430. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3431. }
  3432. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  3433. {
  3434. // 與硬體相同 type : GB
  3435. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3436. {
  3437. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3438. }
  3439. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3440. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3441. }
  3442. PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
  3443. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  3444. result = false;
  3445. }
  3446. }
  3447. */
  3448. }
  3449. else
  3450. {
  3451. // Module Name 不正確 - 告警
  3452. result = false;
  3453. }
  3454. return result;
  3455. }
  3456. void KillTask()
  3457. {
  3458. ChangeLcmByIndex(_LCM_FIX);
  3459. system("killall Module_EventLogging");
  3460. system("killall Module_PrimaryComm");
  3461. system("killall Module_EvComm");
  3462. system("killall Module_LcmControl");
  3463. system("killall Module_InternalComm");
  3464. system("killall Module_PsuComm");
  3465. system("killall OcppBackend &");
  3466. system("killall Module_4g &");
  3467. system("killall Module_Wifi &");
  3468. }
  3469. char CheckUpdateProcess()
  3470. {
  3471. DIR *d;
  3472. struct dirent *dir;
  3473. d = opendir("/mnt/");
  3474. if (d)
  3475. {
  3476. long int MaxLen=48*1024*1024, ImageLen = 0;
  3477. while ((dir = readdir(d)) != NULL)
  3478. {
  3479. char *new_str;
  3480. new_str = malloc(strlen("/mnt/")+strlen(dir->d_name)+1);
  3481. new_str[0] = '\0';
  3482. strcat(new_str, "/mnt/");
  3483. strcat(new_str, dir->d_name);
  3484. int fd = open(new_str, O_RDONLY);
  3485. if (fd < 0)
  3486. {
  3487. return FAIL;
  3488. }
  3489. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  3490. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  3491. //get the image length
  3492. ImageLen = read(fd, ptr, MaxLen);
  3493. if (ImageLen > 20)
  3494. {
  3495. unsigned int Type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
  3496. PRINTF_FUNC("Typed...%x \r\n", Type);
  3497. switch (Type)
  3498. {
  3499. case 0x10000001:
  3500. case 0x10000002:
  3501. case 0x10000003:
  3502. case 0x10000004:
  3503. case 0x10000005:
  3504. {
  3505. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3506. return PASS;
  3507. else
  3508. return FAIL;
  3509. }
  3510. break;
  3511. case 0x10000007:
  3512. case 0x10000008:
  3513. case 0x10000009:
  3514. case 0x1000000A:
  3515. {
  3516. bool isPass = true;
  3517. int CanFd = InitCanBus();
  3518. if (CanFd > 0)
  3519. {
  3520. for(byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3521. {
  3522. if (!isPass)
  3523. break;
  3524. if (chargingInfo[index]->Type == _Type_CCS_2)
  3525. {
  3526. if (Upgrade_CCS(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL)
  3527. {
  3528. isPass = false;
  3529. }
  3530. }
  3531. }
  3532. }
  3533. else
  3534. {
  3535. printf("Upgrade CCS open CAN FD fail.\n");
  3536. isPass = false;
  3537. }
  3538. return isPass;
  3539. }
  3540. break;
  3541. case 0x10000006:
  3542. case 0x1000000D:
  3543. case 0x1000000E:
  3544. case 0x20000002:
  3545. case 0x10000014:
  3546. {
  3547. // CSU_PRIMARY_CONTROLLER : 0x10000006
  3548. byte target = 0x00;
  3549. if (Type == 0x10000006)
  3550. target = UPGRADE_PRI;
  3551. else if (Type == 0x1000000D)
  3552. target = UPGRADE_RB;
  3553. else if (Type == 0x1000000E)
  3554. target = UPGRADE_FAN;
  3555. else if (Type == 0x20000002)
  3556. target = UPGRADE_AC;
  3557. else if (Type == 0x10000014)
  3558. target = UPGRADE_LED;
  3559. int fd = InitComPort(target);
  3560. if (Upgrade_UART(fd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3561. return PASS;
  3562. else
  3563. return FAIL;
  3564. close(fd);
  3565. }
  3566. break;
  3567. case 0x1000000B:
  3568. case 0x1000000C:
  3569. {
  3570. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  3571. bool isPass = true;
  3572. int CanFd = InitCanBus();
  3573. if (CanFd > 0)
  3574. {
  3575. for(byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3576. {
  3577. if (!isPass)
  3578. break;
  3579. if ((Type == 0x1000000B && chargingInfo[index]->Type == _Type_Chademo) ||
  3580. (Type == 0x1000000C && chargingInfo[index]->Type == _Type_GB))
  3581. {
  3582. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3583. return PASS;
  3584. else
  3585. return FAIL;
  3586. }
  3587. }
  3588. }
  3589. else
  3590. {
  3591. PRINTF_FUNC("Upgrad FD fail. \n");
  3592. isPass = false;
  3593. }
  3594. return isPass;
  3595. break;
  3596. }
  3597. }
  3598. }
  3599. free(new_str);
  3600. free(ptr);
  3601. }
  3602. }
  3603. free(dir);
  3604. closedir(d);
  3605. return FAIL;
  3606. }
  3607. /*
  3608. void CreateRfidFork()
  3609. {
  3610. pid_t rfidRecPid;
  3611. rfidRecPid = fork();
  3612. if (rfidRecPid == 0)
  3613. {
  3614. while(true)
  3615. {
  3616. // 刷卡判斷
  3617. RFID rfid;
  3618. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  3619. !ShmSysConfigAndInfo->SysConfig.isRFID)
  3620. {}
  3621. else if(getRequestCardSN(rfidFd, 0, &rfid))
  3622. {
  3623. PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3624. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  3625. {
  3626. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  3627. {
  3628. switch (rfid.snType)
  3629. {
  3630. case RFID_SN_TYPE_6BYTE:
  3631. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3632. "%02X%02X%02X%02X%02X%02X",
  3633. rfid.currentCard[0], rfid.currentCard[1],
  3634. rfid.currentCard[2], rfid.currentCard[3],
  3635. rfid.currentCard[4], rfid.currentCard[5]);
  3636. break;
  3637. case RFID_SN_TYPE_7BYTE:
  3638. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3639. "%02X%02X%02X%02X%02X%02X%02X",
  3640. rfid.currentCard[0], rfid.currentCard[1],
  3641. rfid.currentCard[2], rfid.currentCard[3],
  3642. rfid.currentCard[4], rfid.currentCard[5],
  3643. rfid.currentCard[6]);
  3644. break;
  3645. case RFID_SN_TYPE_10BYTE:
  3646. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3647. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3648. rfid.currentCard[0], rfid.currentCard[1],
  3649. rfid.currentCard[2], rfid.currentCard[3],
  3650. rfid.currentCard[4], rfid.currentCard[5],
  3651. rfid.currentCard[6], rfid.currentCard[7],
  3652. rfid.currentCard[8], rfid.currentCard[9]);
  3653. break;
  3654. case RFID_SN_TYPE_4BYTE:
  3655. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3656. "%02X%02X%02X%02X",
  3657. rfid.currentCard[0], rfid.currentCard[1],
  3658. rfid.currentCard[2], rfid.currentCard[3]);
  3659. break;
  3660. }
  3661. }
  3662. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  3663. {
  3664. switch (rfid.snType)
  3665. {
  3666. case RFID_SN_TYPE_6BYTE:
  3667. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3668. "%02X%02X%02X%02X%02X%02X",
  3669. rfid.currentCard[5], rfid.currentCard[4],
  3670. rfid.currentCard[3], rfid.currentCard[2],
  3671. rfid.currentCard[1], rfid.currentCard[0]);
  3672. break;
  3673. case RFID_SN_TYPE_7BYTE:
  3674. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3675. "%02X%02X%02X%02X%02X%02X%02X",
  3676. rfid.currentCard[6], rfid.currentCard[5],
  3677. rfid.currentCard[4], rfid.currentCard[3],
  3678. rfid.currentCard[2], rfid.currentCard[1],
  3679. rfid.currentCard[0]);
  3680. break;
  3681. case RFID_SN_TYPE_10BYTE:
  3682. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3683. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3684. rfid.currentCard[9], rfid.currentCard[8],
  3685. rfid.currentCard[7], rfid.currentCard[6],
  3686. rfid.currentCard[5], rfid.currentCard[4],
  3687. rfid.currentCard[3], rfid.currentCard[2],
  3688. rfid.currentCard[1], rfid.currentCard[0]);
  3689. break;
  3690. case RFID_SN_TYPE_4BYTE:
  3691. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3692. "%02X%02X%02X%02X",
  3693. rfid.currentCard[3], rfid.currentCard[2],
  3694. rfid.currentCard[1], rfid.currentCard[0]);
  3695. break;
  3696. }
  3697. }
  3698. PRINTF_FUNC("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3699. }
  3700. }
  3701. sleep(1);
  3702. }
  3703. }
  3704. }
  3705. */
  3706. void StartSystemTimeoutDet(unsigned char flag)
  3707. {
  3708. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  3709. {
  3710. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3711. }
  3712. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  3713. }
  3714. void StopSystemTimeoutDet()
  3715. {
  3716. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3717. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  3718. }
  3719. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  3720. {
  3721. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3722. {
  3723. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  3724. {
  3725. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  3726. }
  3727. chargingInfo[gunIndex]->TimeoutFlag = flag;
  3728. }
  3729. }
  3730. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  3731. {
  3732. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3733. {
  3734. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  3735. }
  3736. }
  3737. void TriggerDispenserConnectionTimeoutSetting(void)
  3738. {
  3739. BOOL trigger = FALSE;
  3740. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3741. {
  3742. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  3743. {
  3744. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.ConnectorTimeoutConfigRequest = 1;
  3745. trigger = TRUE;
  3746. }
  3747. }
  3748. if(trigger)
  3749. {
  3750. PRINTF_FUNC("********** Trigger Dispenser Connector Timeout Value: %d **********\n", ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout);
  3751. }
  3752. }
  3753. void CheckConnectionTimeout(void)
  3754. {
  3755. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3756. {
  3757. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3758. }
  3759. else
  3760. {
  3761. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  3762. {
  3763. _connectionTimeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3764. if(_connectionTimeout <= 0)
  3765. {
  3766. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3767. }
  3768. }
  3769. else
  3770. {
  3771. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3772. }
  3773. }
  3774. if(_connectionTimeout != ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout)
  3775. {
  3776. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout = _connectionTimeout;
  3777. TriggerDispenserConnectionTimeoutSetting();
  3778. }
  3779. }
  3780. void CreateTimeoutFork()
  3781. {
  3782. pid_t timeoutPid;
  3783. timeoutPid = fork();
  3784. if (timeoutPid > 0)
  3785. {
  3786. gettimeofday(&_cmdSubPriority_time, NULL);
  3787. CheckConnectionTimeout();
  3788. while(true)
  3789. {
  3790. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  3791. {
  3792. CheckConnectionTimeout();
  3793. gettimeofday(&_cmdSubPriority_time, NULL);
  3794. }
  3795. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  3796. // 系統
  3797. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  3798. {
  3799. // case Timeout_SelftestChk:
  3800. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT)
  3801. // {
  3802. // _SelfTestTimeout();
  3803. // StopSystemTimeoutDet();
  3804. // }
  3805. // break;
  3806. // case Timeout_Authorizing:
  3807. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT)
  3808. // {
  3809. // _AuthorizedTimeout();
  3810. // StopSystemTimeoutDet();
  3811. // }
  3812. // break;
  3813. // case Timeout_VerifyFail:
  3814. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT)
  3815. // {
  3816. // _AutoReturnTimeout();
  3817. // StopSystemTimeoutDet();
  3818. // }
  3819. // break;
  3820. // case Timeout_VerifyComp:
  3821. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_COMP_TIMEOUT)
  3822. // {
  3823. // _AutoReturnTimeout();
  3824. // StopSystemTimeoutDet();
  3825. // }
  3826. // break;
  3827. case Timeout_WaitPlug:
  3828. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout)
  3829. {
  3830. _DetectPlugInTimeout();
  3831. StopSystemTimeoutDet();
  3832. }
  3833. break;
  3834. case Timeout_ReturnToChargingGunDet:
  3835. {
  3836. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE)
  3837. {
  3838. DisplayChargingInfo();
  3839. StopSystemTimeoutDet();
  3840. }
  3841. }
  3842. break;
  3843. case Timeout_AuthorizingForStop:
  3844. {
  3845. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT)
  3846. {
  3847. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3848. ClearAuthorizedFlag();
  3849. StopSystemTimeoutDet();
  3850. }
  3851. }
  3852. break;
  3853. }
  3854. // 各槍
  3855. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3856. {
  3857. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  3858. switch(chargingInfo[gun_index]->TimeoutFlag)
  3859. {
  3860. case Timeout_Preparing:
  3861. {
  3862. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PREPARE_TIMEOUT)
  3863. {
  3864. _PrepareTimeout(gun_index);
  3865. StopGunInfoTimeoutDet(gun_index);
  3866. }
  3867. }
  3868. break;
  3869. case Timeout_EvChargingDet:
  3870. {
  3871. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT)
  3872. {
  3873. _DetectEvChargingEnableTimeout(gun_index);
  3874. StopGunInfoTimeoutDet(gun_index);
  3875. }
  3876. }
  3877. break;
  3878. case Timeout_EvseChargingDet:
  3879. {
  3880. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT)
  3881. {
  3882. _DetectEvseChargingEnableTimeout(gun_index);
  3883. StopGunInfoTimeoutDet(gun_index);
  3884. }
  3885. }
  3886. break;
  3887. case Timeout_EvseCompleteDet:
  3888. {
  3889. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT)
  3890. {
  3891. StopGunInfoTimeoutDet(gun_index);
  3892. }
  3893. }
  3894. break;
  3895. case Timeout_ForCcsPrechargeDet:
  3896. {
  3897. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT)
  3898. {
  3899. _CcsPrechargeTimeout(gun_index);
  3900. StopGunInfoTimeoutDet(gun_index);
  3901. }
  3902. }
  3903. break;
  3904. }
  3905. }
  3906. sleep(1);
  3907. }
  3908. }
  3909. }
  3910. void GetSystemTime()
  3911. {
  3912. struct timeb csuTime;
  3913. struct tm *tmCSU;
  3914. ftime(&csuTime);
  3915. tmCSU = localtime(&csuTime.time);
  3916. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  3917. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3918. tmCSU->tm_sec);
  3919. // byte date[14];
  3920. //
  3921. //
  3922. // //sprintf(&date, "%d", );
  3923. //
  3924. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  3925. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  3926. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  3927. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  3928. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  3929. //
  3930. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  3931. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  3932. //
  3933. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  3934. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  3935. //
  3936. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  3937. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  3938. //
  3939. // date[10] = (tmCSU->tm_min) / 10 % 10;
  3940. // date[11] = (tmCSU->tm_min) / 1 % 10;
  3941. //
  3942. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  3943. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  3944. // 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],
  3945. // date[4], date[5], date[6], date[7],
  3946. // date[8], date[9], date[10], date[11],
  3947. // date[12], date[13]);
  3948. }
  3949. void CheckFactoryConfigFunction()
  3950. {
  3951. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3952. {
  3953. system("cd /root;./FactoryConfig -m");
  3954. system("sync");
  3955. sleep(5);
  3956. system("reboot -f");
  3957. sleep(5);
  3958. system("reboot -f");
  3959. }
  3960. }
  3961. void CheckFwUpdateFunction()
  3962. {
  3963. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  3964. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  3965. {
  3966. DEBUG_INFO_MSG("ftp : update start. \n");
  3967. KillTask();
  3968. if (CheckUpdateProcess() == PASS)
  3969. DEBUG_INFO_MSG("ftp : update complete. \n");
  3970. else
  3971. DEBUG_INFO_MSG("ftp : update fail. \n");
  3972. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  3973. sleep(5);
  3974. system("reboot -f");
  3975. }
  3976. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  3977. {
  3978. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  3979. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  3980. {
  3981. DEBUG_INFO_MSG("Backend : update start. \n");
  3982. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3983. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3984. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3985. KillTask();
  3986. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  3987. {
  3988. setChargerMode(_index, MODE_UPDATE);
  3989. }
  3990. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  3991. {
  3992. ac_chargingInfo[_index]->SystemStatus = MODE_UPDATE;
  3993. }
  3994. if (CheckUpdateProcess() == PASS)
  3995. {
  3996. DEBUG_INFO_MSG("Backend : update complete. \n");
  3997. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3998. }
  3999. else
  4000. {
  4001. DEBUG_INFO_MSG("Backend : update fail. \n");
  4002. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  4003. }
  4004. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  4005. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  4006. sleep(5);
  4007. system("reboot -f");
  4008. }
  4009. }
  4010. }
  4011. //===============================================
  4012. // Check reservation date is expired
  4013. //===============================================
  4014. int isReservationExpired(unsigned char gun_index)
  4015. {
  4016. int result = NO;
  4017. struct tm expiredDate;
  4018. struct timeb expiredTime;
  4019. if (sscanf((char*) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
  4020. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  4021. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  4022. &expiredDate.tm_min, &expiredDate.tm_sec) == 6)
  4023. {
  4024. expiredDate.tm_year -= 1900;
  4025. expiredDate.tm_mon -= 1;
  4026. expiredTime.time = mktime(&expiredDate);
  4027. if (!CheckTimeOut(expiredTime))
  4028. {
  4029. result = YES;
  4030. }
  4031. }
  4032. return result;
  4033. }
  4034. struct timeval _Reboot_time;
  4035. void TriggerDispenserHardwareReboot(void)
  4036. {
  4037. BOOL trigger = FALSE;
  4038. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4039. {
  4040. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4041. {
  4042. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm &&
  4043. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest == 0)
  4044. {
  4045. trigger = TRUE;
  4046. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest = 1;
  4047. }
  4048. }
  4049. }
  4050. if(trigger)
  4051. {
  4052. gettimeofday(&_Reboot_time, NULL);
  4053. PRINTF_FUNC("********** Trigger Dispenser Hardware Reboot **********");
  4054. }
  4055. }
  4056. BOOL IsDispenserHardwareRebootResponsed(void)
  4057. {
  4058. BOOL response_ok = TRUE;
  4059. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4060. {
  4061. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4062. {
  4063. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  4064. {
  4065. response_ok = FALSE;
  4066. // 10s for reboot request timeout
  4067. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  4068. {
  4069. response_ok = TRUE;
  4070. PRINTF_FUNC("Reboot request timeout");
  4071. }
  4072. }
  4073. }
  4074. }
  4075. return response_ok;
  4076. }
  4077. struct timeval _Reset_time;
  4078. void TriggerDispenserSoftwareReset(void)
  4079. {
  4080. BOOL trigger = FALSE;
  4081. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4082. {
  4083. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4084. {
  4085. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  4086. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest == 0)
  4087. {
  4088. trigger = TRUE;
  4089. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest = 1;
  4090. }
  4091. }
  4092. }
  4093. if(trigger)
  4094. {
  4095. gettimeofday(&_Reset_time, NULL);
  4096. PRINTF_FUNC("********** Trigger Dispenser Software Reset **********");
  4097. }
  4098. }
  4099. BOOL IsDispenserSoftwareResetResponsed(void)
  4100. {
  4101. BOOL response_ok = TRUE;
  4102. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4103. {
  4104. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4105. {
  4106. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  4107. {
  4108. response_ok = FALSE;
  4109. // 10s for reset request timeout
  4110. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  4111. {
  4112. response_ok = TRUE;
  4113. PRINTF_FUNC("Reset request timeout");
  4114. }
  4115. }
  4116. }
  4117. }
  4118. return response_ok;
  4119. }
  4120. // the unit of return value: 0.01 dollar
  4121. int TariffParsing(char *StringItem, char *TariffCode)
  4122. {
  4123. char *ptrSave, *ptrToken;
  4124. char strSource[128];
  4125. int fee = 0;
  4126. memcpy(strSource, StringItem, 128);
  4127. ptrToken = strtok_r(strSource, ";", &ptrSave);
  4128. while(ptrToken != NULL)
  4129. {
  4130. char *strTariff = strstr(ptrToken, TariffCode);
  4131. if(strTariff != NULL)
  4132. {
  4133. char *strFee = strstr(strTariff, "$");
  4134. if(strFee != NULL)
  4135. {
  4136. strFee++;
  4137. float fFee = atof(strFee);
  4138. fee = (int)(fFee * 100);
  4139. break;
  4140. }
  4141. }
  4142. ptrToken = strtok_r(NULL, ";", &ptrSave);
  4143. }
  4144. return fee;
  4145. }
  4146. void DefaultPriceHandler(char *StrDefaultPrice)
  4147. {
  4148. BOOL trigger = FALSE;
  4149. unsigned int ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  4150. PRINTF_FUNC("********** Default Price: %s", StrDefaultPrice);
  4151. ConnectionFee = TariffParsing(StrDefaultPrice, "Connection Fee");
  4152. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4153. CurrentRate = TariffParsing(StrDefaultPrice, "Current Rate");
  4154. PRINTF_FUNC("********** Current Rate: %d.%02d **********", (CurrentRate / 100), (CurrentRate % 100));
  4155. OccupancyFee = TariffParsing(StrDefaultPrice, "Occupancy Fee");
  4156. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4157. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice = CurrentRate;
  4158. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4159. {
  4160. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  4161. {
  4162. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest == 0)
  4163. {
  4164. trigger = TRUE;
  4165. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest = 1;
  4166. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.CurrencyConfigRequest = 1;
  4167. }
  4168. }
  4169. }
  4170. if(trigger)
  4171. {
  4172. PRINTF_FUNC("********** Trigger Dispenser Default Price **********");
  4173. }
  4174. }
  4175. void UserPriceHandler(char *UserId, char *UserPrice)
  4176. {
  4177. unsigned int ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  4178. int AccountBalance = 0;
  4179. ConnectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Connection Fee");
  4180. CurrentRate = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Current Rate");
  4181. OccupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Occupancy Fee");
  4182. AccountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.SetUserPrice.price, "Account Balance");
  4183. PRINTF_FUNC("********** User Id: %s **********", ShmOCPP16Data->Cost.SetUserPrice.idToken);
  4184. PRINTF_FUNC("********** User Price: %s", ShmOCPP16Data->Cost.SetUserPrice.price);
  4185. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4186. PRINTF_FUNC("********** Current Rate: %d.%02d **********", (CurrentRate / 100), (CurrentRate % 100));
  4187. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4188. PRINTF_FUNC("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4189. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4190. {
  4191. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  4192. {
  4193. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  4194. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4195. {
  4196. PRINTF_FUNC("********** Connector Id %d User Price %d.%02d, Account Balance %d.%02d **********",
  4197. (i + 1), (CurrentRate / 100), (CurrentRate % 100), (AccountBalance / 100), (AccountBalance % 100));
  4198. }
  4199. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  4200. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4201. int DispenserIndex = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex;
  4202. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[DispenserIndex].AuthorizeStatus == _AuthorizeStatus_Pass &&
  4203. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.AuthorizeRequestType == _AuthorizeSrc_Local)
  4204. {
  4205. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.AccountBalanceRequest = true;
  4206. }
  4207. }
  4208. }
  4209. }
  4210. void RunningFinalCostHandler(void)
  4211. {
  4212. unsigned int ConnectionFee = 0, SessionFee = 0, OccupancyFee = 0, TotalCost = 0;
  4213. int AccountBalance = 0;
  4214. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4215. {
  4216. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  4217. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_COMPLETE)
  4218. {
  4219. if(strlen((char *)ShmOCPP16Data->Cost.RunningCost[i].description) > 0)
  4220. {
  4221. ConnectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Connection Fee");
  4222. SessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Session Fee");
  4223. OccupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Occupancy Fee");
  4224. TotalCost = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Total Cost");
  4225. AccountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.RunningCost[i].description, "Account Balance");
  4226. PRINTF_FUNC("********** Connector Id: %d Running Cost **********", (i + 1));
  4227. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4228. PRINTF_FUNC("********** Session Fee: %d.%02d **********", (SessionFee / 100), (SessionFee % 100));
  4229. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4230. PRINTF_FUNC("********** Total Cost: %d.%02d **********", (TotalCost / 100), (TotalCost % 100));
  4231. PRINTF_FUNC("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4232. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost != TotalCost ||
  4233. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4234. {
  4235. PRINTF_FUNC("********** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d **********",
  4236. (i + 1), (TotalCost / 100), (TotalCost % 100), (AccountBalance / 100), (AccountBalance % 100));
  4237. }
  4238. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost = TotalCost;
  4239. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4240. memset(&ShmOCPP16Data->Cost.RunningCost[i], 0x00, sizeof(struct StrcutRunningFinalCost));
  4241. }
  4242. if(strlen((char *)ShmOCPP16Data->Cost.FinalCost[i].description) > 0)
  4243. {
  4244. ConnectionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Connection Fee");
  4245. SessionFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Session Fee");
  4246. OccupancyFee = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Occupancy Fee");
  4247. TotalCost = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Total Cost");
  4248. AccountBalance = TariffParsing((char *)ShmOCPP16Data->Cost.FinalCost[i].description, "Account Balance");
  4249. PRINTF_FUNC("********** Connector Id: %d Final Cost **********", (i + 1));
  4250. PRINTF_FUNC("********** Connection Fee: %d.%02d **********", (ConnectionFee / 100), (ConnectionFee % 100));
  4251. PRINTF_FUNC("********** Session Fee: %d.%02d **********", (SessionFee / 100), (SessionFee % 100));
  4252. PRINTF_FUNC("********** Occupancy Fee: %d.%02d **********", (OccupancyFee / 100), (OccupancyFee % 100));
  4253. PRINTF_FUNC("********** Total Cost: %d.%02d **********", (TotalCost / 100), (TotalCost % 100));
  4254. PRINTF_FUNC("********** Account Balance: %d.%02d **********", (AccountBalance / 100), (AccountBalance % 100));
  4255. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost != TotalCost ||
  4256. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  4257. {
  4258. PRINTF_FUNC("********** Connector Id %d Total Cost %d.%02d, Account Balance %d.%02d **********",
  4259. (i + 1), (TotalCost / 100), (TotalCost % 100), (AccountBalance / 100), (AccountBalance % 100));
  4260. }
  4261. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost = TotalCost;
  4262. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  4263. memset(&ShmOCPP16Data->Cost.FinalCost[i], 0x00, sizeof(struct StrcutRunningFinalCost));
  4264. }
  4265. }
  4266. }
  4267. }
  4268. struct timeval _Cost_Price_time;
  4269. char MyDefaultPriceString[128];
  4270. struct StrcutSetUserPrice MyUserPrice;
  4271. char MyConnectorRunningCostString[CONNECTOR_QUANTITY][128];
  4272. char MyConnectorFinalCostString[CONNECTOR_QUANTITY][128];
  4273. void CheckOcppCostAndPrice(void)
  4274. {
  4275. // 5s for reboot request timeout
  4276. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  4277. {
  4278. gettimeofday(&_Cost_Price_time, NULL);
  4279. if(strcmp(MyDefaultPriceString, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData) != EQUAL)
  4280. {
  4281. DefaultPriceHandler((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  4282. memcpy(MyDefaultPriceString, ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, 128);
  4283. }
  4284. if(strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken) > 0 && strlen((char *)ShmOCPP16Data->Cost.SetUserPrice.price) > 0)
  4285. {
  4286. UserPriceHandler((char *)ShmOCPP16Data->Cost.SetUserPrice.idToken, (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  4287. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  4288. }
  4289. RunningFinalCostHandler();
  4290. }
  4291. }
  4292. //===============================================
  4293. // OCPP
  4294. //===============================================
  4295. void CheckOcppStatus()
  4296. {
  4297. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  4298. {
  4299. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  4300. PRINTF_FUNC("*********************OCPP Boot Notification****************************\n");
  4301. }
  4302. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES)
  4303. {
  4304. bool canReset = true;
  4305. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  4306. {
  4307. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4308. {
  4309. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  4310. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  4311. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  4312. {
  4313. canReset = false;
  4314. if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  4315. {
  4316. ChargingTerminalProcess(_index);
  4317. }
  4318. }
  4319. }
  4320. }
  4321. if(canReset)
  4322. {
  4323. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  4324. {
  4325. //trigger dispenser to hardware reboot
  4326. TriggerDispenserHardwareReboot();
  4327. if(IsDispenserHardwareRebootResponsed())
  4328. {
  4329. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  4330. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  4331. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  4332. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  4333. sleep(3);
  4334. system("reboot -f");
  4335. }
  4336. }
  4337. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  4338. {
  4339. //trigger dispenser to software reset
  4340. TriggerDispenserSoftwareReset();
  4341. if(IsDispenserSoftwareResetResponsed())
  4342. {
  4343. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  4344. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  4345. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  4346. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  4347. sleep(3);
  4348. system("killall OcppBackend &");
  4349. KillTask();
  4350. system("/usr/bin/run_evse_restart.sh");
  4351. }
  4352. }
  4353. }
  4354. }
  4355. CheckOcppCostAndPrice();
  4356. }
  4357. void OcppStartTransation(byte gunIndex)
  4358. {
  4359. byte _OcppGunIndex = gunIndex;
  4360. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  4361. PRINTF_FUNC("Gun index %d, OCPP Start Transation Index %d, IdTag = %s \n", chargingInfo[gunIndex]->Index, _OcppGunIndex, ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  4362. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  4363. }
  4364. void OcppStopTransation(byte gunIndex)
  4365. {
  4366. byte _OcppGunIndex = gunIndex;
  4367. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  4368. PRINTF_FUNC("Gun index %d, OCPP Stop Transation Index %d, IdTag = %s \n", chargingInfo[gunIndex]->Index, _OcppGunIndex, ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  4369. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  4370. }
  4371. // no use
  4372. bool OcppRemoteStop(byte gunIndex)
  4373. {
  4374. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4375. // 有 AC 槍的話
  4376. if (acDirIndex > 0 && gunIndex > 0)
  4377. {
  4378. gunIndex += acDirIndex;
  4379. }
  4380. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  4381. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  4382. {
  4383. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  4384. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  4385. }
  4386. return result;
  4387. }
  4388. void OcppRemoteStartChk()
  4389. {
  4390. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  4391. {}
  4392. else if(!isDetectPlugin())
  4393. {
  4394. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  4395. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4396. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++)
  4397. {
  4398. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES)
  4399. {
  4400. if (ac_chargingInfo[ac_index]->SystemStatus == S_IDLE ||
  4401. ac_chargingInfo[ac_index]->SystemStatus == S_RESERVATION)
  4402. {
  4403. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  4404. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  4405. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  4406. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  4407. ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  4408. DetectPluginStart();
  4409. return;
  4410. }
  4411. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  4412. }
  4413. }
  4414. byte threeGunIndex = 0;
  4415. byte dcIndex = 0;
  4416. bool isGunUsingStatus = false;
  4417. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4418. {
  4419. // 如果有 AC 槍,且 DC 槍也有兩把
  4420. if (acDirIndex == 1 && _index == 1)
  4421. threeGunIndex = 1;
  4422. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES)
  4423. dcIndex = _index;
  4424. if (chargingInfo[_index]->SystemStatus != S_IDLE)
  4425. {
  4426. isGunUsingStatus = true;
  4427. }
  4428. }
  4429. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  4430. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  4431. {
  4432. if (dcIndex == 0)
  4433. threeGunIndex = 0;
  4434. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4435. return;
  4436. }
  4437. if (dcIndex == 0)
  4438. threeGunIndex = 0;
  4439. if (ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq == YES)
  4440. {
  4441. if (chargingInfo[dcIndex]->SystemStatus == S_IDLE ||
  4442. chargingInfo[dcIndex]->SystemStatus == S_RESERVATION)
  4443. {
  4444. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  4445. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  4446. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  4447. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4448. DetectPluginStart();
  4449. }
  4450. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4451. }
  4452. }
  4453. }
  4454. void ChkOcppStatus(byte gunIndex)
  4455. {
  4456. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  4457. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES)
  4458. {
  4459. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  4460. if (isReservationExpired(gunIndex))
  4461. {
  4462. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4463. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4464. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  4465. chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
  4466. }
  4467. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  4468. }
  4469. if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
  4470. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES)
  4471. {
  4472. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  4473. if (isReservationExpired(gunIndex))
  4474. {
  4475. PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  4476. DEBUG_ERROR_MSG("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  4477. chargingInfo[gunIndex]->ReservationId = 0;
  4478. chargingInfo[gunIndex]->SystemStatus = S_IDLE;
  4479. }
  4480. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  4481. }
  4482. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES)
  4483. {
  4484. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4485. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4486. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  4487. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL)
  4488. {
  4489. if (isDb_ready)
  4490. DB_Update_Operactive(localDb, gunIndex, true);
  4491. chargingInfo[gunIndex]->IsAvailable = YES;
  4492. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.AvailabilityRequest = 1;
  4493. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  4494. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  4495. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  4496. {
  4497. setChargerMode(gunIndex, MODE_IDLE);
  4498. }
  4499. }
  4500. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL)
  4501. {
  4502. if (isDb_ready)
  4503. DB_Update_Operactive(localDb, gunIndex, false);
  4504. chargingInfo[gunIndex]->IsAvailable = NO;
  4505. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.AvailabilityRequest = 1;
  4506. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  4507. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  4508. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  4509. {
  4510. setChargerMode(gunIndex, MODE_MAINTAIN);
  4511. }
  4512. }
  4513. }
  4514. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES)
  4515. {
  4516. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  4517. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  4518. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  4519. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  4520. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  4521. {
  4522. // 充電中,需停止充電
  4523. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  4524. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.UnlockStopRequest = true;
  4525. PRINTF_FUNC("********** OcppUnlockStop Gun %d **********\n", gunIndex);
  4526. }
  4527. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  4528. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  4529. }
  4530. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  4531. {
  4532. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  4533. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  4534. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  4535. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  4536. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  4537. {
  4538. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  4539. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.RemoteStopRequest = true;
  4540. PRINTF_FUNC("********** OcppRemoteStop Gun %d **********\n", gunIndex);
  4541. }
  4542. }
  4543. }
  4544. bool CheckBackendChargingTimeout(byte gunIndex)
  4545. {
  4546. bool result = false;
  4547. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  4548. {
  4549. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  4550. {
  4551. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  4552. result = true;
  4553. }
  4554. }
  4555. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  4556. {
  4557. // 隨插即充電的要看 offline
  4558. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  4559. {
  4560. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  4561. result = true;
  4562. }
  4563. }
  4564. return result;
  4565. }
  4566. bool CheckBackendChargingEnergy(byte gunIndex)
  4567. {
  4568. bool result = false;
  4569. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  4570. {
  4571. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  4572. {
  4573. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  4574. result = true;
  4575. }
  4576. }
  4577. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  4578. {
  4579. // 隨插即充電的要看 offline
  4580. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  4581. {
  4582. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  4583. result = true;
  4584. }
  4585. }
  4586. return result;
  4587. }
  4588. void InformOcppErrOccur(byte codeType)
  4589. {
  4590. char _error[25];
  4591. switch(codeType)
  4592. {
  4593. case 4: strcpy(_error, "InternalError"); break;
  4594. case 6: strcpy(_error, "NoError"); break;
  4595. case 7: strcpy(_error, "OtherError"); break;
  4596. case 13: strcpy(_error, "UnderVoltage"); break;
  4597. case 14: strcpy(_error, "OverVoltage"); break;
  4598. }
  4599. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4600. {
  4601. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, _error);
  4602. }
  4603. }
  4604. //===============================================
  4605. // SQLite3 related routine
  4606. //===============================================
  4607. int DB_Open(sqlite3 *db)
  4608. {
  4609. int result = PASS;
  4610. char* errMsg = NULL;
  4611. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  4612. "idx integer primary key AUTOINCREMENT, "
  4613. "reservationId text, "
  4614. "transactionId text, "
  4615. "startMethod text, "
  4616. "userId text, "
  4617. "dateTimeStart text, "
  4618. "dateTimeStop text,"
  4619. "socStart text, "
  4620. "socStop text, "
  4621. "chargeEnergy text, "
  4622. "stopReason text"
  4623. ");";
  4624. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  4625. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  4626. "`IsAvailable` TEXT NOT NULL, "
  4627. "`connector` INTEGER NOT NULL, "
  4628. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  4629. if(sqlite3_open(DB_FILE, &db))
  4630. {
  4631. result = FAIL;
  4632. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4633. sqlite3_close(db);
  4634. }
  4635. else
  4636. {
  4637. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  4638. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  4639. {
  4640. result = FAIL;
  4641. PRINTF_FUNC( "Create local charging record table error message: %s\n", errMsg);
  4642. }
  4643. else
  4644. {
  4645. PRINTF_FUNC( "Opened local charging record table successfully\n");
  4646. }
  4647. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  4648. {
  4649. result = FAIL;
  4650. PRINTF_FUNC( "Create local config table error message: %s\n", errMsg);
  4651. }
  4652. else
  4653. {
  4654. PRINTF_FUNC( "Opened local config table successfully\n");
  4655. }
  4656. sqlite3_close(db);
  4657. }
  4658. return result;
  4659. }
  4660. int DB_Insert_Record(sqlite3 *db, int gun_index)
  4661. {
  4662. int result = PASS;
  4663. char* errMsg = NULL;
  4664. char insertSql[1024];
  4665. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  4666. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  4667. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  4668. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  4669. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  4670. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  4671. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  4672. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  4673. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  4674. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  4675. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  4676. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4677. if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
  4678. {
  4679. result = FAIL;
  4680. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4681. sqlite3_close(db);
  4682. }
  4683. else
  4684. {
  4685. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  4686. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  4687. {
  4688. result = FAIL;
  4689. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  4690. }
  4691. else
  4692. {
  4693. PRINTF_FUNC( "Insert local charging record successfully\n");
  4694. }
  4695. sqlite3_close(db);
  4696. }
  4697. return result;
  4698. }
  4699. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  4700. {
  4701. uint8_t result = false;
  4702. char* errMsg = NULL;
  4703. char sqlStr[1024];
  4704. srand(time(NULL));
  4705. if(sqlite3_open(DB_FILE, &db))
  4706. {
  4707. result = FAIL;
  4708. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4709. sqlite3_close(db);
  4710. }
  4711. else
  4712. {
  4713. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  4714. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  4715. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  4716. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  4717. {
  4718. result = FAIL;
  4719. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  4720. }
  4721. else
  4722. {
  4723. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  4724. }
  4725. sqlite3_close(db);
  4726. }
  4727. return result;
  4728. }
  4729. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  4730. {
  4731. uint8_t result = true;
  4732. char* errMsg = NULL;
  4733. char sqlStr[1024];
  4734. char **rs;
  4735. int rows, cols;
  4736. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  4737. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  4738. if(sqlite3_open(DB_FILE, &db))
  4739. {
  4740. result = FAIL;
  4741. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4742. sqlite3_close(db);
  4743. }
  4744. else
  4745. {
  4746. PRINTF_FUNC( "Local config query database open successfully.\r\n");
  4747. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  4748. if(rows>0)
  4749. {
  4750. for(int idxRow=1;idxRow<=rows;idxRow++)
  4751. {
  4752. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  4753. {
  4754. result = false;
  4755. }
  4756. PRINTF_FUNC("Query connector-%d isOperactive: %s\r\n", gun_index, rs[(idxRow*cols)+3]);
  4757. }
  4758. }
  4759. else
  4760. {
  4761. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.\r\n", gun_index);
  4762. }
  4763. sqlite3_free_table(rs);
  4764. sqlite3_close(db);
  4765. }
  4766. return result;
  4767. }
  4768. //===============================================
  4769. // Config process
  4770. //===============================================
  4771. void AddPlugInTimes(byte gunIndex)
  4772. {
  4773. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  4774. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  4775. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  4776. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  4777. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  4778. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  4779. }
  4780. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  4781. {
  4782. char cmdBuf[32];
  4783. struct timeb csuTime;
  4784. struct tm *tmCSU;
  4785. ftime(&csuTime);
  4786. tmCSU = localtime(&csuTime.time);
  4787. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  4788. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4789. tmCSU->tm_sec);
  4790. if (isStart)
  4791. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  4792. else
  4793. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  4794. }
  4795. void zipLogFiles()
  4796. {
  4797. const char* logPath = "/Storage/SystemLog";
  4798. // 獲取目錄
  4799. DIR* pDir = opendir(logPath);
  4800. if (pDir != NULL)
  4801. {
  4802. struct timeb csuTime;
  4803. struct tm *tmCSU;
  4804. ftime(&csuTime);
  4805. tmCSU = localtime(&csuTime.time);
  4806. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  4807. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4808. // tmCSU->tm_sec);
  4809. // Read items inside the folder
  4810. struct dirent* pEntry = NULL;
  4811. while ((pEntry = readdir(pDir)) != NULL)
  4812. {
  4813. if (strcmp(pEntry->d_name, ".") != 0 &&
  4814. strcmp(pEntry->d_name, "..") != 0 &&
  4815. strncmp(pEntry->d_name, "[", 1) == 0 &&
  4816. strstr(pEntry->d_name, "tar") < 0)
  4817. {
  4818. char yearC[5];
  4819. unsigned short year = 0;
  4820. char monthC[3];
  4821. unsigned short month = 0;
  4822. yearC[4] = '\0';
  4823. strncpy(yearC, pEntry->d_name + 1, 4);
  4824. monthC[2] = '\0';
  4825. strncpy(monthC, pEntry->d_name + 6, 2);
  4826. year = atoi(yearC);
  4827. month = atoi(monthC);
  4828. if (year != 0)
  4829. {
  4830. if (year < tmCSU->tm_year + 1900 ||
  4831. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  4832. {
  4833. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  4834. char file[256];
  4835. memset(file, 0x00, sizeof(file));
  4836. strcat(file, "tar zcvf ");
  4837. strcat(file, logPath);
  4838. strncat(file, "/", 1);
  4839. strcat(file, pEntry->d_name);
  4840. strcat(file, ".tar");
  4841. strncat(file, " ", 1);
  4842. strcat(file, logPath);
  4843. strncat(file, "/", 1);
  4844. strcat(file, pEntry->d_name);
  4845. PRINTF_FUNC("zip = %s \n", file);
  4846. system(file);
  4847. }
  4848. }
  4849. }
  4850. }
  4851. }
  4852. // Close folder
  4853. closedir(pDir);
  4854. }
  4855. void ChangeGunSelectByIndex(byte sel)
  4856. {
  4857. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  4858. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  4859. }
  4860. void CheckIsAlternatvieByModelName()
  4861. {
  4862. // 黑白機 ?
  4863. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWWU301J0UT1PH") == EQUAL ||
  4864. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYE301J0ET1PH") == EQUAL ||
  4865. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DSYE301J3EW2PH") == EQUAL ||
  4866. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UW1PH") == EQUAL ||
  4867. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UD1PH") == EQUAL)
  4868. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  4869. else
  4870. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  4871. }
  4872. void StopProcessingLoop()
  4873. {
  4874. for (;;)
  4875. {
  4876. CheckFactoryConfigFunction();
  4877. CheckFwUpdateFunction();
  4878. sleep(1);
  4879. }
  4880. }
  4881. void CreateWatchdog()
  4882. {
  4883. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  4884. {
  4885. wtdFd = InitWatchDog();
  4886. if (wtdFd < 0)
  4887. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  4888. }
  4889. }
  4890. bool IsConnectorWholeIdle()
  4891. {
  4892. bool result = true;
  4893. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  4894. {
  4895. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  4896. chargingInfo[count]->SystemStatus != S_RESERVATION)
  4897. {
  4898. result = false;
  4899. break;
  4900. }
  4901. }
  4902. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  4903. {
  4904. if (ac_chargingInfo[count]->SystemStatus != S_IDLE &&
  4905. ac_chargingInfo[count]->IsErrorOccur == NO)
  4906. {
  4907. result = false;
  4908. break;
  4909. }
  4910. }
  4911. return result;
  4912. }
  4913. void ClearAlarmCodeWhenAcOff()
  4914. {
  4915. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  4916. {
  4917. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  4918. }
  4919. }
  4920. //==========================================
  4921. // Check task processing
  4922. //==========================================
  4923. void CheckTask()
  4924. {
  4925. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  4926. {
  4927. if(system("pidof -s Module_4g > /dev/null") != 0)
  4928. {
  4929. DEBUG_ERROR_MSG("Module_4g not running, restart it.\r\n");
  4930. system("/root/Module_4g &");
  4931. }
  4932. }
  4933. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  4934. {
  4935. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  4936. {
  4937. DEBUG_ERROR_MSG("Module_Wifi not running, restart it.\r\n");
  4938. system("/root/Module_Wifi &");
  4939. }
  4940. }
  4941. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  4942. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  4943. {
  4944. if(system("pidof -s OcppBackend > /dev/null") != 0)
  4945. {
  4946. DEBUG_ERROR_MSG("OcppBackend not running, restart it.\r\n");
  4947. system("/root/OcppBackend &");
  4948. }
  4949. }
  4950. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  4951. {
  4952. DEBUG_ERROR_MSG("Module_ProduceUtils not running, restart it.\r\n");
  4953. system ("/root/Module_ProduceUtils &");
  4954. }
  4955. }
  4956. void InitialDHCP()
  4957. {
  4958. char tmpbuf[256];
  4959. memset(tmpbuf,0,256);
  4960. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  4961. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  4962. system(tmpbuf);
  4963. }
  4964. void InitialDispenserDhcpServerConfig(void)
  4965. {
  4966. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  4967. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  4968. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  4969. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  4970. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  4971. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  4972. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  4973. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  4974. usleep(1000);
  4975. }
  4976. void StartDispenserDhcpServer(void)
  4977. {
  4978. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  4979. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  4980. }
  4981. //==========================================
  4982. // Check Smart Charging Profile
  4983. //==========================================
  4984. int GetStartScheduleTime(unsigned char *time)
  4985. {
  4986. int result = -1;
  4987. struct tm tmScheduleStart;
  4988. struct timeb tbScheduleStart;
  4989. 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))
  4990. {
  4991. tmScheduleStart.tm_year -= 1900;
  4992. tmScheduleStart.tm_mon -= 1;
  4993. tbScheduleStart.time = mktime(&tmScheduleStart);
  4994. tbScheduleStart.millitm = 0;
  4995. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  4996. }
  4997. return result;
  4998. }
  4999. void CheckSmartChargeProfile(byte _index)
  5000. {
  5001. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO)
  5002. {
  5003. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO)
  5004. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  5005. }
  5006. else
  5007. {
  5008. // Get Charging Profile
  5009. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  5010. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL)
  5011. {
  5012. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  5013. byte _startCount = NO_DEFINE;
  5014. byte _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  5015. for (byte _count = 0; _count < _maxCount; _count++)
  5016. {
  5017. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  5018. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod &&
  5019. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  5020. {
  5021. _startCount = _count;
  5022. }
  5023. }
  5024. PRINTF_FUNC("_startCount = %d \n", _startCount);
  5025. if (_startCount < _maxCount)
  5026. {
  5027. PRINTF_FUNC("*********Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  5028. chargingInfo[_index]->ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * 10;
  5029. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * chargingInfo[_index]->EvBatterytargetVoltage / 100;
  5030. //chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  5031. // if ((chargingInfo[_index]->EvBatterytargetVoltage * 10) > 0)
  5032. // {
  5033. // chargingInfo[_index]->ChargingProfileCurrent = chargingInfo[_index]->ChargingProfilePower / (chargingInfo[_index]->EvBatterytargetVoltage * 10);
  5034. // }
  5035. }
  5036. else
  5037. {
  5038. chargingInfo[_index]->ChargingProfilePower = -1;
  5039. chargingInfo[_index]->ChargingProfileCurrent = -1;
  5040. }
  5041. PRINTF_FUNC("ChargingProfilePower = %f \n", chargingInfo[_index]->ChargingProfilePower);
  5042. PRINTF_FUNC("ChargingProfileCurrent = %f \n", chargingInfo[_index]->ChargingProfileCurrent);
  5043. }
  5044. //
  5045. // printf("-------------Schedule------------\n");
  5046. // printf("index = %d \n", _index);
  5047. // printf("StartSchedule = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  5048. // printf("ChargingRateUnit = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingRateUnit);
  5049. // printf("----------SchedulePeriod---------\n");
  5050. // for (int v = 0; v < 10; v++)
  5051. // {
  5052. // printf("StartPeriod = %d \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].StartPeriod);
  5053. // printf("Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].Limit);
  5054. // }
  5055. // printf("---------------------------------\n");
  5056. }
  5057. }
  5058. void CheckReturnToChargingConn()
  5059. {
  5060. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  5061. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  5062. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  5063. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  5064. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  5065. {
  5066. bool isReturnTimeout = false;
  5067. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  5068. {
  5069. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  5070. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  5071. {
  5072. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  5073. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  5074. {
  5075. isReturnTimeout = false;
  5076. break;
  5077. }
  5078. }
  5079. else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  5080. {
  5081. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  5082. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  5083. {
  5084. isReturnTimeout = true;
  5085. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  5086. }
  5087. }
  5088. }
  5089. // AC 槍
  5090. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  5091. {
  5092. // 沒有選中 AC,且 AC 在充電中
  5093. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  5094. (ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE))
  5095. {
  5096. // 當前 DC 充電槍在 idle 狀態
  5097. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_IDLE ||
  5098. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_RESERVATION)
  5099. {
  5100. isReturnTimeout = true;
  5101. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  5102. }
  5103. }
  5104. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  5105. ((chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= S_COMPLETE) ||
  5106. (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= S_CCS_PRECHARGE_ST1)))
  5107. {
  5108. // 當前 DC 充電槍在 idle 狀態
  5109. if (ac_chargingInfo[0]->SystemStatus == S_IDLE ||
  5110. ac_chargingInfo[0]->SystemStatus == S_RESERVATION)
  5111. {
  5112. isReturnTimeout = true;
  5113. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  5114. }
  5115. }
  5116. }
  5117. if (!isReturnTimeout)
  5118. StopSystemTimeoutDet();
  5119. }
  5120. }
  5121. bool GetStartChargingByAlterMode(byte _gun)
  5122. {
  5123. bool result = true;
  5124. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2 &&
  5125. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  5126. {
  5127. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select++)
  5128. {
  5129. if (_select != _gun)
  5130. {
  5131. if (chargingInfo[_select]->SystemStatus != S_IDLE &&
  5132. chargingInfo[_select]->SystemStatus != S_RESERVATION)
  5133. {
  5134. result = false;
  5135. break;
  5136. }
  5137. }
  5138. }
  5139. }
  5140. return result;
  5141. }
  5142. void CheckMiscCommandRequirement(void)
  5143. {
  5144. BOOL NeedAnnouncement = FALSE;
  5145. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5146. {
  5147. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.AvailabilityRequest ||
  5148. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.RemoteStartRequest ||
  5149. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.AccountBalanceRequest)
  5150. {
  5151. NeedAnnouncement = TRUE;
  5152. }
  5153. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.ConnectorTimeoutConfigRequest ||
  5154. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.DefaultPriceConfigRequest ||
  5155. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.CurrencyConfigRequest ||
  5156. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootRequest ||
  5157. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetRequest)
  5158. {
  5159. NeedAnnouncement = TRUE;
  5160. }
  5161. }
  5162. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  5163. }
  5164. void CheckDispenserVersionUpdateRequirement(void)
  5165. {
  5166. }
  5167. void DispenserConnectionRetryHandler(void)
  5168. {
  5169. unsigned short _timeout = 0;
  5170. unsigned int _price = 0;
  5171. unsigned char _currency = 0;
  5172. PRINTF_FUNC("********** Kill ModuleEvComm Task & Retry To Excute ModuleEvComm **********");
  5173. _timeout = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout;
  5174. _price = ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice;
  5175. _currency = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency;
  5176. system("killall Module_EvComm");
  5177. usleep(100000);
  5178. system("/root/Module_EvComm &");
  5179. usleep(100000);
  5180. ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectorTimeout = _timeout;
  5181. ShmSysConfigAndInfo->SysInfo.DispenserInfo.DefaultPrice = _price;
  5182. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Currency = _currency;
  5183. }
  5184. struct timeval _dispenserDisconnection_time, _dispenserConnectionRetry_time;
  5185. int main(void)
  5186. {
  5187. if(CreateShareMemory() == 0)
  5188. {
  5189. #ifdef SystemLogMessage
  5190. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  5191. #endif
  5192. if(ShmStatusCodeData!=NULL)
  5193. {
  5194. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  5195. }
  5196. sleep(5);
  5197. system("reboot -f");
  5198. sleep(5);
  5199. system("reboot -f");
  5200. }
  5201. if (!InitialSystemDefaultConfig())
  5202. {
  5203. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  5204. StopProcessingLoop();
  5205. }
  5206. PRINTF_FUNC("SW Version = %s \n", fwVersion);
  5207. PRINTF_FUNC("Chademo = %d, CCS = %d, GB = %d, AC = %d, General: %d\n",
  5208. CHAdeMO_QUANTITY, CCS_QUANTITY, GB_QUANTITY, AC_QUANTITY, GENERAL_GUN_QUANTITY);
  5209. PRINTF_FUNC("CheckConnectorTypeStatus. \n");
  5210. // CheckGunTypeFromHw();
  5211. PRINTF_FUNC("CheckIsAlternatvie, ModelName = %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  5212. // CheckIsAlternatvieByModelName();
  5213. PRINTF_FUNC("InitialShareMemoryInfo \n");
  5214. InitialShareMemoryInfo();
  5215. PRINTF_FUNC("ChargerType (IEC or UL) = %d \n", ShmSysConfigAndInfo->SysInfo.ChargerType);
  5216. ChangeLcmByIndex(_LCM_INIT);
  5217. if (!CheckConnectorTypeStatus())
  5218. isModelNameMatch = false;
  5219. Initialization();
  5220. PRINTF_FUNC("Spawn all Task. \n");
  5221. SpawnTask();
  5222. if (!isModelNameMatch)
  5223. {
  5224. PRINTF_FUNC("Module Name & HW info none match. \n");
  5225. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  5226. ChangeLcmByIndex(_LCM_FIX);
  5227. // Module Name 與硬體對應不正確
  5228. DEBUG_ERROR_MSG("Module Name & HW info none match. \n");
  5229. sleep(3);
  5230. KillTask();
  5231. StopProcessingLoop();
  5232. }
  5233. PRINTF_FUNC("Module Name & HW info correct. Initialize.......\n");
  5234. CreateTimeoutFork();
  5235. PRINTF_FUNC("Self test. \n");
  5236. ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv = true; // green led
  5237. ShmPrimaryMcuData->OutputDrv.bits.SystemLed2Drv = true; // red led
  5238. SelfTestRun();
  5239. // StopSystemTimeoutDet();
  5240. PRINTF_FUNC("SelfTestSeq = %d, Work_Step = %d \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq, ShmPsuData->Work_Step);
  5241. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  5242. ShmPsuData->Work_Step == _NO_WORKING)
  5243. {
  5244. /*
  5245. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  5246. {
  5247. if (!DisplaySelfTestFailReason())
  5248. {
  5249. PRINTF_FUNC("Soft reboot for retry self-tets. \n");
  5250. sleep(3);
  5251. system("killall OcppBackend &");
  5252. KillTask();
  5253. system("/usr/bin/run_evse_restart.sh");
  5254. }
  5255. }
  5256. */
  5257. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  5258. {
  5259. setChargerMode(gun_index, MODE_ALARM);
  5260. }
  5261. ChangeLcmByIndex(_LCM_FIX);
  5262. sleep(3);
  5263. KillTask();
  5264. StopProcessingLoop();
  5265. }
  5266. else
  5267. {
  5268. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  5269. {
  5270. setChargerMode(gun_index, MODE_IDLE);
  5271. }
  5272. }
  5273. // Local DB
  5274. if(DB_Open(localDb) != PASS)
  5275. {
  5276. PRINTF_FUNC("DB_Open fail. \n");
  5277. isDb_ready = false;
  5278. }
  5279. else
  5280. {
  5281. isDb_ready = true;
  5282. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  5283. {
  5284. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  5285. }
  5286. }
  5287. ChangeLcmByIndex(_LCM_IDLE);
  5288. sleep(1);
  5289. //***** 須新增的偵測 *****//
  5290. // 1. Thernal - 控制風扇轉速
  5291. // 2. ouput fuse - 控制風扇轉速
  5292. // CreateRfidFork();
  5293. // Create Watchdog
  5294. //CreateWatchdog();
  5295. ShmPrimaryMcuData->OutputDrv.bits.SystemLed2Drv = false;
  5296. // Main loop
  5297. PRINTF_FUNC("****************************Main Loop********************************** \n");
  5298. gettimeofday(&_cmdMainPriority_time, NULL);
  5299. for (;;)
  5300. {
  5301. CheckOcppStatus();
  5302. ChkPrimaryStatus();
  5303. if ((IsConnectorWholeIdle() || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) &&
  5304. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  5305. {
  5306. CheckFactoryConfigFunction();
  5307. CheckFwUpdateFunction();
  5308. }
  5309. // OCPP 邏輯
  5310. //OcppRemoteStartChk();
  5311. // 讀卡邏輯
  5312. //ScannerCardProcess();
  5313. PowerCabinetAuthorizeProcess();
  5314. // 當 AC 沒有搭上時,清除一些錯誤碼
  5315. ClearAlarmCodeWhenAcOff();
  5316. // 確認是否要回到充電中的槍畫面邏輯判斷
  5317. CheckReturnToChargingConn();
  5318. // if (_acgunIndex > 0 && isDetectPlugin() && !isCardScan)
  5319. // {
  5320. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  5321. // }
  5322. if ((GetTimeoutValue(_cmdMainPriority_time) / 1000) > 5000)
  5323. {
  5324. CheckTask();
  5325. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5326. {
  5327. if (chargingInfo[_index]->SystemStatus == S_CHARGING)
  5328. CheckSmartChargeProfile(_index);
  5329. }
  5330. gettimeofday(&_cmdMainPriority_time, NULL);
  5331. // PRINTF_FUNC("Gun 1(%d): %d(%s), Gun 2(%d): %d(%s), OCPP: %s",
  5332. // chargingInfo[0]->Index, chargingInfo[0]->SystemStatus, chargingInfo[0]->IsAvailable ? "Operative" : "Inoperative",
  5333. // chargingInfo[1]->Index, chargingInfo[1]->SystemStatus, chargingInfo[1]->IsAvailable ? "Operative" : "Inoperative",
  5334. // ShmOCPP16Data->OcppConnStatus ? "On" : "Off");
  5335. // PRINTF_FUNC("Gun 1: %d(%d), Gun 2: %d(%d), Gun 3: %d(%d), Gun 4: %d(%d)",
  5336. // chargingInfo[0]->Index, chargingInfo[0]->Type,
  5337. // chargingInfo[1]->Index, chargingInfo[1]->Type,
  5338. // chargingInfo[2]->Index, chargingInfo[2]->Type,
  5339. // chargingInfo[3]->Index, chargingInfo[3]->Type);
  5340. }
  5341. if(ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  5342. {
  5343. ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv = false;
  5344. ShmPrimaryMcuData->OutputDrv.bits.SystemLed2Drv = true;
  5345. }
  5346. else
  5347. {
  5348. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity == 0)
  5349. {
  5350. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.DispenserDisconnection == 0)
  5351. {
  5352. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.DispenserDisconnection = 1;
  5353. gettimeofday(&_dispenserConnectionRetry_time, NULL);
  5354. }
  5355. if((GetTimeoutValue(_dispenserConnectionRetry_time) / uSEC_VAL) >= DISPENSER_RECONNECTE_TIME)
  5356. {
  5357. DispenserConnectionRetryHandler();
  5358. gettimeofday(&_dispenserConnectionRetry_time, NULL);
  5359. }
  5360. if((GetTimeoutValue(_dispenserDisconnection_time) / 1000) >= 1000)
  5361. {
  5362. ShmPrimaryMcuData->OutputDrv.bits.SystemLed2Drv = ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv ? false : true;
  5363. ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv = ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv ? false : true;
  5364. gettimeofday(&_dispenserDisconnection_time, NULL);
  5365. }
  5366. }
  5367. else
  5368. {
  5369. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.DispenserDisconnection = 0;
  5370. ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv = true;
  5371. ShmPrimaryMcuData->OutputDrv.bits.SystemLed2Drv = false;
  5372. }
  5373. }
  5374. CheckMiscCommandRequirement();
  5375. CheckDispenserVersionUpdateRequirement();
  5376. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  5377. {
  5378. //CheckGpioInStatus();
  5379. CheckErrorOccurStatus(gun_index);
  5380. ChkOcppStatus(gun_index);
  5381. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.TimeoutStopRequest)
  5382. {
  5383. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.TimeoutStopRequest = false;
  5384. if(chargingInfo[gun_index]->SystemStatus == S_PREPARNING ||
  5385. chargingInfo[gun_index]->SystemStatus == S_PREPARING_FOR_EV ||
  5386. chargingInfo[gun_index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  5387. chargingInfo[gun_index]->SystemStatus == S_CHARGING)
  5388. {
  5389. ChargingTerminalProcess(gun_index);
  5390. }
  5391. }
  5392. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  5393. switch(chargingInfo[gun_index]->SystemStatus)
  5394. {
  5395. case S_IDLE:
  5396. case S_RESERVATION:
  5397. case S_MAINTAIN:
  5398. case S_ALARM:
  5399. {
  5400. // clean PermissionRequest flag
  5401. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  5402. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopRequest = false;
  5403. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  5404. if (chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  5405. isModeChange(gun_index))
  5406. {
  5407. PRINTF_FUNC("S_IDLE================================== %x \n", gun_index);
  5408. chargingInfo[gun_index]->PresentChargedDuration = 0;
  5409. chargingInfo[gun_index]->RemainChargingDuration = 0;
  5410. chargingTime[gun_index] = 0;
  5411. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  5412. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  5413. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  5414. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  5415. // clean connector infomation
  5416. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  5417. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  5418. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  5419. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  5420. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  5421. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  5422. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -6553500;
  5423. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  5424. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  5425. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  5426. }
  5427. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION &&
  5428. isModeChange(gun_index))
  5429. {
  5430. PRINTF_FUNC("S_RESERVATION....................%x \n", gun_index);
  5431. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
  5432. }
  5433. if (chargingInfo[gun_index]->IsAvailable == NO)
  5434. {
  5435. setChargerMode(gun_index, MODE_MAINTAIN);
  5436. }
  5437. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  5438. {
  5439. //if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  5440. {
  5441. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  5442. }
  5443. ClearDetectPluginFlag();
  5444. setChargerMode(gun_index, MODE_ALARM);
  5445. }
  5446. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  5447. {
  5448. // 不給充電
  5449. }
  5450. else
  5451. {
  5452. if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_FIX)
  5453. {
  5454. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5455. for (byte g_index = 0; g_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; g_index++)
  5456. setChargerMode(g_index, MODE_IDLE);
  5457. }
  5458. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  5459. {
  5460. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  5461. {
  5462. // 均充 -> 最大充
  5463. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  5464. {
  5465. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  5466. {
  5467. PRINTF_FUNC("=============Smart Charging============= Step 11 \n");
  5468. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  5469. }
  5470. }
  5471. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  5472. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  5473. {
  5474. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  5475. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  5476. }
  5477. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  5478. {
  5479. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5480. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5481. }
  5482. }
  5483. else
  5484. {
  5485. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5486. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5487. }
  5488. }
  5489. else
  5490. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5491. // clean stop charge flag
  5492. chargingInfo[gun_index]->StopChargeFlag = false;
  5493. { // Idle 正常程序起點
  5494. // 判斷是否有啟用檢查插槍
  5495. //if(isDetectPlugin())
  5496. if(IsConnectorAuthorizeSuccess(gun_index))
  5497. {
  5498. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  5499. chargingInfo[gun_index]->IsAvailable &&
  5500. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  5501. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  5502. {
  5503. AddPlugInTimes(gun_index);
  5504. ChangeGunSelectByIndex(gun_index);
  5505. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  5506. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5507. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AuthorizeRequestType == _AuthorizeSrc_Local)
  5508. {
  5509. AuthorizingSettingInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex, _AuthorizeSrc_Local);
  5510. }
  5511. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AuthorizeRequestType == _AuthorizeSrc_Remote)
  5512. {
  5513. AuthorizingSettingInitial(gun_index, _AuthorizeSrc_Remote);
  5514. }
  5515. continue;
  5516. }
  5517. else
  5518. {
  5519. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  5520. {
  5521. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  5522. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  5523. PRINTF_FUNC("Connector %d Stop Wait Plug In", gun_index + 1);
  5524. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AuthorizeRequestType == _AuthorizeSrc_Local)
  5525. {
  5526. AuthorizingSettingInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex, _AuthorizeSrc_Local);
  5527. }
  5528. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AuthorizeRequestType == _AuthorizeSrc_Remote)
  5529. {
  5530. AuthorizingSettingInitial(gun_index, _AuthorizeSrc_Remote);
  5531. }
  5532. }
  5533. }
  5534. /*
  5535. // 卡號驗證成功後,等待充電槍插入充電車
  5536. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  5537. {
  5538. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  5539. chargingInfo[gun_index]->IsAvailable)
  5540. {
  5541. PRINTF_FUNC("-----------------1----------------- %d \n", gun_index);
  5542. chargingInfo[gun_index]->RemoteStartFlag = NO;
  5543. chargingInfo[gun_index]->isRemoteStart = YES;
  5544. ChangeGunSelectByIndex(gun_index);
  5545. AddPlugInTimes(gun_index);
  5546. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5547. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  5548. ClearDetectPluginFlag();
  5549. continue;
  5550. }
  5551. }
  5552. else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE)
  5553. {
  5554. if (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable &&
  5555. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  5556. {
  5557. PRINTF_FUNC("-----------------2----------------- \n");
  5558. ChangeGunSelectByIndex(gun_index);
  5559. AddPlugInTimes(gun_index);
  5560. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  5561. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  5562. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  5563. // 當前操作的槍號,進入 Preparing
  5564. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5565. ClearDetectPluginFlag();
  5566. continue;
  5567. }
  5568. }
  5569. */
  5570. // if (!isCardScan)
  5571. // {
  5572. // LCM => Waiting for plugging
  5573. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  5574. // }
  5575. }
  5576. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
  5577. {
  5578. if (!isReservationExpired(gun_index))
  5579. {
  5580. chargingInfo[gun_index]->SystemStatus = S_IDLE;
  5581. }
  5582. }
  5583. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  5584. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  5585. {
  5586. PRINTF_FUNC("-----------------3----------------- \n");
  5587. bool isCanStartChargingFlag = GetStartChargingByAlterMode(gun_index);
  5588. if (isCanStartChargingFlag)
  5589. {
  5590. ChangeGunSelectByIndex(gun_index);
  5591. AddPlugInTimes(gun_index);
  5592. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5593. ClearDetectPluginFlag();
  5594. continue;
  5595. }
  5596. }
  5597. else
  5598. {
  5599. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5600. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  5601. }
  5602. } // Idle 正常程序終點
  5603. }
  5604. ReleaseAlarmCode(gun_index);
  5605. }
  5606. break;
  5607. case S_REASSIGN_CHECK:
  5608. {
  5609. if (isModeChange(gun_index))
  5610. {
  5611. PRINTF_FUNC("S_REASSIGN_CHECK================================== %x \n", gun_index);
  5612. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5613. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  5614. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  5615. StopSystemTimeoutDet();
  5616. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  5617. }
  5618. bool isRessign = false;
  5619. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  5620. {
  5621. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  5622. {
  5623. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  5624. {
  5625. // 有其他槍已經分配好 psu 模塊
  5626. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  5627. chargingInfo[index]->SystemStatus >= S_PREPARNING &&
  5628. chargingInfo[index]->SystemStatus != S_MAINTAIN)
  5629. {
  5630. PRINTF_FUNC("=============Smart Charging============= Step 1 \n");
  5631. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  5632. isRessign = true;
  5633. break;
  5634. }
  5635. }
  5636. }
  5637. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  5638. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
  5639. {
  5640. // 如果在切換最大充的過程中,需等待最大充切換完成後,在走均充流程
  5641. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  5642. {
  5643. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  5644. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  5645. {
  5646. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  5647. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  5648. }
  5649. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  5650. {
  5651. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5652. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5653. continue;
  5654. }
  5655. }
  5656. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5657. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5658. continue;
  5659. }
  5660. }
  5661. if (isRessign)
  5662. setChargerMode(gun_index, MODE_REASSIGN);
  5663. else
  5664. setChargerMode(gun_index, MODE_PRECHARGE);
  5665. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5666. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5667. }
  5668. break;
  5669. case S_REASSIGN:
  5670. {
  5671. if (isModeChange(gun_index))
  5672. {
  5673. PRINTF_FUNC("S_REASSIGN================================== %x \n", gun_index);
  5674. gettimeofday(&_toAverage_time, NULL);
  5675. }
  5676. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  5677. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  5678. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  5679. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  5680. {
  5681. if (ShmSysConfigAndInfo->SysInfo.CanAverageCharging)
  5682. setChargerMode(gun_index, MODE_PRECHARGE);
  5683. else
  5684. setChargerMode(gun_index, MODE_IDLE);
  5685. }
  5686. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A &&
  5687. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  5688. {
  5689. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
  5690. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  5691. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5692. }
  5693. //PRINTF_FUNC("CurGunSelected = %d, gun_index = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected, gun_index);
  5694. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5695. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5696. }
  5697. break;
  5698. case S_PREPARNING:
  5699. {
  5700. if (isModeChange(gun_index))
  5701. {
  5702. PRINTF_FUNC("S_PREPARNING================================== %x \n", gun_index);
  5703. StopGunInfoTimeoutDet(gun_index);
  5704. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  5705. }
  5706. if (ShmPsuData->SystemPresentPsuQuantity > 0 && ShmPsuData->SystemAvailablePower > 10 &&
  5707. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 5000000)
  5708. {
  5709. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  5710. }
  5711. if (isEvBoardStopChargeFlag(gun_index) == YES)
  5712. {
  5713. // 板端或後臺要求停止
  5714. PRINTF_FUNC("********** EvBoardStop Gun %d (S_PREPARNING)**********\n", gun_index);
  5715. ChargingTerminalProcess(gun_index);
  5716. }
  5717. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5718. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5719. }
  5720. break;
  5721. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  5722. {
  5723. if (isModeChange(gun_index))
  5724. {
  5725. PRINTF_FUNC("S_PREPARING_FOR_EV================================== %x \n", gun_index);
  5726. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  5727. StopGunInfoTimeoutDet(gun_index);
  5728. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  5729. }
  5730. /*
  5731. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  5732. {
  5733. // 檢查車端的槍鎖是否為鎖上
  5734. if (isEvGunLocked_chademo(gun_index) == YES)
  5735. {
  5736. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  5737. }
  5738. }
  5739. else if (chargingInfo[gun_index]->Type == _Type_GB)
  5740. {
  5741. // 檢查車端的 charging enable 是否為 1
  5742. if (isEvGunLocked_gb(gun_index) == YES)
  5743. {
  5744. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  5745. }
  5746. }
  5747. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  5748. {
  5749. // 檢查車端的 charging enable 是否為 1
  5750. if (isEvGunLocked_ccs(gun_index) == YES)
  5751. {
  5752. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  5753. }
  5754. }
  5755. */
  5756. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  5757. {
  5758. setChargerMode(gun_index, MODE_CHARGING);
  5759. }
  5760. if (isEvBoardStopChargeFlag(gun_index) == YES)
  5761. {
  5762. // 板端或後臺要求停止
  5763. PRINTF_FUNC("********** EvBoardStop Gun %d (S_PREPARING_FOR_EV)**********\n", gun_index);
  5764. ChargingTerminalProcess(gun_index);
  5765. }
  5766. // LCM => Pre-charging
  5767. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5768. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5769. }
  5770. break;
  5771. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  5772. {
  5773. if (isModeChange(gun_index))
  5774. {
  5775. PRINTF_FUNC("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
  5776. StopGunInfoTimeoutDet(gun_index);
  5777. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  5778. }
  5779. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  5780. {
  5781. // 檢查樁端的 GFD 結果
  5782. if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8)
  5783. {
  5784. // 當前操作的槍號,進入 Charging
  5785. setChargerMode(gun_index, MODE_CHARGING);
  5786. }
  5787. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5788. {
  5789. // GFD 錯誤停止
  5790. RecordAlarmCode(gun_index, "012234");
  5791. }
  5792. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5793. {
  5794. // GFD 警告
  5795. RecordWarningCode(gun_index, "012296");
  5796. }
  5797. }
  5798. else if (chargingInfo[gun_index]->Type == _Type_GB)
  5799. {
  5800. // 檢查樁端的 GFD 結果
  5801. if (isPrechargeStatus_gb(gun_index) > 5 && isPrechargeStatus_gb(gun_index) < 9)
  5802. {
  5803. setChargerMode(gun_index, MODE_CHARGING);
  5804. }
  5805. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5806. {
  5807. // GFD 錯誤停止
  5808. RecordAlarmCode(gun_index, "012236");
  5809. }
  5810. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5811. {
  5812. // GFD 警告
  5813. RecordWarningCode(gun_index, "012298");
  5814. }
  5815. }
  5816. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  5817. {
  5818. // 檢查樁端的 GFD 結果
  5819. if ((chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  5820. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING))
  5821. {
  5822. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
  5823. }
  5824. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5825. {
  5826. // GFD 錯誤停止
  5827. RecordAlarmCode(gun_index, "012235");
  5828. }
  5829. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5830. {
  5831. // GFD 警告
  5832. RecordWarningCode(gun_index, "012297");
  5833. }
  5834. }
  5835. if (isEvBoardStopChargeFlag(gun_index) == YES)
  5836. {
  5837. // 板端或後臺要求停止
  5838. ChargingTerminalProcess(gun_index);
  5839. }
  5840. // LCM => Pre-charging
  5841. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5842. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5843. }
  5844. break;
  5845. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  5846. {
  5847. if (isModeChange(gun_index))
  5848. {
  5849. PRINTF_FUNC("S_CHARGING================================== %x \n", gun_index);
  5850. StopGunInfoTimeoutDet(gun_index);
  5851. ftime(&startChargingTime[gun_index]);
  5852. ChangeStartOrStopDateTime(YES, gun_index);
  5853. OcppStartTransation(gun_index);
  5854. }
  5855. if (ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf)
  5856. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = NO;
  5857. ftime(&endChargingTime[gun_index]);
  5858. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  5859. // 計算 Power
  5860. chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  5861. if (chargingTime[gun_index] == 0 || chargingTime[gun_index] > chargingInfo[gun_index]->PresentChargedDuration)
  5862. {
  5863. chargingTime[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  5864. }
  5865. else
  5866. {
  5867. int passTime = chargingInfo[gun_index]->PresentChargedDuration - chargingTime[gun_index];
  5868. if(passTime > 0)
  5869. {
  5870. float changingPow = (chargingInfo[gun_index]->PresentChargingPower) * passTime / 3600;
  5871. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  5872. {
  5873. chargingInfo[gun_index]->ChargingFee += changingPow * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  5874. }
  5875. chargingInfo[gun_index]->PresentChargedEnergy += changingPow;
  5876. chargingInfo[gun_index]->PowerConsumption += changingPow;
  5877. chargingTime[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  5878. }
  5879. }
  5880. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  5881. {
  5882. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5883. {
  5884. // GFD 錯誤停止
  5885. RecordAlarmCode(gun_index, "012234");
  5886. }
  5887. //else if (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  5888. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))
  5889. //{
  5890. // UVP
  5891. // RecordAlarmCode(gun_index, "012289");
  5892. // ChargingTerminalProcess(gun_index);
  5893. //}
  5894. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5895. {
  5896. // GFD 警告
  5897. RecordWarningCode(gun_index, "012296");
  5898. }
  5899. }
  5900. else if (chargingInfo[gun_index]->Type == _Type_GB)
  5901. {
  5902. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5903. {
  5904. // GFD 錯誤停止
  5905. RecordAlarmCode(gun_index, "012236");
  5906. }
  5907. //else if (isPrechargeStatus_gb(gun_index) == 10 &&
  5908. // (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  5909. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL)))
  5910. //{
  5911. // UVP
  5912. // RecordAlarmCode(gun_index, "012290");
  5913. // ChargingTerminalProcess(gun_index);
  5914. //}
  5915. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5916. {
  5917. // GFD 警告
  5918. RecordWarningCode(gun_index, "012298");
  5919. }
  5920. }
  5921. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  5922. {
  5923. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5924. {
  5925. // GFD 錯誤停止
  5926. RecordAlarmCode(gun_index, "012235");
  5927. }
  5928. //else if (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  5929. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))
  5930. //{
  5931. // UVP
  5932. // RecordAlarmCode(gun_index, "012288");
  5933. // ChargingTerminalProcess(gun_index);
  5934. //}
  5935. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5936. {
  5937. // GFD 警告
  5938. RecordWarningCode(gun_index, "012297");
  5939. }
  5940. }
  5941. if (isEvBoardStopChargeFlag(gun_index) ||
  5942. CheckBackendChargingTimeout(gun_index) ||
  5943. CheckBackendChargingEnergy(gun_index) ||
  5944. strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid") == EQUAL)
  5945. {
  5946. PRINTF_FUNC("S_CHARGING=======Stop=========================== %f \n", (chargingInfo[gun_index]->EvBatterytargetVoltage * 10));
  5947. // 板端或後臺要求停止
  5948. ChargingTerminalProcess(gun_index);
  5949. }
  5950. // LCM => Charging
  5951. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5952. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  5953. }
  5954. break;
  5955. case S_TERMINATING:
  5956. {
  5957. if (isModeChange(gun_index))
  5958. {
  5959. PRINTF_FUNC ("terminating......................... %x \n", gun_index);
  5960. StopGunInfoTimeoutDet(gun_index);
  5961. }
  5962. setChargerMode(gun_index, MODE_COMPLETE);
  5963. /*
  5964. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  5965. {
  5966. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  5967. if (isEvStopCharging_chademo(gun_index) == YES)
  5968. {
  5969. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  5970. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  5971. }
  5972. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5973. {
  5974. // GFD 錯誤停止
  5975. RecordAlarmCode(gun_index, "012234");
  5976. }
  5977. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5978. {
  5979. // GFD 警告
  5980. RecordWarningCode(gun_index, "012296");
  5981. }
  5982. if (isEvStopCharging_chademo(gun_index) == YES ||
  5983. isPrechargeStatus_chademo(gun_index) <= 0)
  5984. {
  5985. setChargerMode(gun_index, MODE_COMPLETE);
  5986. }
  5987. }
  5988. else if (chargingInfo[gun_index]->Type == _Type_GB)
  5989. {
  5990. PRINTF_FUNC("************ GB lock Status = %d, status = %d \n", isEvStopCharging_gb(gun_index), isPrechargeStatus_gb(gun_index));
  5991. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  5992. if (isEvStopCharging_chademo(gun_index) == YES)
  5993. {
  5994. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  5995. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  5996. }
  5997. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5998. {
  5999. // GFD 錯誤停止
  6000. RecordAlarmCode(gun_index, "012236");
  6001. }
  6002. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  6003. {
  6004. // GFD 警告
  6005. RecordWarningCode(gun_index, "012298");
  6006. }
  6007. if (isEvStopCharging_gb(gun_index) == YES ||
  6008. isPrechargeStatus_gb(gun_index) <= 0)
  6009. {
  6010. setChargerMode(gun_index, MODE_COMPLETE);
  6011. }
  6012. }
  6013. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  6014. {
  6015. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  6016. if (isEvStopCharging_chademo(gun_index) == YES)
  6017. {
  6018. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6019. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  6020. }
  6021. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  6022. {
  6023. // GFD 錯誤停止
  6024. RecordAlarmCode(gun_index, "012235");
  6025. }
  6026. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  6027. {
  6028. // GFD 警告
  6029. RecordWarningCode(gun_index, "012297");
  6030. }
  6031. if (isEvStopCharging_ccs(gun_index) == YES &&
  6032. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  6033. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14))
  6034. {
  6035. setChargerMode(gun_index, MODE_COMPLETE);
  6036. }
  6037. }
  6038. // 車端的停止
  6039. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  6040. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  6041. */
  6042. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6043. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  6044. }
  6045. break;
  6046. case S_COMPLETE:
  6047. {
  6048. if (isModeChange(gun_index))
  6049. {
  6050. PRINTF_FUNC ("complete......................... %x \n", gun_index);
  6051. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  6052. {
  6053. OcppStopTransation(gun_index);
  6054. }
  6055. ftime(&endChargingTime[gun_index]);
  6056. if (chargingInfo[gun_index]->PresentChargedDuration != 0)
  6057. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  6058. chargingInfo[gun_index]->isRemoteStart = NO;
  6059. StopGunInfoTimeoutDet(gun_index);
  6060. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  6061. ChangeStartOrStopDateTime(NO, gun_index);
  6062. }
  6063. if(GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  6064. {
  6065. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  6066. {
  6067. PRINTF_FUNC ("********** Gun %d psu is releasable **********\n", gun_index);
  6068. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  6069. }
  6070. }
  6071. if((chargingInfo[gun_index]->ConnectorPlugIn == NO || ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false) &&
  6072. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  6073. {
  6074. setChargerMode(gun_index, MODE_IDLE);
  6075. }
  6076. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  6077. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  6078. }
  6079. break;
  6080. /*
  6081. case S_CCS_PRECHARGE_ST0:
  6082. {
  6083. if (isModeChange(gun_index))
  6084. {
  6085. PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
  6086. StopGunInfoTimeoutDet(gun_index);
  6087. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  6088. }
  6089. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  6090. {
  6091. // GFD 錯誤停止
  6092. RecordAlarmCode(gun_index, "012235");
  6093. }
  6094. if (isEvBoardStopChargeFlag(gun_index) == YES)
  6095. {
  6096. // 板端要求停止
  6097. ChargingTerminalProcess(gun_index);
  6098. }
  6099. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  6100. // 切換 D+ Relay to Precharge Relay
  6101. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  6102. {
  6103. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  6104. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  6105. {
  6106. PRINTF_FUNC("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
  6107. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  6108. }
  6109. }
  6110. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  6111. {
  6112. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
  6113. }
  6114. break;
  6115. }
  6116. case S_CCS_PRECHARGE_ST1:
  6117. {
  6118. if (isModeChange(gun_index))
  6119. {
  6120. PRINTF_FUNC("CCS Precharge Processing 2....................%x \n", gun_index);
  6121. }
  6122. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  6123. {
  6124. // GFD 錯誤停止
  6125. RecordAlarmCode(gun_index, "012235");
  6126. }
  6127. if (isEvBoardStopChargeFlag(gun_index) == YES)
  6128. {
  6129. // 板端要求停止
  6130. ChargingTerminalProcess(gun_index);
  6131. }
  6132. // 等待小板通知進入充電
  6133. // 切換 D+ Relay to Precharge Relay
  6134. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  6135. {
  6136. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  6137. setChargerMode(gun_index, MODE_CHARGING);
  6138. }
  6139. break;
  6140. }
  6141. */
  6142. }
  6143. }
  6144. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  6145. {
  6146. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  6147. }
  6148. else
  6149. {
  6150. bool dcPageRun = true;
  6151. // if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  6152. // {
  6153. // if (ac_chargingInfo[0]->SystemStatus == S_IDLE)
  6154. // ChangeLcmByIndex(_LCM_IDLE);
  6155. // else if (ac_chargingInfo[0]->SystemStatus == S_PREPARNING)
  6156. // ChangeLcmByIndex(_LCM_PRE_CHARGE);
  6157. // else if (ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  6158. // ChangeLcmByIndex(_LCM_CHARGING);
  6159. // else if (ac_chargingInfo[0]->SystemStatus == S_TERMINATING ||
  6160. // ac_chargingInfo[0]->SystemStatus == S_COMPLETE)
  6161. // ChangeLcmByIndex(_LCM_COMPLETE);
  6162. // else
  6163. // dcPageRun = true;
  6164. // }
  6165. // else
  6166. // dcPageRun = true;
  6167. if (dcPageRun)
  6168. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  6169. }
  6170. write(wtdFd, "a", 1);
  6171. usleep(whileLoopTime);
  6172. }
  6173. return FAIL;
  6174. }