main.c 198 KB

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