main.c 214 KB

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