main.c 213 KB

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