main.c 166 KB

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