main.c 212 KB

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