main.c 204 KB

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