main.c 192 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008
  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 <stdbool.h>
  30. #include <dirent.h>
  31. #include <signal.h>
  32. #include "../Config.h"
  33. #include "main.h"
  34. #include "../common.h"
  35. #include "../timeout.h"
  36. #include "../Log/log.h"
  37. #include "../DataBase/DataBase.h"
  38. #include "../Define/define.h"
  39. #include "../ShareMemory/shmMem.h"
  40. #include "../SelectGun/SelectGun.h"
  41. //------------------------------------------------------------------------------
  42. static struct SysInfoData *pSysInfo = NULL;
  43. static struct SysConfigData *pSysConfig = NULL;
  44. static struct WARNING_CODE_INFO *pSysWarning = NULL;
  45. static struct AlarmCodeData *pAlarmCode = NULL;
  46. static struct FaultCodeData *pFaultCode = NULL;
  47. static struct InfoCodeData *pInfoCode = NULL;
  48. static struct PsuData *ShmPsuData = NULL;
  49. static struct CHAdeMOData *ShmCHAdeMOData = NULL;
  50. static struct GBTData *ShmGBTData = NULL;
  51. static struct CcsData *ShmCcsData = NULL;
  52. static struct PrimaryMcuData *ShmPrimaryMcuData = NULL;
  53. static struct FanModuleData *ShmFanModuleData = NULL;
  54. static struct RelayModuleData *ShmRelayModuleData = NULL;
  55. static struct LedModuleData *ShmLedModuleData = NULL;
  56. static struct OCPP16Data *ShmOCPP16Data = NULL;
  57. static struct OCPP20Data* ShmOCPP20Data = NULL;
  58. static DcCommonInfo *ShmDcCommonData = NULL;
  59. static struct ChargingInfoData *pDcChargingInfo = NULL;
  60. static struct ChargingInfoData *pAcChargingInfo = NULL;
  61. static struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  62. static struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  63. static SelectGunInfo *ShmSelectGunInfo = NULL; //Jerry add
  64. static EvBoardErrMsg gEvBoardErr = {0};
  65. static ChillerTempErr gChillerTempErr = {0};
  66. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  67. struct StatusCodeData *ShmStatusCodeData;
  68. #define DERATING_TARGET_LEVEL 5
  69. // for initial index to check EV board type is correct
  70. uint8_t bd0_1_status = 0;
  71. uint8_t bd0_2_status = 0;
  72. uint8_t bd1_1_status = 0;
  73. uint8_t bd1_2_status = 0;
  74. char *fwVersion = "V2.16.00.0000.00"; // Phihong version
  75. char* DebugVersion = "V2.16.03"; // Software debug version
  76. //sqlite3 *localDb;
  77. bool isDb_ready;
  78. //------------------------------------------------------------------------------
  79. void ClearDetectPluginFlag(int gunIndex);
  80. long long DiffTimebWithNow(struct timeb ST);
  81. uint8_t DetectBitValue(uint8_t _byte, uint8_t _bit);
  82. void SetBitValue(uint8_t *_byte, uint8_t _bit, uint8_t value);
  83. unsigned long GetTimeoutValue(struct timeval _sour_time);
  84. unsigned long GetClockTimeoutValue(struct timespec _start_time);
  85. void GetClockTime(struct timespec *_now_time, void *null);
  86. void gpio_set_value(unsigned int gpio, unsigned int value);
  87. void InformOcppErrOccur(uint8_t codeType);
  88. void RecordAlarmCode(uint8_t gunIndex, char *code);
  89. void ReleaseAlarmCode(uint8_t gunIndex);
  90. void ResetChargerAlarmCode(uint8_t gunIndex, char *code);
  91. void AdjustChargerCurrent(void);
  92. void UpdateErrorCodeToOcpp(uint8_t index);
  93. //------------------------------------------------------------------------------
  94. //Primary.c
  95. extern void ChkPrimaryStatus(void);
  96. //RFID.c
  97. extern void CreateRfidFork(void);
  98. extern void ScannerCardProcess(int gunIndex);
  99. extern bool RfidStopCharging(void);
  100. extern bool GetIsCardScan(void);
  101. extern void SetIsCardScan(bool value);
  102. //SelfTest.c
  103. extern void SelfTestRun(void);
  104. //UpgradeFW.c
  105. /*extern void CheckFwUpdateFunction(void);*/
  106. //Ethernet.c
  107. extern void InitEthernet(void);
  108. extern void GetMacAddress(void);
  109. //WatchDog.c
  110. extern void CreateWatchdog(void);
  111. extern void TryCloseWatchdog(void);
  112. extern void TryFeedWatchdog(void);
  113. //ZipFile.c
  114. extern void zipLogFiles(void);
  115. //------------------------------------------------------------------------------
  116. //--- share memory value ---
  117. //------------------------------------------------------------------------------
  118. static void changeLcmPage(uint8_t index)
  119. {
  120. pSysInfo->SystemPage = index;
  121. }
  122. static uint8_t getCurLcmPage(void)
  123. {
  124. return pSysInfo->SystemPage;
  125. }
  126. static void systemPageRestoreInit(void)
  127. {
  128. int is_idle = TRUE;
  129. int gunIndex;
  130. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  131. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  132. // 檢查電樁狀態是否為idle狀態
  133. if ((pDcChargingInfo->SystemStatus >= S_AUTHORIZING &&
  134. pDcChargingInfo->SystemStatus <= S_RESERVATION) ||
  135. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  136. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST0)) {
  137. is_idle = FALSE;
  138. }
  139. }
  140. if (is_idle)
  141. pSysInfo->SystemPage = _PAGE_IDLE;
  142. else
  143. pSysInfo->SystemPage = _PAGE_SELECT_GUN;
  144. }
  145. //------------------------------------------------------------------------------
  146. void destroySelGun(uint8_t curGun)
  147. {
  148. uint8_t i = 0;
  149. uint8_t totalGun = pSysConfig->TotalConnectorCount;
  150. //for status timeout
  151. if (curGun == DESTROY_ALL_SEL) {
  152. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  153. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  154. log_info("destroy all gun = %d, %d",
  155. ShmSelectGunInfo->SelGunInfo.LeftGun,
  156. ShmSelectGunInfo->SelGunInfo.RightGun);
  157. for (i = 0; i < totalGun; i++) {
  158. StopGunInfoTimeoutDet(i);
  159. memset(&ShmSelectGunInfo->PricesInfo[i], 0, sizeof(PricesInfo));
  160. }
  161. pSysInfo->CurGunSelected = 0;
  162. strcpy((char *)pSysConfig->UserId, "");
  163. //changeLcmPage(_LCM_VIEW);
  164. return;
  165. }
  166. //for charging timeout or complete
  167. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun != SEL_GUN_RELEASE)) {
  168. if (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  169. ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
  170. //changeLcmPage(_PAGE_SELECT_GUN);
  171. }
  172. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  173. StopGunInfoTimeoutDet(LEFT_GUN_NUM);
  174. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  175. ClearAuthorizedFlag();
  176. }
  177. log_info("destroy left gun, cur page = %d", getCurLcmPage());
  178. if (getCurLcmPage() == pSysInfo->SystemPage) {
  179. log_info("clear left balance");
  180. memset(&ShmSelectGunInfo->PricesInfo[curGun], 0, sizeof(PricesInfo));
  181. ShmSelectGunInfo->PricesInfo[curGun].Balance = FAIL_BALANCE_PRICES;
  182. }
  183. }
  184. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun != SEL_GUN_RELEASE)) {
  185. if (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  186. ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
  187. //changeLcmPage(_PAGE_SELECT_GUN);
  188. }
  189. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  190. StopGunInfoTimeoutDet(RIGHT_GUN_NUM);
  191. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  192. ClearAuthorizedFlag();
  193. }
  194. log_info("destroy right gun, cur page = %d", getCurLcmPage());
  195. if (getCurLcmPage() == pSysInfo->SystemPage) {
  196. log_info("clear right balance");
  197. memset(&ShmSelectGunInfo->PricesInfo[curGun], 0, sizeof(PricesInfo));
  198. ShmSelectGunInfo->PricesInfo[curGun].Balance = FAIL_BALANCE_PRICES;
  199. }
  200. }
  201. }
  202. static int waitRightGunPlugIt(uint8_t curGun)
  203. {
  204. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  205. return PASS;
  206. }
  207. return FAIL;
  208. }
  209. static int waitLeftGunPlugIt(uint8_t curGun)
  210. {
  211. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  212. return PASS;
  213. }
  214. return FAIL;
  215. }
  216. void setSelGunWaitToAuthor(uint8_t curSel)
  217. {
  218. if (curSel == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM) {
  219. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_ATHOR;
  220. //log_info("setSelGunWaitToAuthor left");
  221. StopGunInfoTimeoutDet(curSel);
  222. } else if (curSel == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM) {
  223. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_ATHOR;
  224. //log_info("setSelGunWaitToAuthor right");
  225. StopGunInfoTimeoutDet(curSel);
  226. }
  227. }
  228. int getConfirmSelectedGun(uint8_t curSel)
  229. {
  230. if (((curSel == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
  231. ((curSel == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
  232. return PASS;
  233. }
  234. return FAIL;
  235. }
  236. void confirmSelGun(uint8_t selGun)
  237. {
  238. if (selGun == LEFT_GUN_NUM) {
  239. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
  240. //printf("confirmSelGun left");
  241. } else if (selGun == RIGHT_GUN_NUM) {
  242. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
  243. //printf("confirmSelGun right");
  244. }
  245. //changeLcmPage(_PAGE_PLUGIN);
  246. //StartGunInfoTimeoutDet(selGun, Timeout_SelectGun);
  247. //StartSystemTimeoutDet(Timeout_ReturnViewPage);
  248. }
  249. static void GetFirmwareVersion(void)
  250. {
  251. // Get CSU root file system version
  252. sprintf((char *)pSysInfo->CsuRootFsFwRev, fwVersion);
  253. sprintf((char*)ShmDcCommonData->DebugVersion, DebugVersion);
  254. uint8_t count = 0, chademo = 0, ccs = 0, gb = 0;
  255. for (uint8_t idx = 0; idx < 3; idx++) {
  256. if (pSysConfig->ModelName[7 + idx] == 'J') {
  257. chademo++;
  258. count++;
  259. } else if (pSysConfig->ModelName[7 + idx] == 'G') {
  260. gb++;
  261. count++;
  262. } else if (pSysConfig->ModelName[7 + idx] == 'U' ||
  263. pSysConfig->ModelName[7 + idx] == 'V' ||
  264. pSysConfig->ModelName[7 + idx] == 'E') {
  265. ccs++;
  266. count++;
  267. }
  268. }
  269. if (count == 1) {
  270. if (chademo > 0) {
  271. pSysInfo->CsuRootFsFwRev[7] = '1';
  272. } else if (ccs > 0) {
  273. pSysInfo->CsuRootFsFwRev[7] = '2';
  274. } else if (gb > 0) {
  275. pSysInfo->CsuRootFsFwRev[7] = '3';
  276. }
  277. } else {
  278. if (chademo > 0 && ccs > 0) {
  279. pSysInfo->CsuRootFsFwRev[7] = '4';
  280. } else if (chademo > 0 && gb > 0) {
  281. pSysInfo->CsuRootFsFwRev[7] = '5';
  282. } else if (ccs > 0 && gb > 0) {
  283. pSysInfo->CsuRootFsFwRev[7] = '6';
  284. }
  285. }
  286. // Get network option from model name
  287. switch (pSysConfig->ModelName[10]) {
  288. case 'B':
  289. case 'U':
  290. //Blue tooth
  291. pSysInfo->CsuRootFsFwRev[9] = '3';
  292. break;
  293. case 'W':
  294. // WIFI
  295. pSysInfo->CsuRootFsFwRev[9] = '1';
  296. break;
  297. case 'T':
  298. // 3G/4G
  299. pSysInfo->CsuRootFsFwRev[9] = '2';
  300. break;
  301. case 'D': //DS60-120 add
  302. pSysInfo->CsuRootFsFwRev[9] = '5';
  303. break;
  304. default:
  305. // LAN
  306. pSysInfo->CsuRootFsFwRev[9] = '0';
  307. break;
  308. }
  309. // Get rating power from model name
  310. memcpy(&pSysInfo->CsuRootFsFwRev[10], &pSysConfig->ModelName[4], 0x03);
  311. // Get IEC or UL
  312. char _buf[3] = {0};
  313. memcpy(_buf, &pSysConfig->ModelName[2], 2);
  314. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL) {
  315. pSysInfo->ChargerType = _CHARGER_TYPE_IEC;
  316. log_info("IEC model");
  317. } else if (strcmp(_buf, "WU") == EQUAL) {
  318. pSysInfo->ChargerType = _CHARGER_TYPE_UL;
  319. log_info("UL model");
  320. }
  321. }
  322. static void checkGunOTPState(uint8_t gunIndex)
  323. {
  324. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  325. //水冷機溫度檢測
  326. if (((ShmDcCommonData->ChillerValve.MultiChillerGun & 0x80) >> 7) == YES) {
  327. if (pDcChargingInfo->ChillerTemp != UNDEFINED_TEMP) {
  328. if (pDcChargingInfo->ChillerTemp >= GUN_OTP_VALUE) {
  329. if (((gunIndex == 0) &&
  330. ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  331. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)))
  332. ||
  333. ((gunIndex == 1) &&
  334. ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  335. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)))
  336. ) {
  337. RecordAlarmCode(gunIndex, "012323");
  338. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = YES;
  339. }
  340. else
  341. {
  342. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  343. }
  344. } else if (pDcChargingInfo->ChillerTemp != 0 &&
  345. pDcChargingInfo->ChillerTemp < GUN_OTP_RECOVERY) {
  346. //ResetChargerAlarmCode(gunIndex, "012323");
  347. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  348. }
  349. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  350. } else {
  351. // 沒接上 Sensor or 異常
  352. //RecordAlarmCode(gunIndex, "011038");
  353. //ResetChargerAlarmCode(gunIndex, "012323");
  354. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  355. if (((gunIndex == 0) &&
  356. ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  357. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)))
  358. ||
  359. ((gunIndex == 1) &&
  360. ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  361. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)))
  362. ) {
  363. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = YES;
  364. }
  365. else
  366. {
  367. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  368. }
  369. }
  370. if(pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault == YES)
  371. {
  372. RecordAlarmCode(gunIndex, "011037");
  373. }
  374. }
  375. switch (pDcChargingInfo->Type) {
  376. case _Type_Chademo:
  377. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  378. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  379. RecordAlarmCode(gunIndex, "012229");
  380. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = YES;
  381. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  382. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  383. //ResetChargerAlarmCode(gunIndex, "012229");
  384. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  385. }
  386. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  387. } else {
  388. // 沒接上 Sensor or 異常
  389. //RecordAlarmCode(gunIndex, "011018");
  390. //ResetChargerAlarmCode(gunIndex, "012229");
  391. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  392. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = YES;
  393. if ((gunIndex == 0) &&
  394. (strncmp((char *)&pSysConfig->ModelName[7], "J", 1) == 0)) {
  395. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  396. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  397. } else if ((gunIndex == 1) &&
  398. ((strncmp((char *)&pSysConfig->ModelName[9], "J", 1) == 0) )) {
  399. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  400. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  401. }
  402. }
  403. break;
  404. case _Type_CCS_2:
  405. // CCS 不管甚麼輸出都會有槍溫偵測!!~
  406. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  407. //ResetChargerAlarmCode(gunIndex, "011019");
  408. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  409. RecordAlarmCode(gunIndex, "012230");
  410. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = YES;
  411. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  412. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  413. //ResetChargerAlarmCode(gunIndex, "012230");
  414. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  415. }
  416. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  417. //ResetChargerAlarmCode(gunIndex, "011019");
  418. } else {
  419. // 沒接上 Sensor or 異常
  420. //RecordAlarmCode(gunIndex, "011019");
  421. //ResetChargerAlarmCode(gunIndex, "012230");
  422. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  423. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = YES;
  424. }
  425. break;
  426. case _Type_GB:
  427. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  428. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  429. RecordAlarmCode(gunIndex, "012231");
  430. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = YES;
  431. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  432. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  433. //ResetChargerAlarmCode(gunIndex, "012231");
  434. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  435. }
  436. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  437. } else {
  438. // 沒接上 Sensor or 異常
  439. //RecordAlarmCode(gunIndex, "011020");
  440. //ResetChargerAlarmCode(gunIndex, "012231");
  441. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  442. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = YES;
  443. }
  444. break;
  445. }
  446. }
  447. static void collectError(uint8_t gunIndex)
  448. {
  449. gEvBoardErr.GunErrMessage |= ShmDcCommonData->ConnectErrList[gunIndex].GunErrMessage;
  450. gChillerTempErr.TempErrMsg |= ShmDcCommonData->ChillerTempErr[gunIndex].TempErrMsg;
  451. }
  452. static void checkGBTAlarmState(uint8_t gunType)
  453. {
  454. // GFD Trip
  455. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  456. pAlarmCode->AlarmEvents.bits.GbGfdTrip = YES;
  457. } else {
  458. pAlarmCode->AlarmEvents.bits.GbGfdTrip = NO;
  459. }
  460. // UVP
  461. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  462. pAlarmCode->AlarmEvents.bits.GbtOutputUVPFail = YES;
  463. } else {
  464. pAlarmCode->AlarmEvents.bits.GbtOutputUVPFail = NO;
  465. }
  466. // OTP
  467. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  468. pAlarmCode->AlarmEvents.bits.GbConnectorOTP = YES;
  469. } else {
  470. pAlarmCode->AlarmEvents.bits.GbConnectorOTP = NO;
  471. }
  472. // OVP
  473. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  474. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = YES;
  475. } else {
  476. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = NO;
  477. }
  478. // GFD Warning
  479. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  480. pAlarmCode->AlarmEvents.bits.GbGroundfaultWarning = YES;
  481. } else {
  482. pAlarmCode->AlarmEvents.bits.GbGroundfaultWarning = NO;
  483. }
  484. // Relay Welding
  485. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  486. pFaultCode->FaultEvents.bits.GbOutputRelayWelding = YES;
  487. } else {
  488. pFaultCode->FaultEvents.bits.GbOutputRelayWelding = NO;
  489. }
  490. // Relay Driving
  491. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  492. pFaultCode->FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  493. } else {
  494. pFaultCode->FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  495. }
  496. // Connect temp Sensor broken
  497. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  498. pFaultCode->FaultEvents.bits.GbConnectorTempSensorBroken = YES;
  499. } else {
  500. pFaultCode->FaultEvents.bits.GbConnectorTempSensorBroken = NO;
  501. }
  502. }
  503. static void checkCCSAlarmState(uint8_t gunType)
  504. {
  505. // GFD Trip
  506. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  507. pAlarmCode->AlarmEvents.bits.CcsGfdTrip = YES;
  508. } else {
  509. pAlarmCode->AlarmEvents.bits.CcsGfdTrip = NO;
  510. }
  511. // UVP
  512. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  513. pAlarmCode->AlarmEvents.bits.CcsOutputUVPFail = YES;
  514. } else {
  515. pAlarmCode->AlarmEvents.bits.CcsOutputUVPFail = NO;
  516. }
  517. // OTP
  518. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  519. pAlarmCode->AlarmEvents.bits.CcsConnectorOTP = YES;
  520. } else {
  521. pAlarmCode->AlarmEvents.bits.CcsConnectorOTP = NO;
  522. }
  523. // OVP
  524. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  525. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = YES;
  526. } else {
  527. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = NO;
  528. }
  529. // GFD Warning
  530. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  531. pAlarmCode->AlarmEvents.bits.CcsGroundfaultWarning = YES;
  532. } else {
  533. pAlarmCode->AlarmEvents.bits.CcsGroundfaultWarning = NO;
  534. }
  535. // Relay Welding
  536. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  537. pFaultCode->FaultEvents.bits.CcsOutputRelayWelding = YES;
  538. } else {
  539. pFaultCode->FaultEvents.bits.CcsOutputRelayWelding = NO;
  540. }
  541. // Relay Driving
  542. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  543. pFaultCode->FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  544. } else {
  545. pFaultCode->FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  546. }
  547. // Connect temp Sensor broken
  548. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  549. pFaultCode->FaultEvents.bits.CcsConnectorTempSensorBroken = YES;
  550. } else {
  551. pFaultCode->FaultEvents.bits.CcsConnectorTempSensorBroken = NO;
  552. }
  553. }
  554. static void checkChaDeMoAlarmState(uint8_t gunType)
  555. {
  556. // GFD Trip
  557. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  558. pAlarmCode->AlarmEvents.bits.ChademoGfdTrip = YES;
  559. } else {
  560. pAlarmCode->AlarmEvents.bits.ChademoGfdTrip = NO;
  561. }
  562. // UVP
  563. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  564. pAlarmCode->AlarmEvents.bits.ChademoOutputUVPFail = YES;
  565. } else {
  566. pAlarmCode->AlarmEvents.bits.ChademoOutputUVPFail = NO;
  567. }
  568. // OTP
  569. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  570. pAlarmCode->AlarmEvents.bits.ChademoConnectorOTP = YES;
  571. } else {
  572. pAlarmCode->AlarmEvents.bits.ChademoConnectorOTP = NO;
  573. }
  574. // OVP
  575. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  576. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = YES;
  577. } else {
  578. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = NO;
  579. }
  580. // GFD Warning
  581. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  582. pAlarmCode->AlarmEvents.bits.ChademoGroundWarning = YES;
  583. } else {
  584. pAlarmCode->AlarmEvents.bits.ChademoGroundWarning = NO;
  585. }
  586. // Relay Welding
  587. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  588. pFaultCode->FaultEvents.bits.ChademoOutputRelayWelding = YES;
  589. } else {
  590. pFaultCode->FaultEvents.bits.ChademoOutputRelayWelding = NO;
  591. }
  592. // Relay Driving
  593. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  594. pFaultCode->FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  595. } else {
  596. pFaultCode->FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  597. }
  598. // Connect temp Sensor broken
  599. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  600. pFaultCode->FaultEvents.bits.ChademoConnectorTempSensorBroken = YES;
  601. } else {
  602. pFaultCode->FaultEvents.bits.ChademoConnectorTempSensorBroken = NO;
  603. }
  604. }
  605. static void checkChillerAlarmState(void)
  606. {
  607. if (DetectBitValue(gChillerTempErr.TempErrMsg, 0)) {
  608. pAlarmCode->AlarmEvents.bits.SystemChillerOTP = YES;
  609. } else {
  610. pAlarmCode->AlarmEvents.bits.SystemChillerOTP = NO;
  611. }
  612. if (DetectBitValue(gChillerTempErr.TempErrMsg, 1)) {
  613. pFaultCode->FaultEvents.bits.ChillerTempSensorBroken = YES;
  614. } else {
  615. pFaultCode->FaultEvents.bits.ChillerTempSensorBroken = NO;
  616. }
  617. }
  618. static void checkEvBoardAlarmState(uint8_t gunType)
  619. {
  620. switch (gunType) {
  621. case _Type_Chademo:
  622. checkChaDeMoAlarmState(gunType);
  623. break;
  624. case _Type_CCS_2:
  625. checkCCSAlarmState(gunType);
  626. break;
  627. case _Type_GB:
  628. checkGBTAlarmState(gunType);
  629. break;
  630. }
  631. }
  632. unsigned long GetTimeoutValue(struct timeval _sour_time)
  633. {
  634. struct timeval _end_time;
  635. gettimeofday(&_end_time, NULL);
  636. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  637. }
  638. void GetClockTime(struct timespec *_now_time, void *null)
  639. {
  640. clock_gettime(CLOCK_MONOTONIC, _now_time);
  641. }
  642. // return value unit: 1us
  643. unsigned long GetClockTimeoutValue(struct timespec _start_time)
  644. {
  645. struct timespec ts_end;
  646. unsigned long ret = 0;
  647. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  648. ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000000) + ((unsigned long)((ts_end.tv_nsec / 1000) - (_start_time.tv_nsec/ 1000)));
  649. return ret;
  650. }
  651. int DiffTimeb(struct timeb ST, struct timeb ET)
  652. {
  653. //return milli-second
  654. unsigned int StartTime, StopTime;
  655. StartTime = (unsigned int)ST.time;
  656. StopTime = (unsigned int)ET.time;
  657. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  658. return (StopTime - StartTime);
  659. }
  660. bool CheckTimeOut(struct timeb ST)
  661. {
  662. struct timeb ET;
  663. unsigned int StartTime, StopTime;
  664. ftime(&ET);
  665. StartTime = (unsigned int) ST.time;
  666. StopTime = (unsigned int) ET.time;
  667. return (StopTime > StartTime) ? YES : NO;
  668. }
  669. void setChargerMode(uint8_t gunIndex, uint8_t mode)
  670. {
  671. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  672. pDcChargingInfo->SystemStatus = mode;
  673. }
  674. long long DiffTimebWithNow(struct timeb ST)
  675. {
  676. //return milli-second
  677. struct timeb ET;
  678. long long StartTime, StopTime;
  679. ftime(&ET);
  680. StartTime = (long long)ST.time;
  681. StopTime = (long long)ET.time;
  682. return ((StopTime - StartTime) * 1000) + (ET.millitm - ST.millitm);
  683. }
  684. //==========================================
  685. // Log
  686. //==========================================
  687. void CheckFwSlotStatusLog(void)
  688. {
  689. if (bd0_1_status == 0 && bd0_2_status == 1) {
  690. log_info("Connector 1 : Chademo");
  691. } else if (bd0_1_status == 1 && bd0_2_status == 0) {
  692. log_info("Connector 1 : CCS");
  693. } else if (bd0_1_status == 1 && bd0_2_status == 1) {
  694. log_info("Connector 1 : GB");
  695. }
  696. if (bd1_1_status == 0 && bd1_2_status == 1) {
  697. log_info("Connector 2 : Chademo");
  698. } else if (bd1_1_status == 1 && bd1_2_status == 0) {
  699. log_info("Connector 2 : CCS");
  700. } else if (bd1_1_status == 1 && bd1_2_status == 1) {
  701. log_info("Connector 2 : GB");
  702. }
  703. }
  704. void CheckHwSlotStatusLog(uint8_t index)
  705. {
  706. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  707. if (pDcChargingInfo->Type == _Type_Chademo) {
  708. log_info("Hw check : Connector %d, Type : Chademo, Evboard_id = %d ",
  709. index, pDcChargingInfo->Evboard_id);
  710. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  711. log_info("Hw check : Connector %d, Type : CCS, Evboard_id = %d ",
  712. index, pDcChargingInfo->Evboard_id);
  713. } else if (pDcChargingInfo->Type == _Type_GB) {
  714. log_info("Hw check : Connector %d, Type : GB, Evboard_id = %d ",
  715. index, pDcChargingInfo->Evboard_id);
  716. }
  717. }
  718. //=================================
  719. // LCM Page
  720. //=================================
  721. void ChangeLcmByIndex(uint8_t page_index)
  722. {
  723. if (pSysWarning->Level != WARN_LV_ER
  724. ) {
  725. pSysInfo->PageIndex = page_index;
  726. //log_info("LCM index:%d",pSysInfo->PageIndex);
  727. }
  728. }
  729. //======================================================
  730. // Peripheral initial
  731. //======================================================
  732. void InitGPIO()
  733. {
  734. /*****************0~3, 4 bank, bank x 32+ num*********************/
  735. /***************************************************************/
  736. /*************** GPIO 0 ***************************************/
  737. /***************************************************************/
  738. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  739. system("echo 22 > /sys/class/gpio/export");
  740. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  741. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  742. system("echo 23 > /sys/class/gpio/export");
  743. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  744. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  745. system("echo 26 > /sys/class/gpio/export");
  746. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  747. system("echo 1 > /sys/class/gpio/gpio26/value");
  748. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  749. system("echo 27 > /sys/class/gpio/export");
  750. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  751. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  752. system("echo 29 > /sys/class/gpio/export");
  753. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  754. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  755. system("echo 19 > /sys/class/gpio/export");
  756. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  757. system("echo 1 > /sys/class/gpio/gpio19/value");
  758. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  759. system("echo 20 > /sys/class/gpio/export");
  760. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  761. /***************************************************************/
  762. /*************** GPIO 1 ***************************************/
  763. /***************************************************************/
  764. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  765. system("echo 44 > /sys/class/gpio/export");
  766. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  767. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  768. system("echo 45 > /sys/class/gpio/export");
  769. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  770. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  771. system("echo 46 > /sys/class/gpio/export");
  772. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  773. system("echo 0 > /sys/class/gpio/gpio46/value");
  774. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  775. system("echo 47 > /sys/class/gpio/export");
  776. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  777. /***************************************************************/
  778. /*************** GPIO 2 ***************************************/
  779. /***************************************************************/
  780. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  781. system("echo 89 > /sys/class/gpio/export");
  782. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  783. system("echo 1 > /sys/class/gpio/gpio89/value");
  784. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  785. system("echo 87 > /sys/class/gpio/export");
  786. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  787. system("echo 0 > /sys/class/gpio/gpio87/value");
  788. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  789. system("echo 88 > /sys/class/gpio/export");
  790. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  791. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  792. system("echo 86 > /sys/class/gpio/export");
  793. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  794. /***************************************************************/
  795. /*************** GPIO 3 ***************************************/
  796. /***************************************************************/
  797. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  798. system("echo 111 > /sys/class/gpio/export");
  799. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  800. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  801. system("echo 114 > /sys/class/gpio/export");
  802. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  803. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  804. system("echo 113 > /sys/class/gpio/export");
  805. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  806. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  807. system("echo 110 > /sys/class/gpio/export");
  808. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  809. system("echo 0 > /sys/class/gpio/gpio110/value");
  810. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  811. system("echo 115 > /sys/class/gpio/export");
  812. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  813. system("echo 0 > /sys/class/gpio/gpio115/value");
  814. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  815. system("echo 112 > /sys/class/gpio/export");
  816. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  817. system("echo 1 > /sys/class/gpio/gpio112/value");
  818. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  819. system("echo 116 > /sys/class/gpio/export");
  820. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  821. system("echo 0 > /sys/class/gpio/gpio116/value");
  822. log_info("Initial GPIO OK");
  823. return ;
  824. }
  825. int LoadSysConfigAndInfo()
  826. {
  827. int fd, wrd;
  828. unsigned char *buf;
  829. unsigned int ChkSum, ChkSumOrg;
  830. if ((buf = malloc(MtdBlockSize)) == NULL) {
  831. log_error("malloc buffer NG,rebooting..");
  832. if (pAlarmCode != NULL) {
  833. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  834. }
  835. sleep(5);
  836. system("reboot -f");
  837. sleep(5);
  838. system("reboot -f");
  839. }
  840. memset(buf, 0, MtdBlockSize);
  841. //================================================
  842. // Load configuration from mtdblock10
  843. //================================================
  844. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  845. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  846. if (fd < 0) {
  847. free(buf);
  848. log_error("open mtdblock10 NG,rebooting..");
  849. if (pAlarmCode != NULL) {
  850. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  851. }
  852. sleep(5);
  853. system("reboot -f");
  854. sleep(5);
  855. system("reboot -f");
  856. }
  857. wrd = read(fd, buf, MtdBlockSize);
  858. close(fd);
  859. if (wrd < MtdBlockSize) {
  860. free(buf);
  861. log_error("read SysConfigData data NG,rebooting..");
  862. if (pAlarmCode != NULL) {
  863. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  864. }
  865. sleep(5);
  866. system("reboot -f");
  867. sleep(5);
  868. system("reboot -f");
  869. }
  870. ChkSum = 0;
  871. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  872. ChkSum += buf[wrd];
  873. }
  874. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  875. memcpy(&pSysConfig->ModelName, buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev)), ARRAY_SIZE(pSysConfig->ModelName));
  876. memcpy(&pSysConfig->SerialNumber, buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev) + ARRAY_SIZE(pSysConfig->ModelName) + ARRAY_SIZE(pSysConfig->AcModelName)), ARRAY_SIZE(pSysConfig->SerialNumber));
  877. //================================================
  878. // Load configuration from mtdblock11
  879. //================================================
  880. if (ChkSum != ChkSumOrg) {
  881. log_error("Primary SysConfigData checksum NG, read backup");
  882. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  883. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  884. if (fd < 0) {
  885. free(buf);
  886. log_error("open mtdblock11 (backup) NG,rebooting..");
  887. if (pAlarmCode != NULL) {
  888. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  889. }
  890. sleep(5);
  891. system("reboot -f");
  892. sleep(5);
  893. system("reboot -f");
  894. }
  895. memset(buf, 0, MtdBlockSize);
  896. wrd = read(fd, buf, MtdBlockSize);
  897. close(fd);
  898. if (wrd < MtdBlockSize) {
  899. free(buf);
  900. log_error("read backup SysConfigData data NG,rebooting..");
  901. if (pAlarmCode != NULL) {
  902. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  903. }
  904. sleep(5);
  905. system("reboot -f");
  906. sleep(5);
  907. system("reboot -f");
  908. }
  909. ChkSum = 0;
  910. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  911. ChkSum += buf[wrd];
  912. }
  913. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  914. //================================================
  915. // Load configuration from mtdblock12 (Factory default)
  916. //================================================
  917. if (ChkSum != ChkSumOrg) {
  918. log_warn("backup SysConfigData checksum NG, read Factory default");
  919. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  920. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  921. if (fd < 0) {
  922. log_error("open mtdblock12 (Factory default) NG,rebooting..");
  923. free(buf);
  924. if (pAlarmCode != NULL) {
  925. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  926. }
  927. sleep(5);
  928. system("reboot -f");
  929. sleep(5);
  930. system("reboot -f");
  931. }
  932. memset(buf, 0, MtdBlockSize);
  933. wrd = read(fd, buf, MtdBlockSize);
  934. close(fd);
  935. if (wrd < MtdBlockSize) {
  936. log_error("read factory default SysConfigData data NG,rebooting..");
  937. free(buf);
  938. if (pAlarmCode != NULL) {
  939. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  940. }
  941. sleep(5);
  942. system("reboot -f");
  943. sleep(5);
  944. system("reboot -f");
  945. }
  946. ChkSum = 0;
  947. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  948. ChkSum += buf[wrd];
  949. }
  950. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  951. memcpy(buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev)), &pSysConfig->ModelName, ARRAY_SIZE(pSysConfig->ModelName));
  952. memcpy(buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev) + ARRAY_SIZE(pSysConfig->ModelName) + ARRAY_SIZE(pSysConfig->AcModelName)), &pSysConfig->SerialNumber, ARRAY_SIZE(pSysConfig->SerialNumber));
  953. if (ChkSum != ChkSumOrg) {
  954. log_warn("factory default SysConfigData checksum NG, restore factory default");
  955. free(buf);
  956. system("cd /root;./FactoryConfig -m");
  957. system("rm -f /Storage/OCPP/OCPPConfiguration");
  958. system("sync");
  959. sleep(5);
  960. system("reboot -f");
  961. sleep(5);
  962. system("reboot -f");
  963. return FAIL;
  964. }
  965. }
  966. }
  967. //load OK
  968. memcpy(pSysConfig, buf, sizeof(struct SysConfigData));
  969. free(buf);
  970. system("rm -f /mnt/EvseConfig.bin");
  971. // SysConfig in flash is empty (0xffffffff)
  972. if ((strlen((char *)pSysConfig->ModelName) > ARRAY_SIZE(pSysConfig->ModelName)) ||
  973. (strlen((char *)pSysConfig->SerialNumber) > ARRAY_SIZE(pSysConfig->SerialNumber)) ||
  974. (strlen((char *)pSysConfig->SystemId) > ARRAY_SIZE(pSysConfig->SystemId)) ||
  975. (pSysConfig->Eth0Interface.EthDhcpClient == 0xff)) {
  976. if (strlen((char *)pSysConfig->ModelName) > ARRAY_SIZE(pSysConfig->ModelName)) {
  977. memset(pSysConfig->ModelName, 0x00, ARRAY_SIZE(pSysConfig->ModelName));
  978. }
  979. if (strlen((char *)pSysConfig->SerialNumber) > ARRAY_SIZE(pSysConfig->SerialNumber)) {
  980. memset(pSysConfig->SerialNumber, 0x00, ARRAY_SIZE(pSysConfig->SerialNumber));
  981. }
  982. if (strlen((char *)pSysConfig->SystemId) > ARRAY_SIZE(pSysConfig->SystemId)) {
  983. memset(pSysConfig->SystemId, 0x00, ARRAY_SIZE(pSysConfig->SystemId));
  984. }
  985. if (pSysConfig->Eth0Interface.EthDhcpClient == 0xff) {
  986. log_info("Ethernet dhcp config is null.");
  987. }
  988. if (strlen((char *)pSysConfig->ModelName) == 0x00) {
  989. log_info("Model name over length.");
  990. }
  991. if (strlen((char *)pSysConfig->SerialNumber) == 0x00) {
  992. log_info("Model serial number over length.");
  993. }
  994. if (strlen((char *)pSysConfig->SystemId) == 0x00) {
  995. log_info("SystemId over length.");
  996. }
  997. system("cd /root;./FactoryConfig -m");
  998. sleep(3);
  999. system("/usr/bin/run_evse_restart.sh");
  1000. }
  1001. log_info("Load SysConfigData OK");
  1002. return PASS;
  1003. }
  1004. int Initialization(void)
  1005. {
  1006. uint8_t count = 0;
  1007. uint8_t pinOut[2] = {116, 115};
  1008. // 初始化卡號驗證的 Flag
  1009. ClearAuthorizedFlag();
  1010. for (count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  1011. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  1012. // 初始化插槍驗證的 Flag
  1013. ClearDetectPluginFlag(count);
  1014. pDcChargingInfo->RemoteStartFlag = NO;
  1015. if (pDcChargingInfo->Type == _Type_Chademo) {
  1016. gpio_set_value(pinOut[count], 0x00);
  1017. ShmCHAdeMOData->evse[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  1018. } else if (pDcChargingInfo->Type == _Type_GB) {
  1019. gpio_set_value(pinOut[count], 0x00);
  1020. ShmGBTData->evse[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  1021. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1022. //if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) { //DS60-120 remove
  1023. if (pSysConfig->TotalConnectorCount == 1) {
  1024. gpio_set_value(pinOut[1], 0x01);
  1025. } else {
  1026. gpio_set_value(pinOut[count], 0x01);
  1027. }
  1028. ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  1029. //}
  1030. }
  1031. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  1032. }
  1033. for (count = 0; count < pSysConfig->AcConnectorCount; count++) {
  1034. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(count);
  1035. pAcChargingInfo->RemoteStartFlag = NO;
  1036. if (pAcChargingInfo->Type == _Type_AC) {
  1037. pAcChargingInfo->SelfTest_Comp = NO;
  1038. strcpy((char *)ShmOCPP16Data->StatusNotification[count + pSysConfig->TotalConnectorCount].ErrorCode, "NoError");
  1039. }
  1040. }
  1041. return PASS;
  1042. }
  1043. bool InitialSystemDefaultConfig()
  1044. {
  1045. bool result = true;
  1046. LoadSysConfigAndInfo();
  1047. InitGPIO();
  1048. InitEthernet();
  1049. GetMacAddress();
  1050. return result;
  1051. }
  1052. //顯示自檢錯誤原因,觸發對應的flag
  1053. bool DisplaySelfTestFailReason()
  1054. {
  1055. bool result = false;
  1056. uint8_t index = 0;
  1057. // RB、FB、407、EV 小板中有些板子無回應
  1058. if (ShmRelayModuleData->SelfTest_Comp == NO) {
  1059. pAlarmCode->AlarmEvents.bits.RelayboardStestFail = true;
  1060. }
  1061. if (ShmFanModuleData->SelfTest_Comp == NO) {
  1062. pAlarmCode->AlarmEvents.bits.FanboardStestFail = true;
  1063. }
  1064. if (ShmPrimaryMcuData->SelfTest_Comp == NO) {
  1065. pAlarmCode->AlarmEvents.bits.PrimaryStestFail = true;
  1066. }
  1067. if (ShmLedModuleData->SelfTest_Comp == NO) {
  1068. pAlarmCode->AlarmEvents.bits.LedboardStestFail = true;
  1069. }
  1070. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  1071. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1072. if (pDcChargingInfo->Type == _Type_Chademo) {
  1073. if (ShmCHAdeMOData->evse[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1074. pAlarmCode->AlarmEvents.bits.ChademoboardStestFail = true;
  1075. }
  1076. } else if (pDcChargingInfo->Type == _Type_GB) {
  1077. if (ShmGBTData->evse[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1078. pAlarmCode->AlarmEvents.bits.GbtboardStestFail = true;
  1079. }
  1080. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1081. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1082. if (ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1083. pAlarmCode->AlarmEvents.bits.CCSboardStestFail = true;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. for (index = 0; index < pSysConfig->AcConnectorCount; index++) {
  1089. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(index);
  1090. // 先借 GBT 顯示
  1091. if (pAcChargingInfo->SelfTest_Comp == NO) {
  1092. pAlarmCode->AlarmEvents.bits.AcConnectorStestFail = true;
  1093. }
  1094. }
  1095. if (pSysInfo->AcContactorStatus == NO) {
  1096. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox &&!defined DD360UCar
  1097. // AC Contact 未搭上
  1098. pAlarmCode->AlarmEvents.bits.AcContactStestFail = true;
  1099. result = true;
  1100. #endif // !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  1101. }
  1102. //else if (pAlarmCode->AlarmEvents.bits.PsuDipSwitchStestFail == YES) { //DS60-120 add
  1103. // result = true;
  1104. //}
  1105. else if (ShmPsuData->SystemAvailablePower <= 0 &&
  1106. ShmPsuData->SystemAvailableCurrent <= 0) {
  1107. // PSU 通訊問題
  1108. pAlarmCode->AlarmEvents.bits.PsuModuleStestFail = true;
  1109. result = true;
  1110. }
  1111. return result;
  1112. }
  1113. //===============================================
  1114. // Common Detect Chk - Stop Charging ?
  1115. //===============================================
  1116. int isEvBoardStopChargeFlag(uint8_t gunIndex)
  1117. {
  1118. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1119. //printf("StopChargeFlag = %d", chargingInfo[gunIndex]->StopChargeFlag);
  1120. return pDcChargingInfo->StopChargeFlag;
  1121. }
  1122. bool isEvBoardNormalStopChargeFlag(uint8_t gunIndex)
  1123. {
  1124. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1125. return pDcChargingInfo->NormalStopChargeFlag;
  1126. }
  1127. //===============================================
  1128. // 掃描插槍狀況
  1129. //===============================================
  1130. void ClearDetectPluginFlag(int gunIndex)
  1131. {
  1132. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1133. if (pDcChargingInfo->RemoteStartFlag == YES) {
  1134. pDcChargingInfo->RemoteStartFlag = NO;
  1135. }
  1136. ShmDcCommonData->pGunInfo[gunIndex].WaitForPlugit = NO;
  1137. if (pSysInfo->OrderCharging != NO_DEFINE) {
  1138. pSysInfo->OrderCharging = NO_DEFINE;
  1139. }
  1140. }
  1141. void DetectPluginStart(int gunIndex)
  1142. {
  1143. //pSysInfo->WaitForPlugit = YES;
  1144. ShmDcCommonData->pGunInfo[gunIndex].WaitForPlugit = YES;
  1145. }
  1146. bool isDetectPlugin(int gunIndex)
  1147. {
  1148. if (ShmDcCommonData->pGunInfo[gunIndex].WaitForPlugit == YES) {
  1149. return true;
  1150. }
  1151. /*
  1152. if (pSysInfo->WaitForPlugit == YES) {
  1153. return true;
  1154. }
  1155. */
  1156. return false;
  1157. }
  1158. //===============================================
  1159. // Common Detect Chk - Chademo
  1160. //===============================================
  1161. bool isEvGunLocked_chademo(uint8_t gunIndex)
  1162. {
  1163. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1164. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1165. }
  1166. bool isEvContactorWelding_chademo(uint8_t gunIndex)
  1167. {
  1168. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1169. return DetectBitValue(ShmCHAdeMOData->ev[pDcChargingInfo->type_index].EvDetection, 3);
  1170. }
  1171. bool isEvStopReq_chademo(uint8_t gunIndex)
  1172. {
  1173. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1174. return DetectBitValue(ShmCHAdeMOData->ev[pDcChargingInfo->type_index].EvDetection, 4);
  1175. }
  1176. bool isEvStopCharging_chademo(uint8_t gunIndex)
  1177. {
  1178. if (isEvGunLocked_chademo(gunIndex) == NO) {
  1179. // 無鎖槍 = 停止
  1180. //log_info("gun locked none (%d) ", gunIndex);
  1181. return YES;
  1182. }
  1183. return NO;
  1184. }
  1185. uint8_t isPrechargeStatus_chademo(uint8_t gunIndex)
  1186. {
  1187. uint8_t result = 0x00;
  1188. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1189. result = ShmCHAdeMOData->ev[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1190. return result;
  1191. }
  1192. //===============================================
  1193. // Common Detect Chk - GB
  1194. //===============================================
  1195. bool isEvGunLocked_gb(uint8_t gunIndex)
  1196. {
  1197. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1198. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1199. }
  1200. bool isEvStopCharging_gb(uint8_t gunIndex)
  1201. {
  1202. if (isEvGunLocked_gb(gunIndex) == NO) {
  1203. // 無鎖槍 = 停止
  1204. //log_info("gun locked none. ");
  1205. return YES;
  1206. }
  1207. return NO;
  1208. }
  1209. uint8_t isPrechargeStatus_gb(uint8_t gunIndex)
  1210. {
  1211. uint8_t result = 0x00;
  1212. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1213. result = ShmGBTData->ev[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1214. return result;
  1215. }
  1216. //===============================================
  1217. // Common Detect Chk - CCS
  1218. //===============================================
  1219. bool isEvGunLocked_ccs(uint8_t gunIndex)
  1220. {
  1221. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1222. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1223. }
  1224. uint8_t isPrechargeStatus_ccs(uint8_t gunIndex)
  1225. {
  1226. //uint8_t result = 0x00;
  1227. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1228. return ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1229. }
  1230. bool isEvStopCharging_ccs(uint8_t gunIndex)
  1231. {
  1232. if (isEvGunLocked_ccs(gunIndex) == NO) {
  1233. // 無鎖槍 = 停止
  1234. //log_info("gun locked none. ");
  1235. return YES;
  1236. }
  1237. return NO;
  1238. }
  1239. //===============================================
  1240. // Callback
  1241. //===============================================
  1242. void DisplayChargingInfo()
  1243. {
  1244. uint8_t i = 0;
  1245. log_info("*********** DisplayChargingInfo *********** ");
  1246. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1247. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1248. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  1249. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  1250. ChangeGunSelectByIndex(i);
  1251. return;
  1252. }
  1253. }
  1254. if (pSysConfig->AcConnectorCount > 0 &&
  1255. pSysInfo->CurGunSelectedByAc == NO_DEFINE)
  1256. {
  1257. pAcChargingInfo = (struct ChargingInfoData*)GetAcChargingInfoData(0);
  1258. if (pAcChargingInfo->SystemStatus >= S_PREPARNING &&
  1259. pAcChargingInfo->SystemStatus <= S_COMPLETE)
  1260. {
  1261. pSysInfo->CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1262. }
  1263. }
  1264. usleep(50000);
  1265. systemPageRestoreInit();
  1266. }
  1267. void _AutoReturnTimeout(int gunIndex)
  1268. {
  1269. log_info("*********** _AutoReturnTimeout(%d) *********** ", pSysInfo->PageIndex);
  1270. if (pSysInfo->PageIndex == _PAGE_PRECHARGE) {
  1271. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1272. pDcChargingInfo->SystemStatus = S_ALARM;
  1273. //pSysInfo->SystemPage = _LCM_ERROR;
  1274. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  1275. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  1276. ShmDcCommonData->PreAuth_Result = 0;
  1277. //pSysInfo->SystemPage = _PAGE_PAYING;
  1278. //StartGunInfoTimeoutDet(pSysInfo->CurGunSelected,Timeout_FinalCost);
  1279. ClearDetectPluginFlag(gunIndex);
  1280. }
  1281. }
  1282. void _SelfTestTimeout(void)
  1283. {
  1284. if (pSysInfo->BootingStatus != BOOT_COMPLETE) {
  1285. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  1286. setChargerMode(gunIndex, MODE_ALARM);
  1287. }
  1288. }
  1289. ShmPsuData->Work_Step = _NO_WORKING;
  1290. pSysInfo->SelfTestSeq = _STEST_FAIL;
  1291. log_info("Self test timeout. ");
  1292. }
  1293. void _AuthorizedTimeout(void)
  1294. {
  1295. int i;
  1296. //if (IsAuthorizingMode()) {
  1297. log_info("*********** _AuthorizedTimeout *********** ");
  1298. StopSystemTimeoutDet();
  1299. //isCardScan = false;
  1300. SetIsCardScan(false);
  1301. //StopSystemTimeoutDet();
  1302. StopGunInfoTimeoutDet(pSysInfo->CurGunSelected);
  1303. ShmDcCommonData->TradeCancel = TRUE;
  1304. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  1305. ShmDcCommonData->PreAuth_Result = 0;
  1306. pSysInfo->SystemPage = _PAGE_SENSING;
  1307. StartSystemTimeoutDet(Timeout_TradeCancel);
  1308. for (i = 0; i <= 30; i++) {
  1309. if (ShmDcCommonData->PreAuth_Config == _CREDITCARD_IDLE)
  1310. break;
  1311. sleep(1);
  1312. }
  1313. ShmDcCommonData->PreAuth_Result = 0;
  1314. StopSystemTimeoutDet();
  1315. ClearDetectPluginFlag(pSysInfo->CurGunSelected);
  1316. strcpy((char*)pSysConfig->UserId, "");
  1317. ClearAuthorizedFlag();
  1318. setChargerMode(pSysInfo->CurGunSelected, S_IDLE);
  1319. // StartSystemTimeoutDet(Timeout_ReturnViewPage);
  1320. //}
  1321. }
  1322. void _DetectPlugInTimeout(uint8_t gunIndex)
  1323. {
  1324. int i;
  1325. log_info("*********** Gun%d _DetectPlugInTimeout *********** ",gunIndex);
  1326. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  1327. //StopSystemTimeoutDet();
  1328. StopGunInfoTimeoutDet(gunIndex);
  1329. ShmDcCommonData->TradeCancel = TRUE;
  1330. if (pDcChargingInfo->RemoteStartFlag || pDcChargingInfo->isRemoteStart) {
  1331. StopSystemTimeoutDet();
  1332. ClearDetectPluginFlag(gunIndex);
  1333. strcpy((char*)pSysConfig->UserId, "");
  1334. setChargerMode(gunIndex, S_IDLE);
  1335. return;
  1336. }
  1337. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  1338. ShmDcCommonData->PreAuth_Result = 0;
  1339. pSysInfo->SystemPage = _PAGE_SENSING;
  1340. StartSystemTimeoutDet(Timeout_TradeCancel);
  1341. for(i=0;i<=30;i++) {
  1342. if(ShmDcCommonData->PreAuth_Config == _CREDITCARD_IDLE)
  1343. break;
  1344. sleep(1);
  1345. }
  1346. ShmDcCommonData->PreAuth_Result = 0;
  1347. if (pSysInfo->CurGunSelected == gunIndex) {
  1348. systemPageRestoreInit();
  1349. }
  1350. StopSystemTimeoutDet();
  1351. ClearDetectPluginFlag(gunIndex);
  1352. strcpy((char *)pSysConfig->UserId, "");
  1353. setChargerMode(gunIndex, S_IDLE);
  1354. //systemPageRestoreInit();
  1355. }
  1356. void _DetectEvChargingEnableTimeout(uint8_t gunIndex)
  1357. {
  1358. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1359. if (pDcChargingInfo->Type == _Type_Chademo) {
  1360. if (!isEvGunLocked_chademo(gunIndex)) {
  1361. log_info("*********** _DetectEvChargingEnableTimeout (chademo) ***********");
  1362. }
  1363. } else if (pDcChargingInfo->Type == _Type_GB) {
  1364. if (!isEvGunLocked_ccs(gunIndex)) {
  1365. log_info("*********** _DetectEvChargingEnableTimeout (gb) ***********");
  1366. }
  1367. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1368. if (!isEvGunLocked_ccs(gunIndex)) {
  1369. log_info("*********** _DetectEvChargingEnableTimeout (ccs) ***********");
  1370. }
  1371. }
  1372. //pSysInfo->SystemPage = _LCM_ERROR;
  1373. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  1374. ChargingTerminalProcess(gunIndex);
  1375. _AutoReturnTimeout(gunIndex);
  1376. }
  1377. void _DetectEvseChargingEnableTimeout(uint8_t gunIndex)
  1378. {
  1379. log_info("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********");
  1380. ChargingTerminalProcess(gunIndex);
  1381. //pSysInfo->SystemPage = _LCM_ERROR;
  1382. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  1383. _AutoReturnTimeout(gunIndex);
  1384. }
  1385. void _PrepareTimeout(uint8_t gunIndex)
  1386. {
  1387. log_info("*********** _PrepareTimeout ***********");
  1388. ChargingTerminalProcess(gunIndex);
  1389. pAlarmCode->AlarmEvents.bits.PsuNoResource = YES;
  1390. //pSysInfo->SystemPage = _LCM_ERROR;
  1391. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  1392. _AutoReturnTimeout(gunIndex);
  1393. }
  1394. void _CcsPrechargeTimeout(uint8_t gunIndex)
  1395. {
  1396. log_info("*********** _CcsPrechargeTimeout ***********");
  1397. ChargingTerminalProcess(gunIndex);
  1398. //pSysInfo->SystemPage = _LCM_ERROR;
  1399. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  1400. }
  1401. void _LinkErrorTimeout(uint8_t gunIndex)
  1402. {
  1403. log_info("*********** _LinkErrorTimeout ***********");
  1404. setChargerMode(pSysInfo->CurGunSelected, S_IDLE);
  1405. systemPageRestoreInit();
  1406. }
  1407. //===============================================
  1408. // 取得卡號與卡號驗證
  1409. //===============================================
  1410. void AuthorizingStart(void)
  1411. {
  1412. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1413. pSysInfo->AuthorizeFlag = YES;
  1414. }
  1415. void ClearAuthorizedFlag(void)
  1416. {
  1417. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1418. pSysInfo->AuthorizeFlag = NO;
  1419. }
  1420. bool isAuthorizedComplete(void)
  1421. {
  1422. if (pSysInfo->AuthorizeFlag == YES) {
  1423. return false;
  1424. }
  1425. return true;
  1426. }
  1427. bool IsAuthorizingMode()
  1428. {
  1429. if (pSysInfo->AuthorizeFlag == NO) {
  1430. return false;
  1431. }
  1432. return true;
  1433. }
  1434. //===============================================
  1435. // 紀錄 Alarm Code
  1436. //===============================================
  1437. void ResetChargerAlarmCode(uint8_t gunIndex, char *code)
  1438. {
  1439. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1440. if (strcmp(code, "012234") == EQUAL) {
  1441. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  1442. } else if (strcmp(code, "012235") == EQUAL) {
  1443. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  1444. } else if (strcmp(code, "012236") == EQUAL) {
  1445. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  1446. } else if (strcmp(code, "012288") == EQUAL) {
  1447. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  1448. } else if (strcmp(code, "012289") == EQUAL) {
  1449. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  1450. } else if (strcmp(code, "012290") == EQUAL) {
  1451. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  1452. } else if (strcmp(code, "012229") == EQUAL) {
  1453. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  1454. } else if (strcmp(code, "012230") == EQUAL) {
  1455. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  1456. } else if (strcmp(code, "012231") == EQUAL) {
  1457. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  1458. } else if (strcmp(code, "011011") == EQUAL) {
  1459. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  1460. } else if (strcmp(code, "011013") == EQUAL) {
  1461. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  1462. } else if (strcmp(code, "011015") == EQUAL) {
  1463. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  1464. } else if (strcmp(code, "011012") == EQUAL) {
  1465. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  1466. } else if (strcmp(code, "011014") == EQUAL) {
  1467. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  1468. } else if (strcmp(code, "011016") == EQUAL) {
  1469. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  1470. } else if (strcmp(code, "011018") == EQUAL) {
  1471. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  1472. } else if (strcmp(code, "011019") == EQUAL) {
  1473. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  1474. } else if (strcmp(code, "011020") == EQUAL) {
  1475. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  1476. } else if (strcmp(code, "012323") == EQUAL) {
  1477. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  1478. } else if (strcmp(code, "011038") == EQUAL) {
  1479. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  1480. }
  1481. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012229", 6) == EQUAL ||
  1482. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012230", 6) == EQUAL ||
  1483. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012231", 6) == EQUAL ||
  1484. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011011", 6) == EQUAL ||
  1485. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011013", 6) == EQUAL ||
  1486. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011015", 6) == EQUAL ||
  1487. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011012", 6) == EQUAL ||
  1488. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011014", 6) == EQUAL ||
  1489. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011016", 6) == EQUAL ||
  1490. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011018", 6) == EQUAL ||
  1491. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011019", 6) == EQUAL ||
  1492. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011020", 6) == EQUAL ||
  1493. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012323", 6) == EQUAL ||
  1494. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011038", 6) == EQUAL) {
  1495. strncpy((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6);
  1496. }
  1497. }
  1498. void RecordAlarmCode(uint8_t gunIndex, char *code)
  1499. {
  1500. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1501. if((pDcChargingInfo->SystemStatus > S_IDLE && pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1502. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 && pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1503. {
  1504. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1505. memcpy(pDcChargingInfo->ConnectorAlarmCode, code, 6);
  1506. }
  1507. if (pDcChargingInfo->StopChargeFlag == NO)
  1508. {
  1509. log_info("RecordAlarmCode set Stop Charge Flag");
  1510. pDcChargingInfo->StopChargeFlag = YES;
  1511. }
  1512. }
  1513. }
  1514. void ReleaseAlarmCode(uint8_t gunIndex)
  1515. {
  1516. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1517. //if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1518. // return;
  1519. //}
  1520. switch (pDcChargingInfo->Type) {
  1521. case _Type_Chademo:
  1522. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  1523. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  1524. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP = NO;
  1525. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  1526. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  1527. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  1528. break;
  1529. case _Type_CCS_2:
  1530. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  1531. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  1532. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP = NO;
  1533. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  1534. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  1535. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  1536. break;
  1537. case _Type_GB:
  1538. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  1539. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  1540. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP = NO;
  1541. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  1542. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  1543. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  1544. break;
  1545. }
  1546. memcpy(pDcChargingInfo->ConnectorAlarmCode, "", 6);
  1547. }
  1548. //===============================================
  1549. // EmergencyStop and Charging Stop
  1550. //===============================================
  1551. void ChargingTerminalProcess(uint8_t gunIndex)
  1552. {
  1553. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  1554. //if (pDcChargingInfo->SystemStatus == S_CHARGING)
  1555. //ShmDcCommonData->StopCharge[gunIndex] = TRUE;
  1556. setChargerMode(gunIndex, MODE_TERMINATING);
  1557. }
  1558. void ChargingAlarmProcess(uint8_t gunIndex)
  1559. {
  1560. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  1561. //if (pDcChargingInfo->SystemStatus == S_CHARGING)
  1562. //ShmDcCommonData->StopCharge[gunIndex] = TRUE;
  1563. UpdateErrorCodeToOcpp(gunIndex);
  1564. setChargerMode(gunIndex, MODE_ALARM);
  1565. }
  1566. void AcChargingTerminalProcess(void)
  1567. {
  1568. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  1569. pAcChargingInfo->SystemStatus = MODE_TERMINATING;
  1570. }
  1571. void StopChargingProcessByString(uint8_t level)
  1572. {
  1573. if (level > pSysWarning->Level) {
  1574. pSysWarning->Level = level;
  1575. }
  1576. }
  1577. void ReleaseChargingProcessByString(uint8_t level)
  1578. {
  1579. if (level >= pSysWarning->Level) {
  1580. pSysWarning->Level = WARN_LV_NL;
  1581. }
  1582. }
  1583. // 一般錯誤停止充電處理函式
  1584. void BoardErrOccurByString(uint8_t index, char *code)
  1585. {
  1586. uint8_t level = 1;
  1587. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1588. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1589. pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1590. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1591. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  1592. if (strncmp(code, "023730", 6) == EQUAL &&
  1593. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop == NO
  1594. ) {
  1595. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  1596. }
  1597. ChargingTerminalProcess(index);
  1598. }
  1599. StopChargingProcessByString(level);
  1600. }
  1601. void ReleaseBoardErrOccurByString(uint8_t index, char *code)
  1602. {
  1603. bool isTrigger = false;
  1604. uint8_t level = 1;
  1605. if (strncmp(code, "023730", 6) == 0 &&
  1606. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop == YES) {
  1607. isTrigger = true;
  1608. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  1609. }
  1610. if (isTrigger) {
  1611. ReleaseChargingProcessByString(level);
  1612. }
  1613. }
  1614. // 急停狀況的停止充電處理函式
  1615. void EmcOccureByString(char *code)
  1616. {
  1617. uint8_t level = 2;
  1618. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  1619. // 其錯誤等級為 2
  1620. //DS60-120 remove
  1621. if (strncmp(code, "012251", 6) == EQUAL ||
  1622. strncmp(code, "012252", 6) == EQUAL ||
  1623. strncmp(code, "012238", 6) == EQUAL ||
  1624. strncmp(code, "042251", 6) == EQUAL ||
  1625. strncmp(code, "042252", 6) == EQUAL ||
  1626. strncmp(code, "012304", 6) == EQUAL ||
  1627. strncmp(code, "042327", 6) == EQUAL ||
  1628. strncmp(code, "042328", 6) == EQUAL ||
  1629. strncmp(code, "042200", 6) == EQUAL ||
  1630. strncmp(code, "042201", 6) == EQUAL ||
  1631. strncmp(code, "042202", 6) == EQUAL ||
  1632. strncmp(code, "042267", 6) == EQUAL) {
  1633. for (uint8_t gun = 0; gun < pSysConfig->TotalConnectorCount; gun++) {
  1634. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gun);
  1635. //strncpy((char *)ShmOCPP16Data->StatusNotification[gun].VendorErrorCode, code, 6);
  1636. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1637. pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1638. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1639. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  1640. //ChargingTerminalProcess(gun);
  1641. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1642. memcpy(pDcChargingInfo->ConnectorAlarmCode, code, 6);
  1643. }
  1644. ChargingAlarmProcess(gun);
  1645. }
  1646. }
  1647. StopChargingProcessByString(level);
  1648. InformOcppErrOccur(4);
  1649. }
  1650. }
  1651. void ReleaseEmsOccureByString(uint8_t index, char *code)
  1652. {
  1653. bool isTrigger = false;
  1654. uint8_t level = 2;
  1655. if (strncmp(code, "042251", 6) == 0 ) {
  1656. isTrigger = true;
  1657. } else if (strncmp(code, "012251", 6) == 0 &&
  1658. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES) {
  1659. isTrigger = true;
  1660. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip = NO;
  1661. } else if (strncmp(code, "012252", 6) == 0 &&
  1662. pAlarmCode->AlarmEvents.bits.DoorOpen == YES) {
  1663. isTrigger = true;
  1664. pAlarmCode->AlarmEvents.bits.DoorOpen = NO;
  1665. } else if (strncmp(code, "012237", 6) == 0 &&
  1666. pAlarmCode->AlarmEvents.bits.SpdTrip == YES) {
  1667. isTrigger = true;
  1668. pAlarmCode->AlarmEvents.bits.SpdTrip = NO;
  1669. } else if (strncmp(code, "012238", 6) == 0 &&
  1670. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  1671. isTrigger = true;
  1672. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip = NO;
  1673. }
  1674. if (isTrigger) {
  1675. ReleaseChargingProcessByString(level);
  1676. InformOcppErrOccur(6);
  1677. }
  1678. }
  1679. static void checkOvpState(uint8_t gunIndex, uint8_t gunType)
  1680. {
  1681. switch(gunType) {
  1682. case _Type_Chademo:
  1683. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP == YES)
  1684. RecordAlarmCode(gunIndex, "012217");
  1685. break;
  1686. case _Type_CCS_2:
  1687. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP == YES)
  1688. RecordAlarmCode(gunIndex, "012219");
  1689. break;
  1690. case _Type_GB:
  1691. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP == YES)
  1692. RecordAlarmCode(gunIndex, "012221");
  1693. break;
  1694. }
  1695. }
  1696. //===============================================
  1697. // 確認各小板偵測的錯誤狀況
  1698. //===============================================
  1699. void CheckErrorOccurStatus(uint8_t index)
  1700. {
  1701. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1702. // RB
  1703. if (pSysConfig->PhaseLossPolicy == YES) {
  1704. if (pAlarmCode->AlarmEvents.bits.SystemL1InputUVP == YES ||
  1705. pAlarmCode->AlarmEvents.bits.SystemL2InputUVP == YES ||
  1706. pAlarmCode->AlarmEvents.bits.SystemL3InputUVP == YES) {
  1707. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  1708. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  1709. StopChargingProcessByString(2);
  1710. InformOcppErrOccur(13);
  1711. }
  1712. //DS60-120 add -----
  1713. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1714. if (pAlarmCode->AlarmEvents.bits.SystemL1InputUVP == YES) {
  1715. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012203", 6);
  1716. } else if (pAlarmCode->AlarmEvents.bits.SystemL2InputUVP == YES) {
  1717. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012204", 6);
  1718. } else if (pAlarmCode->AlarmEvents.bits.SystemL3InputUVP == YES) {
  1719. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012205", 6);
  1720. }
  1721. }
  1722. log_info("1 CheckErrorOccurStatus");
  1723. pDcChargingInfo->StopChargeFlag = YES;
  1724. //------------------------------------------------------------------
  1725. } else {
  1726. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  1727. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1728. ReleaseChargingProcessByString(2);
  1729. InformOcppErrOccur(6);
  1730. }
  1731. }
  1732. } else {
  1733. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  1734. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1735. ReleaseChargingProcessByString(2);
  1736. InformOcppErrOccur(6);
  1737. }
  1738. }
  1739. if (pAlarmCode->AlarmEvents.bits.SystemL1InputOVP == YES ||
  1740. pAlarmCode->AlarmEvents.bits.SystemL2InputOVP == YES ||
  1741. pAlarmCode->AlarmEvents.bits.SystemL3InputOVP == YES) {
  1742. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  1743. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  1744. StopChargingProcessByString(2);
  1745. InformOcppErrOccur(14);
  1746. }
  1747. //DS60-120 -----
  1748. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1749. if (pAlarmCode->AlarmEvents.bits.SystemL1InputOVP == YES) {
  1750. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012200", 6);
  1751. } else if (pAlarmCode->AlarmEvents.bits.SystemL2InputOVP == YES) {
  1752. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012201", 6);
  1753. } else if (pAlarmCode->AlarmEvents.bits.SystemL3InputOVP == YES) {
  1754. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012202", 6);
  1755. }
  1756. }
  1757. log_info("2 CheckErrorOccurStatus");
  1758. pDcChargingInfo->StopChargeFlag = YES;
  1759. //----------------------------------------------------------------------
  1760. } else {
  1761. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP) {
  1762. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1763. ReleaseChargingProcessByString(2);
  1764. InformOcppErrOccur(6);
  1765. }
  1766. }
  1767. checkOvpState(index, pDcChargingInfo->Type);
  1768. //--------------------------------------------------------------------------
  1769. if (strlen((char *)pDcChargingInfo->ConnectorAlarmCode) == 0) {
  1770. //Primary
  1771. if (pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES) {
  1772. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012251", 6);
  1773. } else if (pAlarmCode->AlarmEvents.bits.DoorOpen == YES) {
  1774. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012252", 6);
  1775. } else if (pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  1776. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012238", 6);
  1777. } else if (pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES) {
  1778. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012304", 6);
  1779. }
  1780. //Chiller temperature
  1781. //else if (pAlarmCode->AlarmEvents.bits.SystemChillerOTP == YES) {
  1782. // memcpy(pDcChargingInfo->ConnectorAlarmCode, "012323", 6);
  1783. //} else if (pFaultCode->FaultEvents.bits.ChillerTempSensorBroken == YES) {
  1784. // memcpy(pDcChargingInfo->ConnectorAlarmCode, "011038", 6);
  1785. //}
  1786. }
  1787. /*
  1788. if (pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES ||
  1789. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES ||
  1790. pAlarmCode->AlarmEvents.bits.DoorOpen == YES ||
  1791. pSysWarning->ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  1792. pAlarmCode->AlarmEvents.bits.PsuFailureAlarm == YES ||
  1793. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES ||
  1794. //Power cabinet alarm status
  1795. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop == YES ||
  1796. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen == YES ||
  1797. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP == YES ||
  1798. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputUVP == YES ||
  1799. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP == YES ||
  1800. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP == YES ||
  1801. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP == YES ||
  1802. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure == YES
  1803. ) {
  1804. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1805. pDcChargingInfo->SystemStatus < S_COMPLETE) ||
  1806. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1807. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1 ))
  1808. ChargingAlarmProcess(index);
  1809. pSysWarning->Level = WARN_LV_ER;
  1810. }*/
  1811. }
  1812. //===============================================
  1813. // 確認 GPIO 狀態
  1814. //===============================================
  1815. void gpio_set_value(unsigned int gpio, unsigned int value)
  1816. {
  1817. int fd;
  1818. char buf[MAX_BUF];
  1819. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1820. fd = open(buf, O_WRONLY);
  1821. if (fd < 0) {
  1822. perror("gpio/set-value");
  1823. return;
  1824. }
  1825. if (value) {
  1826. write(fd, "1", 2);
  1827. } else {
  1828. write(fd, "0", 2);
  1829. }
  1830. close(fd);
  1831. }
  1832. int gpio_get_value(unsigned int gpio, unsigned int *value)
  1833. {
  1834. int fd;
  1835. char buf[MAX_BUF];
  1836. char ch;
  1837. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1838. fd = open(buf, O_RDONLY);
  1839. if (fd < 0) {
  1840. perror("gpio/get-value");
  1841. return fd;
  1842. }
  1843. read(fd, &ch, 1);
  1844. if (ch != '0') {
  1845. *value = 1;
  1846. } else {
  1847. *value = 0;
  1848. }
  1849. close(fd);
  1850. return 0;
  1851. }
  1852. void CheckGunTypeFromHw()
  1853. {
  1854. uint8_t i = 0;
  1855. int pinIn[4] = {22, 23, 44, 45};
  1856. unsigned int gpioValue = 0;
  1857. uint8_t tmp[2] = {0};
  1858. log_info("ModelName = %s", pSysConfig->ModelName);
  1859. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1860. gpio_get_value(pinIn[i], &gpioValue);
  1861. switch (pinIn[i]) {
  1862. //right slot
  1863. case 22:
  1864. bd1_1_status = gpioValue;
  1865. break;
  1866. case 23:
  1867. bd1_2_status = gpioValue;
  1868. break;
  1869. //left slot
  1870. case 44:
  1871. bd0_1_status = gpioValue;
  1872. break;
  1873. case 45:
  1874. bd0_2_status = gpioValue;
  1875. break;
  1876. }
  1877. }
  1878. //BD1(Left-CCS-CND1-SMR2-左槍), BD2(Right-CHADEMO-CND2-SMR1-右槍), CCS: 10 , CHAdeMO: 01 , GBT: 11
  1879. //CcsChargingData [0至1] 分別為 Right至Left
  1880. //model name 槍順序左至右分別為Right至Left
  1881. tmp[1] = (bd0_1_status << 4 | bd0_2_status);
  1882. tmp[0] = (bd1_1_status << 4 | bd1_2_status);
  1883. for (i = 0; i < 2; i++) {
  1884. switch (tmp[i]) {
  1885. case 0x01:
  1886. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "CHAdeMO");
  1887. break;
  1888. case 0x10:
  1889. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "CCS");
  1890. break;
  1891. case 0x11:
  1892. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "GBT");
  1893. break;
  1894. case 0x00:
  1895. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "None");
  1896. break;
  1897. }
  1898. }
  1899. }
  1900. void CheckGpioInStatus()
  1901. {
  1902. int i = 0;
  1903. int pinIn[2] = { 27, 47 };//{IO BD1_2, IO BD2_2}
  1904. unsigned int gpioValue = 0;
  1905. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1906. gpio_get_value(pinIn[i], &gpioValue);
  1907. if (gpioValue == 0x01) {
  1908. switch (pinIn[i]) {
  1909. // 小板緊急停止
  1910. case 47:
  1911. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1912. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1913. if (pDcChargingInfo->slotsIndex == 1) {
  1914. if (pDcChargingInfo->Type == _Type_Chademo) {
  1915. BoardErrOccurByString(i, "023730");
  1916. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1917. BoardErrOccurByString(i, "013627");
  1918. }
  1919. break;
  1920. }
  1921. }
  1922. break;
  1923. case 27:
  1924. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1925. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1926. if (pDcChargingInfo->slotsIndex == 3) {
  1927. if (pDcChargingInfo->Type == _Type_Chademo) {
  1928. BoardErrOccurByString(i, "023730");
  1929. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1930. BoardErrOccurByString(i, "013627");
  1931. }
  1932. break;
  1933. }
  1934. }
  1935. break;
  1936. }
  1937. } else {
  1938. switch (pinIn[i]) {
  1939. // 小板解除緊急停止
  1940. case 47:
  1941. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1942. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1943. if (pDcChargingInfo->slotsIndex == 1) {
  1944. if (pDcChargingInfo->Type == _Type_Chademo) {
  1945. ReleaseBoardErrOccurByString(i, "023730");
  1946. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1947. ReleaseBoardErrOccurByString(i, "013627");
  1948. }
  1949. break;
  1950. }
  1951. }
  1952. break;
  1953. case 27:
  1954. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1955. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1956. if (pDcChargingInfo->slotsIndex == 3) {
  1957. if (pDcChargingInfo->Type == _Type_Chademo) {
  1958. ReleaseBoardErrOccurByString(i, "023730");
  1959. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1960. ReleaseBoardErrOccurByString(i, "013627");
  1961. }
  1962. break;
  1963. }
  1964. }
  1965. break;
  1966. }
  1967. }
  1968. }
  1969. }
  1970. //===============================================
  1971. // Main process
  1972. //===============================================
  1973. // 檢查 uint8_t 中某個 Bit 的值
  1974. // _byte : 欲改變的 byte
  1975. // _bit : 該 byte 的第幾個 bit
  1976. uint8_t DetectBitValue(uint8_t _byte, uint8_t _bit)
  1977. {
  1978. uint8_t mask_table[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
  1979. return ( _byte & mask_table[_bit] ) != 0x00;
  1980. }
  1981. // 設定 Byte 中某個 Bit的值
  1982. // _byte : 欲改變的 byte
  1983. // _bit : 該 byte 的第幾個 bit
  1984. // value : 修改的值為 0 or 1
  1985. void SetBitValue(uint8_t *_byte, uint8_t _bit, uint8_t value)
  1986. {
  1987. if (value == 1) {
  1988. *_byte |= (1 << _bit);
  1989. } else if (value == 0) {
  1990. *_byte ^= (1 << _bit);
  1991. }
  1992. }
  1993. uint8_t isModeChange(uint8_t gunIndex)
  1994. {
  1995. uint8_t result = NO;
  1996. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1997. if (pDcChargingInfo->SystemStatus == pDcChargingInfo->PreviousSystemStatus) {
  1998. return result;
  1999. }
  2000. pDcChargingInfo->PreviousSystemStatus = pDcChargingInfo->SystemStatus;
  2001. ShmDcCommonData->SystemModeChange[gunIndex] = YES; //for Module_EvRxComm print temperature message
  2002. return YES;
  2003. }
  2004. static int checkSlotGpioMappingGunType(uint8_t gunIndex, uint8_t gunType)
  2005. {
  2006. uint8_t slot1 = 0, slot2 = 0;
  2007. if (gunIndex == 0) {
  2008. slot1 = bd0_1_status;
  2009. slot2 = bd0_2_status;
  2010. } else if (gunIndex == 1) {
  2011. slot1 = bd1_1_status;
  2012. slot2 = bd1_2_status;
  2013. }
  2014. log_info("slot %d gpio1 = %d, gpio2 = %d, type = %d",
  2015. gunIndex,
  2016. slot1,
  2017. slot2,
  2018. gunType);
  2019. switch (gunType) {
  2020. case _Type_Chademo:
  2021. if (slot1 != NO && slot2 != YES) {
  2022. return FAIL;
  2023. }
  2024. break;
  2025. case _Type_CCS_2:
  2026. if (slot1 != YES && slot2 != NO) {
  2027. return FAIL;
  2028. }
  2029. break;
  2030. case _Type_GB:
  2031. if (slot1 != YES && slot2 != YES) {
  2032. return FAIL;
  2033. }
  2034. break;
  2035. }
  2036. return PASS;
  2037. }
  2038. bool CheckConnectorTypeStatus(void)
  2039. {
  2040. bool result = true;
  2041. uint8_t gunIndex = 0;
  2042. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  2043. struct ChargingInfoData *pDcChargingInfo = NULL;
  2044. if (!MappingGunChargingInfo("CSU Task")) {
  2045. log_error("CheckConnectorTypeStatus MappingGunChargingInfo failed");
  2046. return false;
  2047. }
  2048. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  2049. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2050. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2051. if (checkSlotGpioMappingGunType(gunIndex, pDcChargingInfo->Type) == FAIL) {
  2052. return false;
  2053. }
  2054. pDcChargingInfo->SystemStatus = S_BOOTING;
  2055. switch (gunIndex) {
  2056. case 0:
  2057. if (pSysConfig->TotalConnectorCount == 1) {
  2058. if ((bd0_1_status == YES || bd0_2_status == YES) &&
  2059. (bd1_1_status == NO && bd1_2_status == NO)) {
  2060. pDcChargingInfo->Evboard_id = 0x01;
  2061. }
  2062. if ((bd0_1_status == NO && bd0_2_status == NO) &&
  2063. (bd1_1_status == YES || bd1_2_status == YES)
  2064. ) {
  2065. pDcChargingInfo->Evboard_id = 0x02;
  2066. }
  2067. break;
  2068. }
  2069. pDcChargingInfo->Evboard_id = 0x01;
  2070. break;
  2071. case 1:
  2072. pDcChargingInfo->Evboard_id = 0x02;
  2073. break;
  2074. }
  2075. CheckHwSlotStatusLog(gunIndex);
  2076. if (pDcChargingInfo->Evboard_id == 0x00) {
  2077. return false;
  2078. }
  2079. }
  2080. AdjustChargerCurrent();
  2081. return result;
  2082. }
  2083. int SpawnTask(void)
  2084. {
  2085. if(SIMULATION)
  2086. system("/root/simulation &");
  2087. sleep(2);
  2088. system("/root/Module_EventLogging &");
  2089. system("/root/Module_PrimaryComm &");
  2090. system("/root/Module_EvComm &");
  2091. system("/root/Module_LcmControl &");
  2092. system("/root/Module_InternalComm &");
  2093. system("/root/Module_ProduceUtils &");
  2094. system("/root/Module_UpdateFW &");
  2095. system("/root/Module_DoComm &");
  2096. return PASS;
  2097. }
  2098. void KillTask(void)
  2099. {
  2100. //ChangeLcmByIndex(_LCM_ERROR);
  2101. ChangeLcmByIndex(_PAGE_MAINTAIN);
  2102. system("killall Module_EventLogging");
  2103. system("killall Module_PrimaryComm");
  2104. system("killall Module_EvComm");
  2105. system("killall Module_LcmControl");
  2106. system("killall Module_InternalComm");
  2107. system("killall Module_UpdateFW");
  2108. system("killall Module_ChkSysTask");
  2109. //system("killall Module_DoComm");
  2110. return ;
  2111. }
  2112. void KillTaskExceptPrimary(void)
  2113. {
  2114. //ChangeLcmByIndex(_LCM_ERROR);
  2115. ChangeLcmByIndex(_PAGE_MAINTAIN);
  2116. system("killall Module_EvComm");
  2117. system("killall Module_InternalComm");
  2118. return;
  2119. }
  2120. void KillAllTask(void)
  2121. {
  2122. //ChangeLcmByIndex(_LCM_ERROR);
  2123. ChangeLcmByIndex(_PAGE_MAINTAIN);
  2124. system("killall Module_EventLogging");
  2125. system("killall Module_PrimaryComm");
  2126. system("killall Module_EvComm");
  2127. system("killall Module_LcmControl");
  2128. system("killall Module_InternalComm");
  2129. system("killall Module_UpdateFW");
  2130. system("killall Module_ChkSysTask");
  2131. system("killall Module_DoComm");
  2132. return ;
  2133. }
  2134. void StartSystemTimeoutDet(uint8_t flag)
  2135. {
  2136. if (pSysInfo->SystemTimeoutFlag != flag) {
  2137. //log_info("Set System Timeout Flag %d", flag);
  2138. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  2139. }
  2140. pSysInfo->SystemTimeoutFlag = flag;
  2141. }
  2142. void StopSystemTimeoutDet(void)
  2143. {
  2144. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  2145. pSysInfo->SystemTimeoutFlag = Timeout_None;
  2146. }
  2147. void StartGunInfoTimeoutDet(uint8_t gunIndex, uint8_t flag)
  2148. {
  2149. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2150. if (gunIndex < pSysConfig->TotalConnectorCount) {
  2151. if (pDcChargingInfo->TimeoutFlag != flag) {
  2152. GetClockTime(&pDcChargingInfo->ConnectorTimeout, NULL);
  2153. //log_info("Set Gun%d Timeout Flag %d", gunIndex, flag);
  2154. }
  2155. pDcChargingInfo->TimeoutFlag = flag;
  2156. }
  2157. }
  2158. void StopGunInfoTimeoutDet(uint8_t gunIndex)
  2159. {
  2160. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2161. if (gunIndex < pSysConfig->TotalConnectorCount) {
  2162. pDcChargingInfo->TimeoutFlag = Timeout_None;
  2163. }
  2164. }
  2165. void CheckConnectionTimeout(void)
  2166. {
  2167. if (ShmSelectGunInfo->RemoteSetup.ConnectionTimeout != 0) { //Jerry add
  2168. _connectionTimeout = ShmSelectGunInfo->RemoteSetup.ConnectionTimeout;
  2169. } else {
  2170. _connectionTimeout = CONN_PLUG_TIMEOUT;
  2171. }
  2172. return;
  2173. }
  2174. void _evccidlinktimeout(uint8_t gunIndex)
  2175. {
  2176. log_info("Getting EVCCID Timeout");
  2177. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2178. pDcChargingInfo->isEVCCIDVerify = true;
  2179. StopGunInfoTimeoutDet(gunIndex);
  2180. destroySelGun(pSysInfo->CurGunSelected);
  2181. systemPageRestoreInit();
  2182. }
  2183. void CreateTimeoutFork(void)
  2184. {
  2185. pid_t timeoutPid;
  2186. timeoutPid = fork();
  2187. if (timeoutPid == 0) {
  2188. GetClockTime(&_cmdSubPriority_time, NULL);
  2189. CheckConnectionTimeout();
  2190. //log_info("Timeout Fork Child's PID is %d", getpid());
  2191. while (1) {
  2192. if ((GetClockTimeoutValue(_cmdSubPriority_time) / 1000) > 5000) {
  2193. CheckConnectionTimeout();
  2194. GetClockTime(&_cmdSubPriority_time, NULL);
  2195. }
  2196. /*
  2197. if (pSysInfo->SystemTimeoutFlag != 0)
  2198. log_info("Timeout ***********SystemTimeoutFlag = %d(%d) ********",pSysInfo->SystemTimeoutFlag,
  2199. GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL);
  2200. */
  2201. // 系統
  2202. switch (pSysInfo->SystemTimeoutFlag) {
  2203. case Timeout_SelftestChk:
  2204. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT) {
  2205. _SelfTestTimeout();
  2206. StopSystemTimeoutDet();
  2207. destroySelGun(DESTROY_ALL_SEL); //jerry add
  2208. }
  2209. break;
  2210. case Timeout_ReturnViewPage:
  2211. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= RETURN_VIEWPAGE_TIMEOUT) {
  2212. StopSystemTimeoutDet();
  2213. systemPageRestoreInit();
  2214. }
  2215. break;
  2216. case Timeout_Authorizing:
  2217. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT) {
  2218. log_error("Authorizing Timeout");
  2219. _AuthorizedTimeout();
  2220. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2221. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2222. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2223. log_error("Author timeout restart DoComm");
  2224. system("killall Module_DoComm");
  2225. }
  2226. }
  2227. break;
  2228. case Timeout_WaitBalance:
  2229. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= BALANCE_TIMEOUT) {
  2230. log_error("Wait Balance timeout");
  2231. _AuthorizedTimeout();
  2232. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2233. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2234. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2235. log_error("Author timeout restart DoComm");
  2236. system("killall Module_DoComm");
  2237. }
  2238. }
  2239. break;
  2240. case Timeout_VerifyFail:
  2241. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT) {
  2242. log_info("Timeout_VerifyFail");
  2243. StopSystemTimeoutDet();
  2244. systemPageRestoreInit();
  2245. setChargerMode(pSysInfo->CurGunSelected, S_IDLE);
  2246. }
  2247. break;
  2248. /*
  2249. case Timeout_WaitPlug:
  2250. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout) {
  2251. _DetectPlugInTimeout();
  2252. destroySelGun(pSysInfo->CurGunSelected);
  2253. }
  2254. break;
  2255. */
  2256. /*
  2257. case Timeout_ReturnToChargingGunDet:
  2258. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE) {
  2259. if (getCurLcmPage() != _LCM_VIEW &&
  2260. getCurLcmPage() != _LCM_DETAIL_VIEW ) {
  2261. destroySelGun(pSysInfo->CurGunSelected); //jerry add
  2262. }
  2263. DisplayChargingInfo();
  2264. StopSystemTimeoutDet();
  2265. }
  2266. break;
  2267. */
  2268. case Timeout_ScanCard:
  2269. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_SCANCARD_TIMEOUT) {
  2270. log_info("Timeout_ScanCard");
  2271. strcpy((char *)pSysConfig->UserId, "");
  2272. ClearAuthorizedFlag();
  2273. StopSystemTimeoutDet();
  2274. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2275. pDcChargingInfo->SystemStatus = S_IDLE;
  2276. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  2277. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2278. }
  2279. break;
  2280. case Timeout_Terminating:
  2281. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TERMINATING_TIMEOUT) {
  2282. StopSystemTimeoutDet();
  2283. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2284. log_info("Terminating timeout");
  2285. setChargerMode(pSysInfo->CurGunSelected, S_COMPLETE);
  2286. }
  2287. break;
  2288. case Timeout_AddLine:
  2289. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_ADDLINE_TIMEOUT) {
  2290. log_info("Timeout_AddLine");
  2291. StopSystemTimeoutDet();
  2292. systemPageRestoreInit();
  2293. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2294. }
  2295. break;
  2296. case Timeout_DonateComfirm:
  2297. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_DONATECOMFIRM_TIMEOUT) {
  2298. log_info("Timeout_DonateComfirm");
  2299. StopSystemTimeoutDet();
  2300. systemPageRestoreInit();
  2301. }
  2302. break;
  2303. case Timeout_SelectPayMode:
  2304. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_SELECTPAY_TIMEOUT) {
  2305. log_info("Timeout_SelectPayMode");
  2306. StopSystemTimeoutDet();
  2307. systemPageRestoreInit();
  2308. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2309. }
  2310. break;
  2311. case Timeout_TradeCancel:
  2312. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_TRADECANCEL_TIMEOUT) {
  2313. log_info("Timeout_TradeCancel");
  2314. StopSystemTimeoutDet();
  2315. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2316. setChargerMode(pSysInfo->CurGunSelected, MODE_IDLE);
  2317. systemPageRestoreInit();
  2318. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2319. }
  2320. break;
  2321. case Timeout_LINEPAYING:
  2322. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_LINEPAYING_TIMEOUT) {
  2323. log_info("Timeout_LINEPAYING");
  2324. StopSystemTimeoutDet();
  2325. systemPageRestoreInit();
  2326. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2327. }
  2328. break;
  2329. }
  2330. // 各槍
  2331. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2332. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2333. /*
  2334. if (pDcChargingInfo->TimeoutFlag != 0)
  2335. log_info("Timeout ***********GunTimeoutFlag = %d(%d) ********",pDcChargingInfo->TimeoutFlag,
  2336. GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL);
  2337. */
  2338. switch (pDcChargingInfo->TimeoutFlag) {
  2339. case Timeout_AuthorizingForStop:
  2340. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT) {
  2341. log_info("Gun %d Timeout_AuthorizingForStop", gunIndex);
  2342. strcpy((char*)pSysConfig->UserId, "");
  2343. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  2344. pDcChargingInfo->SystemStatus = S_IDLE;
  2345. ClearAuthorizedFlag();
  2346. StopGunInfoTimeoutDet(gunIndex);
  2347. if (pSysInfo->CurGunSelected == gunIndex)
  2348. systemPageRestoreInit();
  2349. }
  2350. break;
  2351. case Timeout_WaitPlug:
  2352. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= _connectionTimeout) {
  2353. _DetectPlugInTimeout(gunIndex);
  2354. destroySelGun(gunIndex);
  2355. }
  2356. break;
  2357. case Timeout_EVCCID_Link:
  2358. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= EVCCID_LINK_TIMEOUT) {
  2359. _evccidlinktimeout(gunIndex);
  2360. }
  2361. break;
  2362. case Timeout_LinkError:
  2363. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= LINKERROR_TIMEOUT) {
  2364. _LinkErrorTimeout(gunIndex);
  2365. StopGunInfoTimeoutDet(gunIndex);
  2366. destroySelGun(gunIndex);
  2367. }
  2368. break;
  2369. case Timeout_Preparing:
  2370. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_PREPARE_TIMEOUT) {
  2371. _PrepareTimeout(gunIndex);
  2372. StopGunInfoTimeoutDet(gunIndex);
  2373. destroySelGun(gunIndex); //jerry add
  2374. }
  2375. break;
  2376. case Timeout_EvChargingDet:
  2377. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT) {
  2378. _DetectEvChargingEnableTimeout(gunIndex);
  2379. StopGunInfoTimeoutDet(gunIndex);
  2380. destroySelGun(gunIndex); //jerry add
  2381. }
  2382. break;
  2383. case Timeout_EvseChargingDet:
  2384. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT) {
  2385. _DetectEvseChargingEnableTimeout(gunIndex);
  2386. StopGunInfoTimeoutDet(gunIndex);
  2387. destroySelGun(gunIndex); //jerry add
  2388. }
  2389. break;
  2390. case Timeout_EvseCompleteDet:
  2391. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT) {
  2392. StopGunInfoTimeoutDet(gunIndex);
  2393. destroySelGun(gunIndex); //jerry add
  2394. }
  2395. break;
  2396. case Timeout_ForCcsPrechargeDet:
  2397. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT) {
  2398. _CcsPrechargeTimeout(gunIndex);
  2399. StopGunInfoTimeoutDet(gunIndex);
  2400. destroySelGun(gunIndex); //jerry add
  2401. }
  2402. break;
  2403. case Timeout_PlugOutGun:
  2404. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= PLUGOUTGUN_TIMEOUT) {
  2405. log_info("Gun[%d] Timeout_PlugOutGun",gunIndex);
  2406. setChargerMode(gunIndex, MODE_IDLE);
  2407. destroySelGun(gunIndex); //Jerry add
  2408. StopGunInfoTimeoutDet(gunIndex);
  2409. if (pSysInfo->CurGunSelected == gunIndex)
  2410. systemPageRestoreInit();
  2411. }
  2412. break;
  2413. case Timeout_FinalCost:
  2414. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_FINALCOST_TIMEOUT) {
  2415. log_info("Gun[%d] Timeout_FinalCost",gunIndex);
  2416. StopGunInfoTimeoutDet(gunIndex);
  2417. if (ShmDcCommonData->finalcost_flag[gunIndex] == FALSE) {
  2418. log_info("Not Get Final Cost");
  2419. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  2420. if (pSysInfo->CurGunSelected == gunIndex)
  2421. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  2422. }
  2423. }
  2424. break;
  2425. case Timeout_LineReigster:
  2426. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_LINEREGISTER_TIMEOUT) {
  2427. log_info("Gun[%d] Timeout_LineReigster",gunIndex);
  2428. StopGunInfoTimeoutDet(gunIndex);
  2429. systemPageRestoreInit();
  2430. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = FALSE;
  2431. ShmDcCommonData->OperateIDLE[gunIndex] = 1;
  2432. }
  2433. break;
  2434. case Timeout_ExitPage:
  2435. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_EXITPAGE_TIMEOUT) {
  2436. log_info("Gun[%d] Timeout_ExitPage", gunIndex);
  2437. StopGunInfoTimeoutDet(gunIndex);
  2438. ShmDcCommonData->is_exit[gunIndex] = TRUE;
  2439. if (pSysInfo->CurGunSelected == gunIndex)
  2440. pSysInfo->SystemPage = _PAGE_EXIT;
  2441. StartGunInfoTimeoutDet(gunIndex, Timeout_PlugOutGun);
  2442. }
  2443. break;
  2444. case Timeout_CompletPlugout:
  2445. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_COMPLETE_PLUGOUT_TIMEOUT) {
  2446. log_info("Gun[%d] Timeout_CompletePlugout", gunIndex);
  2447. setChargerMode(gunIndex, MODE_IDLE);
  2448. destroySelGun(gunIndex); //Jerry add
  2449. StopGunInfoTimeoutDet(gunIndex);
  2450. }
  2451. break;
  2452. }
  2453. }
  2454. sleep(1);
  2455. }
  2456. }
  2457. }
  2458. void CheckFactoryConfigFunction(void)
  2459. {
  2460. char Buf[256] = {0};
  2461. if (pSysInfo->FactoryConfiguration) {
  2462. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  2463. pSysConfig->ModelName,
  2464. pSysConfig->SerialNumber);
  2465. system(Buf);
  2466. system("rm -f /Storage/OCPP/OCPPConfiguration");
  2467. system("sync");
  2468. sleep(5);
  2469. system("reboot -f");
  2470. sleep(5);
  2471. system("reboot -f");
  2472. }
  2473. }
  2474. //===============================================
  2475. // Check reservation date is expired
  2476. //===============================================
  2477. int isReservationExpired(uint8_t gunIndex)
  2478. {
  2479. int result = NO;
  2480. struct tm expiredDate;
  2481. struct timeb expiredTime;
  2482. if (sscanf((char *) ShmOCPP16Data->ReserveNow[gunIndex].ExpiryDate,
  2483. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  2484. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  2485. &expiredDate.tm_min, &expiredDate.tm_sec) == 6) {
  2486. expiredDate.tm_year -= 1900;
  2487. expiredDate.tm_mon -= 1;
  2488. expiredTime.time = mktime(&expiredDate);
  2489. if (!CheckTimeOut(expiredTime)) {
  2490. result = YES;
  2491. }
  2492. }
  2493. return result;
  2494. }
  2495. //===============================================
  2496. // OCPP
  2497. //===============================================
  2498. void CheckOcppStatus(void)
  2499. {
  2500. bool canReset = true;
  2501. //bool canHardReset = true; //DS60-120 add
  2502. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES) {
  2503. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  2504. }
  2505. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES) {
  2506. if (pSysWarning->Level != WARN_LV_ER) {
  2507. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  2508. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2509. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2510. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  2511. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  2512. canReset = false;
  2513. if (pDcChargingInfo->SystemStatus >= S_REASSIGN &&
  2514. pDcChargingInfo->SystemStatus < S_TERMINATING) {
  2515. //canHardReset = false;
  2516. ChargingTerminalProcess(_index);
  2517. //restartFlag = 1;
  2518. }
  2519. }
  2520. }
  2521. }
  2522. if (canReset) {
  2523. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  2524. sprintf((char *)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2525. if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL) {
  2526. log_error("****** Hard Reboot ****** ");
  2527. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  2528. sleep(3);
  2529. system("reboot -f");
  2530. } else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL) {
  2531. log_error("****** Soft Reboot ****** ");
  2532. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  2533. sleep(3);
  2534. system("killall OcppBackend &");
  2535. KillAllTask();
  2536. TryCloseWatchdog();
  2537. system("/usr/bin/run_evse_restart.sh");
  2538. }
  2539. }
  2540. }
  2541. }
  2542. void OcppStopTransation(uint8_t gunIndex)
  2543. {
  2544. uint8_t _OcppGunIndex = gunIndex;
  2545. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2546. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  2547. if (pSysConfig->AcConnectorCount == 1 && gunIndex == 1) {
  2548. _OcppGunIndex = 2;
  2549. }
  2550. if (strcmp((char *)pDcChargingInfo->StartUserId, "") == EQUAL) {
  2551. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  2552. } else {
  2553. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)pDcChargingInfo->StartUserId);
  2554. }
  2555. log_info("IdTag = %s ", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  2556. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  2557. }
  2558. bool OcppRemoteStop(uint8_t gunIndex)
  2559. {
  2560. uint8_t acDirIndex = pSysConfig->AcConnectorCount;
  2561. // 有 AC 槍的話
  2562. if (acDirIndex > 0 && gunIndex > 0) {
  2563. gunIndex += acDirIndex;
  2564. }
  2565. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  2566. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES) {
  2567. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  2568. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  2569. }
  2570. return result;
  2571. }
  2572. bool WifiScheduleStop(uint8_t gunIndex)
  2573. {
  2574. bool result = false;
  2575. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2576. result = pDcChargingInfo->schedule.isTriggerStop;
  2577. pDcChargingInfo->schedule.isTriggerStop = NO;
  2578. return result;
  2579. }
  2580. void OcppRemoteStartChk()
  2581. {
  2582. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING) {
  2583. } else {
  2584. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  2585. uint8_t acDirIndex = pSysConfig->AcConnectorCount;
  2586. if (!isDetectPlugin(acDirIndex)) {
  2587. for (uint8_t ac_index = 0; ac_index < pSysConfig->AcConnectorCount; ac_index++) {
  2588. pAcChargingInfo = (struct ChargingInfoData*)GetAcChargingInfoData(ac_index);
  2589. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES) {
  2590. if (pAcChargingInfo->SystemStatus == S_IDLE ||
  2591. pAcChargingInfo->SystemStatus == S_RESERVATION) {
  2592. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  2593. pAcChargingInfo->RemoteStartFlag = YES;
  2594. pSysInfo->OrderCharging = YES;
  2595. //pSysInfo->OrderCharging = DEFAULT_AC_INDEX;
  2596. ShmOCPP16Data->CsMsg.bits[pSysConfig->TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  2597. DetectPluginStart(acDirIndex);
  2598. return;
  2599. }
  2600. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  2601. }
  2602. }
  2603. }
  2604. //uint8_t threeGunIndex = 0;
  2605. //uint8_t dcIndex = 0;
  2606. //bool isGunUsingStatus = false;
  2607. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  2608. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(_index);
  2609. /*
  2610. // 如果有 AC 槍,且 DC 槍也有兩把
  2611. if (acDirIndex == 1 && _index == 1) {
  2612. threeGunIndex = 1;
  2613. }
  2614. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES) {
  2615. dcIndex = _index;
  2616. }
  2617. if (pDcChargingInfo->SystemStatus != S_IDLE) {
  2618. isGunUsingStatus = true;
  2619. }
  2620. if (pDcChargingInfo->RemoteStartFlag == YES) {
  2621. //_remotestarting = true;
  2622. }
  2623. }
  2624. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  2625. if (isGunUsingStatus && pSysInfo->IsAlternatvieConf) {
  2626. if (dcIndex == 0) {
  2627. threeGunIndex = 0;
  2628. }
  2629. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  2630. return;
  2631. }
  2632. if (dcIndex == 0) {
  2633. threeGunIndex = 0;
  2634. }
  2635. */
  2636. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(_index);
  2637. if (ShmOCPP16Data->CsMsg.bits[_index].RemoteStartTransactionReq == YES &&
  2638. !isDetectPlugin(_index)) {
  2639. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2640. pDcChargingInfo->SystemStatus == S_RESERVATION) {
  2641. pDcChargingInfo->RemoteStartFlag = YES;
  2642. ShmDcCommonData->is_RemoteStart[_index] = TRUE;
  2643. //pSysInfo->OrderCharging = YES;
  2644. //pDcChargingInfo->SystemStatus = S_AUTHORIZING;
  2645. //pSysInfo->OrderCharging = gunIndex;
  2646. ShmOCPP16Data->CsMsg.bits[_index].RemoteStartTransactionReq = NO;
  2647. DetectPluginStart(_index);
  2648. setSelGunWaitToAuthor(_index);
  2649. log_info("Ocpp Remote Start Gun%d Pass", _index);
  2650. }
  2651. ShmOCPP16Data->CsMsg.bits[_index].RemoteStartTransactionReq = NO;
  2652. }
  2653. }
  2654. }
  2655. }
  2656. void ChkOcppStatus(uint8_t gunIndex)
  2657. {
  2658. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2659. if (pDcChargingInfo->SystemStatus == S_IDLE &&
  2660. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES) {
  2661. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  2662. if (isReservationExpired(gunIndex)) {
  2663. log_error("***************ChkOcppStatus : OcppReservedStatus******************** ");
  2664. pDcChargingInfo->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  2665. pDcChargingInfo->SystemStatus = S_RESERVATION;
  2666. }
  2667. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  2668. }
  2669. if (pDcChargingInfo->SystemStatus == S_RESERVATION &&
  2670. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES) {
  2671. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  2672. if (isReservationExpired(gunIndex)) {
  2673. log_error("***************ChkOcppStatus : Cancel OcppReservedStatus******************** ");
  2674. pDcChargingInfo->ReservationId = 0;
  2675. pDcChargingInfo->SystemStatus = S_IDLE;
  2676. }
  2677. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  2678. }
  2679. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES) {
  2680. log_error("***************ChkOcppStatus : OcppChangeAvailability******************** ");
  2681. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  2682. if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL) {
  2683. if (isDb_ready) {
  2684. DB_Update_Operactive(gunIndex, true);
  2685. }
  2686. pDcChargingInfo->IsAvailable = YES;
  2687. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2688. pDcChargingInfo->SystemStatus == S_RESERVATION ||
  2689. pDcChargingInfo->SystemStatus == S_MAINTAIN) {
  2690. setChargerMode(gunIndex, MODE_IDLE);
  2691. }
  2692. } else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL) {
  2693. if (isDb_ready) {
  2694. DB_Update_Operactive(gunIndex, false);
  2695. }
  2696. pDcChargingInfo->IsAvailable = NO;
  2697. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2698. pDcChargingInfo->SystemStatus == S_RESERVATION ||
  2699. pDcChargingInfo->SystemStatus == S_MAINTAIN) {
  2700. setChargerMode(gunIndex, MODE_MAINTAIN);
  2701. }
  2702. }
  2703. }
  2704. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES) {
  2705. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  2706. if (pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK && // DS60-120 add ||
  2707. pDcChargingInfo->SystemStatus <= S_CHARGING) {
  2708. // 充電中,需停止充電
  2709. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  2710. ChargingTerminalProcess(gunIndex);
  2711. }
  2712. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  2713. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  2714. }
  2715. }
  2716. bool CheckBackendChargingTimeout(uint8_t gunIndex)
  2717. {
  2718. bool result = false;
  2719. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2720. if (pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE) {
  2721. if (pSysConfig->MaxChargingDuration > 0) {
  2722. if (pDcChargingInfo->PresentChargedDuration > (pSysConfig->MaxChargingDuration * 60)) {
  2723. result = true;
  2724. }
  2725. }
  2726. }
  2727. return result;
  2728. }
  2729. bool CheckBackendChargingEnergy(uint8_t gunIndex)
  2730. {
  2731. bool result = false;
  2732. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2733. if (pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE) {
  2734. if (pSysConfig->MaxChargingEnergy > 0) {
  2735. if (pDcChargingInfo->PresentChargedEnergy > pSysConfig->MaxChargingEnergy) {
  2736. result = true;
  2737. }
  2738. }
  2739. }
  2740. return result;
  2741. }
  2742. void InformOcppErrOccur(uint8_t codeType)
  2743. {
  2744. char _error[25];
  2745. switch (codeType) {
  2746. case 4:
  2747. strcpy(_error, "InternalError");
  2748. break;
  2749. case 6:
  2750. strcpy(_error, "NoError");
  2751. break;
  2752. case 7:
  2753. strcpy(_error, "OtherError");
  2754. break;
  2755. case 13:
  2756. strcpy(_error, "UnderVoltage");
  2757. break;
  2758. case 14:
  2759. strcpy(_error, "OverVoltage");
  2760. break;
  2761. }
  2762. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2763. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, _error);
  2764. }
  2765. }
  2766. //===============================================
  2767. // Config process
  2768. //===============================================
  2769. /*
  2770. void AddPlugInTimes(uint8_t gunIndex)
  2771. {
  2772. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2773. if (pDcChargingInfo->Type == _Type_Chademo) {
  2774. pSysConfig->ChademoPlugInTimes += 1;
  2775. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  2776. pSysConfig->Ccs2PlugInTimes += 1;
  2777. } else if (pDcChargingInfo->Type == _Type_GB) {
  2778. pSysConfig->GbPlugInTimes += 1;
  2779. }
  2780. }*/
  2781. void ChangeStartOrStopDateTime(uint8_t isStart, uint8_t gunIndex)
  2782. {
  2783. char cmdBuf[32];
  2784. struct timeb csuTime;
  2785. struct tm *tmCSU;
  2786. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2787. ftime(&csuTime);
  2788. tmCSU = localtime(&csuTime.time);
  2789. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  2790. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  2791. tmCSU->tm_sec);
  2792. if (isStart) {
  2793. strcpy((char *)pDcChargingInfo->StartDateTime, cmdBuf);
  2794. } else {
  2795. strcpy((char *)pDcChargingInfo->StopDateTime, cmdBuf);
  2796. }
  2797. }
  2798. void ChangeGunSelectByIndex(uint8_t sel)
  2799. {
  2800. pSysInfo->CurGunSelected = sel;
  2801. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  2802. }
  2803. void CheckIsAlternatvieByModelName()
  2804. {
  2805. if (pSysConfig->ModelName[1] == 'W') { //DS60-120
  2806. // 壁掛
  2807. pSysInfo->IsAlternatvieConf = YES;
  2808. } else {
  2809. pSysInfo->IsAlternatvieConf = NO;
  2810. }
  2811. }
  2812. void StopProcessingLoop()
  2813. {
  2814. log_info("Stop Processing....");
  2815. for (;;) {
  2816. CheckFactoryConfigFunction();
  2817. //CheckFwUpdateFunction();
  2818. if (pSysWarning->Level == WARN_LV_ER) {
  2819. ChkPrimaryStatus();
  2820. if (pSysWarning->Level == WARN_LV_NL) {
  2821. log_info("Soft reboot for retry self-tets (Primary). ");
  2822. KillAllTask();
  2823. sleep(3);
  2824. TryCloseWatchdog();
  2825. system("/usr/bin/run_evse_restart.sh");
  2826. return;
  2827. }
  2828. }
  2829. sleep(1);
  2830. TryFeedWatchdog();
  2831. }
  2832. }
  2833. bool IsConnectorWholeIdle()
  2834. {
  2835. bool result = true;
  2836. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  2837. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  2838. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2839. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  2840. result = false;
  2841. break;
  2842. }
  2843. }
  2844. for (uint8_t count = 0; count < pSysConfig->AcConnectorCount; count++) {
  2845. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(count);
  2846. if (pAcChargingInfo->SystemStatus != S_IDLE &&
  2847. pAcChargingInfo->IsErrorOccur == NO) {
  2848. result = false;
  2849. break;
  2850. }
  2851. }
  2852. return result;
  2853. }
  2854. void ClearAlarmCodeWhenAcOff()
  2855. {
  2856. if (!pSysInfo->AcContactorStatus) {
  2857. pAlarmCode->AlarmEvents.bits.PsuNoResource = NO;
  2858. }
  2859. }
  2860. //==========================================
  2861. // Check Smart Charging Profile
  2862. //==========================================
  2863. int GetStartScheduleTime(uint8_t *time)
  2864. {
  2865. int result = -1;
  2866. struct tm tmScheduleStart;
  2867. struct timeb tbScheduleStart;
  2868. 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)) {
  2869. tmScheduleStart.tm_year -= 1900;
  2870. tmScheduleStart.tm_mon -= 1;
  2871. tbScheduleStart.time = mktime(&tmScheduleStart);
  2872. tbScheduleStart.millitm = 0;
  2873. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  2874. }
  2875. return result;
  2876. }
  2877. void CheckSmartChargeProfile(uint8_t _index)
  2878. {
  2879. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2880. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == YES) {
  2881. // Get Charging Profile
  2882. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  2883. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL &&
  2884. ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileId == YES //DS60-120 add
  2885. ) {
  2886. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  2887. uint8_t _startCount = NO_DEFINE;
  2888. uint8_t _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  2889. for (uint8_t _count = 0; _count < _maxCount; _count++) {
  2890. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  2891. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod) {
  2892. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  2893. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR) {
  2894. _startCount = _count;
  2895. }
  2896. }
  2897. }
  2898. log_info("_startCount = %d ", _startCount);
  2899. if (_startCount < _maxCount) {
  2900. //DS60-120 add
  2901. log_info("Profile Limit = %f ", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  2902. pDcChargingInfo->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  2903. if (pDcChargingInfo->EvBatterytargetVoltage > 0 && pDcChargingInfo->PresentChargingVoltage > 0) {
  2904. pDcChargingInfo->ChargingProfileCurrent = (pDcChargingInfo->ChargingProfilePower / pDcChargingInfo->PresentChargingVoltage) * 10;
  2905. } else {
  2906. pDcChargingInfo->ChargingProfileCurrent = 0;
  2907. }
  2908. } else {
  2909. pDcChargingInfo->ChargingProfilePower = -1;
  2910. pDcChargingInfo->ChargingProfileCurrent = -1;
  2911. }
  2912. } else {
  2913. pDcChargingInfo->ChargingProfilePower = -1;
  2914. pDcChargingInfo->ChargingProfileCurrent = -1;
  2915. }
  2916. log_info("ChargingProfilePower = %f ", pDcChargingInfo->ChargingProfilePower);
  2917. log_info("ChargingProfileCurrent = %f ", pDcChargingInfo->ChargingProfileCurrent);
  2918. }
  2919. }
  2920. void ChargingProfileFlat(uint8_t _index)
  2921. {
  2922. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO) {
  2923. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO) {
  2924. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  2925. }
  2926. }
  2927. }
  2928. /*
  2929. void CheckReturnToChargingConn()
  2930. {
  2931. if ((pSysConfig->TotalConnectorCount + pSysConfig->AcConnectorCount) > 1 &&
  2932. pSysInfo->PageIndex != _LCM_START_AUTHORIZING &&
  2933. pSysInfo->PageIndex != _LCM_START_AUTHORIZE_FAIL &&
  2934. pSysInfo->PageIndex != _LCM_WAIT_PLUGIN) {
  2935. bool isReturnTimeout = false;
  2936. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  2937. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  2938. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  2939. if (count == pSysInfo->CurGunSelected) {
  2940. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2941. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2942. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2943. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  2944. ) {
  2945. isReturnTimeout = false;
  2946. break;
  2947. }
  2948. } else if (count != pSysInfo->CurGunSelected) {
  2949. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2950. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2951. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2952. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  2953. ) {
  2954. isReturnTimeout = true;
  2955. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2956. }
  2957. }
  2958. }
  2959. // AC 槍
  2960. if (!isReturnTimeout && pSysConfig->AcConnectorCount > 0) {
  2961. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2962. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  2963. // 沒有選中 AC,且 AC 在充電中
  2964. if (pSysInfo->CurGunSelectedByAc == NO_DEFINE &&
  2965. (pAcChargingInfo->SystemStatus >= S_PREPARNING &&
  2966. pAcChargingInfo->SystemStatus <= S_COMPLETE)) {
  2967. // 當前 DC 充電槍在 idle 狀態
  2968. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2969. pDcChargingInfo->SystemStatus == S_RESERVATION) {
  2970. isReturnTimeout = true;
  2971. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2972. }
  2973. } else if (pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2974. ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2975. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2976. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2977. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))) {
  2978. // 當前 DC 充電槍在 idle 狀態
  2979. if (pAcChargingInfo->SystemStatus == S_IDLE ||
  2980. pAcChargingInfo->SystemStatus == S_RESERVATION) {
  2981. isReturnTimeout = true;
  2982. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2983. }
  2984. }
  2985. }
  2986. if (!isReturnTimeout) {
  2987. StopSystemTimeoutDet();
  2988. }
  2989. }
  2990. }
  2991. bool GetStartChargingByAlterMode(uint8_t _gun)
  2992. {
  2993. bool result = true;
  2994. if (pSysConfig->TotalConnectorCount == 2 &&
  2995. pSysInfo->IsAlternatvieConf == YES) {
  2996. for (uint8_t _select = 0; _select < pSysConfig->TotalConnectorCount; _select++) {
  2997. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_select);
  2998. if (_select != _gun) {
  2999. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  3000. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  3001. result = false;
  3002. break;
  3003. }
  3004. }
  3005. }
  3006. }
  3007. return result;
  3008. }
  3009. */
  3010. void TheEndCharging(uint8_t gunIndex)
  3011. {
  3012. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3013. ftime(&endChargingTime[gunIndex]);
  3014. if (pDcChargingInfo->PresentChargedDuration != 0) {
  3015. pDcChargingInfo->PresentChargedDuration = DiffTimeb(startChargingTime[gunIndex], endChargingTime[gunIndex]);
  3016. }
  3017. pDcChargingInfo->isRemoteStart = NO;
  3018. if (pDcChargingInfo->TimeoutFlag != Timeout_FinalCost)
  3019. StopGunInfoTimeoutDet(gunIndex);
  3020. //StartGunInfoTimeoutDet(gunIndex, Timeout_EvseCompleteDet);
  3021. ChangeStartOrStopDateTime(NO, gunIndex);
  3022. DB_Insert_Record(gunIndex);
  3023. }
  3024. void UpdateErrorCodeToOcpp(uint8_t index)
  3025. {
  3026. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  3027. //log_info("%d = ConnectorAlarmCode = %s", index, pDcChargingInfo->ConnectorAlarmCode);
  3028. //log_info("%d = EvConnAlarmCode = %s", index, pDcChargingInfo->EvConnAlarmCode);
  3029. if (strcmp((char *)pDcChargingInfo->ConnectorAlarmCode, "") != EQUAL) {
  3030. //if (strlen((char *)pDcChargingInfo->ConnectorAlarmCode) == EQUAL) {
  3031. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "InternalError");
  3032. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)pDcChargingInfo->ConnectorAlarmCode);
  3033. } else if (strcmp((char *)pDcChargingInfo->EvConnAlarmCode, "") != EQUAL) {
  3034. //} else if (strlen((char *)pDcChargingInfo->EvConnAlarmCode) == EQUAL) {
  3035. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "OtherError");
  3036. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)pDcChargingInfo->EvConnAlarmCode);
  3037. }
  3038. //log_info("Gun %d = VendorErrorCode = %s", index, (char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode);
  3039. }
  3040. void AdjustChargerCurrent()
  3041. {
  3042. pSysConfig->RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  3043. // 設定的電流~ 如超過可輸出的電流,則 bypass
  3044. if (pSysConfig->RatingCurrent < pSysConfig->MaxChargingCurrent ||
  3045. pSysConfig->RatingCurrent == 0) {
  3046. pSysConfig->MaxChargingCurrent = 0;
  3047. }
  3048. log_info("PSU : MaxChargingPower = %d, MaxChargingCurrent = %d",
  3049. ShmPsuData->SystemAvailablePower / 10,
  3050. ShmPsuData->SystemAvailableCurrent / 10
  3051. );
  3052. log_info("Config : ChargingPower = %d, ChargingCurrent = %d",
  3053. pSysConfig->MaxChargingPower,
  3054. pSysConfig->MaxChargingCurrent
  3055. );
  3056. }
  3057. void ResetDetAlarmStatus(uint8_t gun)
  3058. {
  3059. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gun);
  3060. if (pDcChargingInfo->Type == _Type_Chademo) {
  3061. if (pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP == YES) {
  3062. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = NO;
  3063. }
  3064. } else if (pDcChargingInfo->Type == _Type_GB) {
  3065. if (pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP == YES) {
  3066. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = NO;
  3067. }
  3068. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3069. if (pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP == YES) {
  3070. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = NO;
  3071. }
  3072. }
  3073. }
  3074. static void autoStartCharging(uint8_t gunIndex)
  3075. {
  3076. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3077. if ( (pSysInfo->SystemPage >= _PAGE_AUTHORIZE && pSysInfo->SystemPage <= _PAGE_SENSING) ||
  3078. (pDcChargingInfo->isEVCCIDVerify && strcmp( (char *)pSysConfig->UserId , "" ) != EQUAL)) {
  3079. return;
  3080. }
  3081. if ((pDcChargingInfo->ConnectorPlugIn == YES) &&
  3082. (pDcChargingInfo->SystemStatus == S_IDLE )
  3083. ) {
  3084. if(!pSysConfig->AuthorisationMode) {
  3085. /*
  3086. if (pSysConfig->isAuthrizeByEVCCID && !pDcChargingInfo->isEVCCIDVerify &&
  3087. pDcChargingInfo->Type == _Type_CCS_2) {
  3088. // LCM show linking pic. & timeout 2 min
  3089. if( pSysInfo->CurGunSelected == gunIndex ) {
  3090. systemPageRestoreInit();
  3091. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3092. }
  3093. StartGunInfoTimeoutDet(gunIndex,Timeout_EVCCID_Link);
  3094. // Getting EVCCID
  3095. if( strcmp( (char *)pDcChargingInfo->EVCCID, "" ) != EQUAL ) {
  3096. log_info("Authorizing EVCCID");
  3097. StopSystemTimeoutDet();
  3098. strcpy((char *)pSysConfig->UserId, (char *)pDcChargingInfo->EVCCID);
  3099. ChangeGunSelectByIndex(gunIndex);
  3100. confirmSelGun(gunIndex);
  3101. pDcChargingInfo->isEVCCIDVerify = true;
  3102. pSysInfo->SystemPage = _LCM_START_AUTHORIZING;
  3103. log_info("Get User(%d) ID:%s",gunIndex,pSysConfig->UserId);
  3104. setChargerMode(gunIndex, MODE_AUTHORIZING);
  3105. }
  3106. } else
  3107. return;
  3108. */
  3109. } else {
  3110. strcpy((char *)&pSysConfig->UserId, "AutoStartCharging");
  3111. ChangeGunSelectByIndex(gunIndex);
  3112. confirmSelGun(gunIndex);
  3113. ShmDcCommonData->TradeCancel = FALSE;
  3114. ShmDcCommonData->is_AutoStart[gunIndex] = TRUE;
  3115. pSysInfo->SystemPage = _PAGE_SENSING;
  3116. ShmDcCommonData->AuthPass_flag[gunIndex] = TRUE;
  3117. //setChargerMode(gunIndex, MODE_AUTHORIZING);
  3118. log_info("Get User(%d) ID:%s",gunIndex,pSysConfig->UserId);
  3119. }
  3120. }
  3121. }
  3122. static bool PrecheckIsPass(uint8_t gunIndex)
  3123. {
  3124. bool result = true;
  3125. // relay welding or driving 是反向
  3126. result = !ShmDcCommonData->GunRelayWeldingOccur[gunIndex];
  3127. return result;
  3128. }
  3129. static void ReviewCriticalAlarm(void)
  3130. {
  3131. if (
  3132. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES ||
  3133. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES ||
  3134. pAlarmCode->AlarmEvents.bits.DoorOpen == YES ||
  3135. pSysWarning->ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  3136. pAlarmCode->AlarmEvents.bits.PsuFailureAlarm == YES ||
  3137. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES ||
  3138. //Power cabinet alarm status
  3139. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop == YES ||
  3140. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen == YES ||
  3141. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP == YES ||
  3142. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputUVP == YES ||
  3143. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP == YES ||
  3144. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP == YES ||
  3145. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP == YES ||
  3146. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure == YES ||
  3147. ShmSelectGunInfo->EthDevStatus.Backend == 0 ||
  3148. ShmSelectGunInfo->EthDevStatus.Backend == 2
  3149. ) {
  3150. if ((ShmSelectGunInfo->EthDevStatus.Backend == 0 ||
  3151. ShmSelectGunInfo->EthDevStatus.Backend == 2) &&
  3152. (ShmDcCommonData->DebugFlag == TRUE ||
  3153. pSysConfig->AuthorisationMode )) {
  3154. pSysWarning->Level = WARN_LV_NL;
  3155. return;
  3156. }
  3157. pSysWarning->Level = WARN_LV_ER;
  3158. } else {
  3159. pSysWarning->Level = WARN_LV_NL;
  3160. }
  3161. }
  3162. static void CheckRelayWeldingOrDrivingFault(uint8_t gunIndex)
  3163. {
  3164. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3165. // relay welding fault then stop the charging process.
  3166. uint8_t faultCode = RELAY_STATUS_ERROR_NONE;
  3167. //static uint8_t drivingCount = 0;
  3168. if (gunIndex == 0) {
  3169. if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  3170. ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  3171. faultCode = RELAY_STATUS_ERROR_WELDING;
  3172. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  3173. ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  3174. faultCode = RELAY_STATUS_ERROR_DRIVING;
  3175. }
  3176. } else if (gunIndex == 1) {
  3177. if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  3178. ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  3179. faultCode = RELAY_STATUS_ERROR_WELDING;
  3180. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  3181. ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  3182. faultCode = RELAY_STATUS_ERROR_DRIVING;
  3183. }
  3184. }
  3185. if (pSysConfig->TotalConnectorCount >= 2 &&
  3186. !pSysInfo->IsAlternatvieConf) {
  3187. // 橋接
  3188. if (ShmDcCommonData->CheckRelayStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  3189. ShmDcCommonData->CheckRelayStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  3190. faultCode = RELAY_STATUS_ERROR_WELDING;
  3191. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  3192. ShmDcCommonData->CheckRelayStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  3193. faultCode = RELAY_STATUS_ERROR_DRIVING;
  3194. }
  3195. }
  3196. if (faultCode == RELAY_STATUS_ERROR_WELDING) {
  3197. // welding
  3198. if (pDcChargingInfo->Type == _Type_Chademo) {
  3199. RecordAlarmCode(gunIndex, "011011");
  3200. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = YES;
  3201. } else if (pDcChargingInfo->Type == _Type_GB) {
  3202. RecordAlarmCode(gunIndex, "011015");
  3203. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = YES;
  3204. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3205. RecordAlarmCode(gunIndex, "011013");
  3206. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = YES;
  3207. }
  3208. ShmDcCommonData->GunRelayWeldingOccur[gunIndex] = YES;
  3209. //EmcOccureByString("");
  3210. } else if (faultCode == RELAY_STATUS_ERROR_DRIVING) {
  3211. //if (drivingCount++ != 2) {
  3212. // return;
  3213. //}
  3214. // driving
  3215. if (pDcChargingInfo->Type == _Type_Chademo) {
  3216. RecordAlarmCode(gunIndex, "011012");
  3217. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = YES;
  3218. } else if (pDcChargingInfo->Type == _Type_GB) {
  3219. RecordAlarmCode(gunIndex, "011016");
  3220. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = YES;
  3221. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3222. RecordAlarmCode(gunIndex, "011014");
  3223. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = YES;
  3224. }
  3225. ShmDcCommonData->GunRelayDrivingOccur[gunIndex] = YES;
  3226. //EmcOccureByString("");
  3227. } else {
  3228. ShmDcCommonData->GunRelayWeldingOccur[gunIndex] = NO;
  3229. ShmDcCommonData->GunRelayDrivingOccur[gunIndex] = NO;
  3230. //drivingCount = 0;
  3231. }
  3232. }
  3233. static void isChargingAverageState(void)
  3234. {
  3235. if (pSysInfo->MainChargingMode != _MAIN_CHARGING_MODE_AVER) {
  3236. pSysInfo->ReAssignedFlag = _REASSIGNED_NONE;
  3237. return;
  3238. }
  3239. // 均充 -> 最大充
  3240. if (pSysInfo->BridgeRelayStatus == NO) {
  3241. if (pSysInfo->ReAssignedFlag == _REASSIGNED_NONE) {
  3242. log_info("=============Smart Charging============= Step 11 ");
  3243. pSysInfo->ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  3244. }
  3245. } else if (pSysInfo->ReAssignedFlag != _REASSIGNED_COMP &&
  3246. pSysInfo->ReAssignedFlag != _REASSIGNED_WAITING) {
  3247. log_info("=============Smart Charging============= Step 14 ");
  3248. pSysInfo->ReAssignedFlag = _REASSIGNED_WAITING;
  3249. } else if (pSysInfo->ReAssignedFlag == _REASSIGNED_COMP) {
  3250. pSysInfo->MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3251. pSysInfo->ReAssignedFlag = _REASSIGNED_NONE;
  3252. }
  3253. }
  3254. bool Taskconutstring(char *src, char *taskname)
  3255. {
  3256. bool result = false;
  3257. if (src == NULL || strlen(src) == 0)
  3258. return result;
  3259. if (strstr(src, taskname) != NULL &&
  3260. strstr(src, "grep") == NULL &&
  3261. strstr(src, "[") == NULL)
  3262. {
  3263. result = true;
  3264. }
  3265. return result;
  3266. }
  3267. int GetProcessCount(char *procName)
  3268. {
  3269. int result = 0;
  3270. FILE *fp;
  3271. char cmd[256];
  3272. char buf[256];
  3273. sprintf(cmd, "ps -ef |grep %s", procName);
  3274. fp = popen(cmd, "r");
  3275. if(fp != NULL)
  3276. {
  3277. while(fgets(buf, sizeof(buf), fp) != NULL)
  3278. {
  3279. if (Taskconutstring(buf, procName))
  3280. result++;
  3281. }
  3282. }
  3283. pclose(fp);
  3284. return result;
  3285. }
  3286. void CheckTaskAlive()
  3287. {
  3288. pid_t Pid = fork();
  3289. if ( Pid == 0 ) {
  3290. //log_info("CheckTaskAlive fork Child's PID is %d", getpid());
  3291. while(1) {
  3292. sleep(3);
  3293. unsigned char count = GetProcessCount("Module_ChkSysTask");
  3294. if( count != 1 ) {
  3295. system("pkill Module_ChkSysTask");
  3296. sleep(10);
  3297. //log_info("Create Check System Task");
  3298. system("/root/Module_ChkSysTask &");
  3299. }
  3300. }
  3301. return;
  3302. }
  3303. log_info("Create Check Systam fork:%d",Pid);
  3304. return;
  3305. }
  3306. /**
  3307. * [checkPileEndGfdResult 確認充電樁GFD狀態]
  3308. * @Author Jerry
  3309. * @DateTime 2021-07-01
  3310. * @param gunIndex [當前的槍]
  3311. * @param gunType [槍的類型]
  3312. * @param sysStatus [槍的系統狀態]
  3313. */
  3314. static void checkPileEndGfdResult(uint8_t gunIndex, uint8_t gunType, uint8_t sysStatus)
  3315. {
  3316. switch (gunType) {
  3317. case _Type_Chademo:
  3318. // 檢查樁端的 GFD 結果
  3319. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3320. isPrechargeStatus_chademo(gunIndex) >= 6 ) {
  3321. // 當前操作的槍號,進入 Charging
  3322. setChargerMode(gunIndex, MODE_CHARGING);
  3323. }
  3324. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3325. // GFD 錯誤停止
  3326. RecordAlarmCode(gunIndex, "012234");
  3327. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = YES;
  3328. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  3329. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3330. // GFD 警告
  3331. //RecordAlarmCode(gunIndex, "012296");
  3332. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  3333. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = YES;
  3334. }
  3335. else {
  3336. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  3337. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  3338. }
  3339. break;
  3340. case _Type_GB:
  3341. // 檢查樁端的 GFD 結果
  3342. //if (isPrechargeStatus_gb(gunIndex) > 5 && isPrechargeStatus_gb(gunIndex) < 9) {
  3343. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3344. isPrechargeStatus_gb(gunIndex) >= 6) {
  3345. setChargerMode(gunIndex, MODE_CHARGING);
  3346. }
  3347. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3348. // GFD 錯誤停止
  3349. RecordAlarmCode(gunIndex, "012236");
  3350. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = YES;
  3351. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  3352. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3353. // GFD 警告
  3354. //RecordAlarmCode(gunIndex, "012298");
  3355. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  3356. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = YES;
  3357. }
  3358. else {
  3359. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  3360. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  3361. }
  3362. break;
  3363. case _Type_CCS_2:
  3364. // 檢查樁端的 GFD 結果
  3365. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3366. (pDcChargingInfo->GroundFaultStatus == GFD_PASS ||
  3367. pDcChargingInfo->GroundFaultStatus == GFD_WARNING)
  3368. ) {
  3369. setChargerMode(gunIndex, MODE_CCS_PRECHARGE_STEP0);
  3370. }
  3371. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3372. // GFD 錯誤停止
  3373. RecordAlarmCode(gunIndex, "012235");
  3374. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  3375. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  3376. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3377. // GFD 警告
  3378. //RecordAlarmCode(gunIndex, "012297");
  3379. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  3380. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = YES;
  3381. }
  3382. else {
  3383. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  3384. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  3385. }
  3386. break;
  3387. }
  3388. }
  3389. static void checkEvBoardReqStop(uint8_t sysStatus, uint8_t gunIndex)
  3390. {
  3391. uint8_t evBoardStopState = 0;
  3392. if ((evBoardStopState = isEvBoardStopChargeFlag(gunIndex)) > 0) {
  3393. // 板端要求停止 (錯誤)
  3394. if (sysStatus != S_CCS_PRECHARGE_ST0 &&
  3395. sysStatus != S_CCS_PRECHARGE_ST1) {
  3396. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3397. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "EVDisconnected");
  3398. }
  3399. }
  3400. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  3401. log_info("EV Board Stop Charging with some error");
  3402. ChargingAlarmProcess(gunIndex);
  3403. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  3404. log_info("Power Cabient Stop Charging");
  3405. ChargingTerminalProcess(gunIndex);
  3406. }
  3407. } else if (isEvBoardNormalStopChargeFlag(gunIndex) == YES) {
  3408. // 板端要求停止 (正常)
  3409. if (sysStatus != S_CCS_PRECHARGE_ST0 &&
  3410. sysStatus != S_CCS_PRECHARGE_ST1) {
  3411. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3412. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "EVDisconnected");
  3413. }
  3414. }
  3415. log_info("EV Board Normal Stop Charging");
  3416. ChargingTerminalProcess(gunIndex);
  3417. } else if (OcppRemoteStop(gunIndex) == YES ||
  3418. WifiScheduleStop(gunIndex)) {
  3419. // 後臺要求停止
  3420. log_info("OCPP Stop Charging");
  3421. ChargingTerminalProcess(gunIndex);
  3422. }
  3423. }
  3424. static void checkOCPPReqStop(uint8_t gunIndex)
  3425. {
  3426. if (OcppRemoteStop(gunIndex) == YES ||
  3427. WifiScheduleStop(gunIndex) ||
  3428. CheckBackendChargingTimeout(gunIndex) ||
  3429. CheckBackendChargingEnergy(gunIndex) ||
  3430. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex].ResponseIdTagInfo.Status, "Invalid") == EQUAL) {
  3431. // 後臺要求停止
  3432. ChargingTerminalProcess(gunIndex);
  3433. }
  3434. }
  3435. void ResetIdleData(uint8_t gunIndex)
  3436. {
  3437. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3438. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  3439. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  3440. float powerconsumption = ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption;
  3441. pDcChargingInfo->PresentChargedDuration = 0;
  3442. pDcChargingInfo->RemainChargingDuration = 0;
  3443. pDcChargingInfo->PresentChargingVoltage = 0;//DS60-120 add
  3444. pDcChargingInfo->PresentChargingCurrent = 0;//DS60-120 add
  3445. pDcChargingInfo->RemoteStartFlag = NO;
  3446. pDcChargingInfo->Replug_flag = FALSE;
  3447. strcpy((char *)pDcChargingInfo->StartDateTime, "");
  3448. strcpy((char *)pDcChargingInfo->StopDateTime, "");
  3449. strcpy((char *)pDcChargingInfo->StartUserId, "");
  3450. strcpy((char *)pSysConfig->UserId, "");
  3451. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "");
  3452. ClearDetectPluginFlag(gunIndex);
  3453. //Jerry add
  3454. memset(&ShmSelectGunInfo->PricesInfo[gunIndex], 0, sizeof(PricesInfo));
  3455. memset(&ShmDcCommonData->TransactionInfo[gunIndex], 0, sizeof(RecordTransactionInfo));
  3456. memset(&ShmDcCommonData->pGunInfo[gunIndex], 0, sizeof(GunInfo));
  3457. ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption = powerconsumption;
  3458. ShmDcCommonData->finalcost_flag[gunIndex] = FALSE;
  3459. ShmDcCommonData->AuthPass_flag[gunIndex] = FALSE;
  3460. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  3461. ShmDcCommonData->OperateIDLE[gunIndex] = 1;
  3462. ShmDcCommonData->is_RemoteStart[gunIndex] = FALSE;
  3463. ShmDcCommonData->is_AutoStart[gunIndex] = FALSE;
  3464. ShmDcCommonData->is_exit[gunIndex] = FALSE;
  3465. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  3466. ShmDcCommonData->is_plugout[gunIndex] = FALSE;
  3467. ShmSelectGunInfo->PricesInfo[gunIndex].Balance = FAIL_BALANCE_PRICES;
  3468. destroySelGun(gunIndex);
  3469. ResetDetAlarmStatus(gunIndex); //recovery OVP status code
  3470. if (ShmSelectGunInfo->AuthorStateFromCabinet[gunIndex] == YES) {
  3471. ShmSelectGunInfo->AuthorStateFromCabinet[gunIndex] = NO;
  3472. }
  3473. //strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].VendorErrorCode, "");
  3474. ReleaseAlarmCode(gunIndex);
  3475. if (pSysInfo->SystemPage != _PAGE_SELECT_GUN &&
  3476. pSysInfo->SystemPage != _PAGE_SELECT_PAY &&
  3477. pSysInfo->CurGunSelected == gunIndex) {
  3478. systemPageRestoreInit();
  3479. }
  3480. }
  3481. void CheckErrorCode(uint8_t gunIndex)
  3482. {
  3483. /*
  3484. struct ChargingInfoData *pSelectedDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3485. if (gunIndex == pSysInfo->CurGunSelected) {
  3486. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3487. } else if (pSelectedDcChargingInfo->SystemStatus != S_IDLE &&
  3488. pSelectedDcChargingInfo->SystemStatus != S_RESERVATION &&
  3489. pSelectedDcChargingInfo->SystemStatus != S_MAINTAIN &&
  3490. pSelectedDcChargingInfo->SystemStatus != S_FAULT) {
  3491. if (pSelectedDcChargingInfo->SystemStatus == S_CHARGING) {
  3492. if(ShmDcCommonData->PayPass_flag[gunIndex] == FALSE)
  3493. pSysInfo->SystemPage = _PAGE_PAYING;
  3494. else
  3495. pSysInfo->SystemPage = _PAGE_COMPLETE;
  3496. } else {
  3497. systemPageRestoreInit();
  3498. }
  3499. }
  3500. */
  3501. ClearDetectPluginFlag(gunIndex);
  3502. if (pDcChargingInfo->SystemStatus != S_FAULT)
  3503. {
  3504. UpdateErrorCodeToOcpp(gunIndex);
  3505. setChargerMode(gunIndex, MODE_FAULT);
  3506. }
  3507. }
  3508. void AuthorizeStopCharging(uint8_t gunIndex)
  3509. {
  3510. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3511. checkOCPPReqStop(gunIndex);
  3512. //RfidStopCharging();
  3513. }
  3514. void showversion()
  3515. {
  3516. char str[10240];
  3517. int len = 0;
  3518. uint8_t index = 0;
  3519. len += sprintf(&str[len],"\n=========== Dispenser info =========== \n");
  3520. len += sprintf(&str[len],"SW Version = %s\n", fwVersion);
  3521. len += sprintf(&str[len],"Debug Version = %s\n",DebugVersion);
  3522. len += sprintf(&str[len],"ModelName = %s\n", pSysConfig->ModelName);
  3523. len += sprintf(&str[len],"Relay Board FW Rev = %s\n", pSysInfo->RelayModuleFwRev);
  3524. len += sprintf(&str[len],"Fan Board FW Rev = %s\n", pSysInfo->FanModuleFwRev);
  3525. len += sprintf(&str[len],"Primary FW Rev = %s\n", pSysInfo->CsuPrimFwRev);
  3526. len += sprintf(&str[len],"LED FW Rev = %s\n", pSysInfo->LedModuleFwRev);
  3527. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  3528. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  3529. if (pDcChargingInfo->Type == _Type_Chademo) {
  3530. len += sprintf(&str[len],"CHAdeMO[%d] FW Rev = %s\n",
  3531. index,
  3532. ShmCHAdeMOData->evse[pDcChargingInfo->type_index].version);
  3533. } else if (pDcChargingInfo->Type == _Type_GB) {
  3534. len += sprintf(&str[len],"GBT[%d] FW Rev = %s\n",
  3535. index,
  3536. ShmGBTData->evse[pDcChargingInfo->type_index].version);
  3537. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3538. len += sprintf(&str[len],"CCS[%d] FW Rev = %s\n",
  3539. index,
  3540. ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].version);
  3541. }
  3542. }
  3543. len += sprintf(&str[len],"==================================== ");
  3544. log_info("%s",str);
  3545. }
  3546. void SetNatural200AGunOTPValue(struct DERATING_BY_OTP* deratingByConnOtp)
  3547. {
  3548. memset(&deratingByConnOtp->deratingTargetRate[0], 200, sizeof(deratingByConnOtp->deratingTargetRate));
  3549. }
  3550. void SetNatural300AGunOTPValue(struct DERATING_BY_OTP* deratingByConnOtp)
  3551. {
  3552. deratingByConnOtp->deratingTargetCurrent[0] = 500;
  3553. deratingByConnOtp->deratingTargetCurrent[1] = 300;
  3554. deratingByConnOtp->deratingTargetCurrent[2] = 100;
  3555. deratingByConnOtp->deratingTargetCurrent[3] = 100;
  3556. deratingByConnOtp->deratingTargetCurrent[4] = 100;
  3557. }
  3558. void SetLiquidCoolGunOTPValue(struct DERATING_BY_OTP* deratingByConnOtp)
  3559. {
  3560. deratingByConnOtp->deratingTargetCurrent[0] = 500;
  3561. deratingByConnOtp->deratingTargetCurrent[1] = 400;
  3562. deratingByConnOtp->deratingTargetCurrent[2] = 300;
  3563. deratingByConnOtp->deratingTargetCurrent[3] = 300;
  3564. deratingByConnOtp->deratingTargetCurrent[4] = 300;
  3565. }
  3566. void SetCHAdeMoTypeJOTPValue(uint8_t regulation,struct DERATING_BY_OTP* deratingByConnOtp)
  3567. {
  3568. if (regulation == 'J') {
  3569. memset(&deratingByConnOtp->deratingTargetRate[0], 125, sizeof(deratingByConnOtp->deratingTargetRate));
  3570. } else {
  3571. memset(&deratingByConnOtp->deratingTargetRate[0], 120, sizeof(deratingByConnOtp->deratingTargetRate));
  3572. }
  3573. }
  3574. void SetCHAdeMoTypeKOTPValue(struct DERATING_BY_OTP* deratingByConnOtp)
  3575. {
  3576. memset(&deratingByConnOtp->deratingTargetRate[0], 200, sizeof(deratingByConnOtp->deratingTargetRate));
  3577. }
  3578. void SetCHAdeMoTypeSJOTPValue(struct DERATING_BY_OTP* deratingByConnOtp)
  3579. {
  3580. deratingByConnOtp->deratingTargetRate[0] = 1;
  3581. deratingByConnOtp->deratingTargetRate[1] = 0.8;
  3582. }
  3583. static void SetGunTypeOTPValue(void)
  3584. {
  3585. //struct ChargingInfoData* chargingData_2 = NULL;
  3586. uint8_t Gun1Type = 0;
  3587. uint8_t Gun2Type = 0;
  3588. int i;
  3589. if (pSysConfig->TotalConnectorCount == 1) {
  3590. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(0);
  3591. Gun1Type = pSysConfig->ModelName[7];
  3592. Gun2Type = pSysConfig->ModelName[7];
  3593. switch (Gun1Type) {
  3594. //CHAdeMo
  3595. case 'J':
  3596. SetCHAdeMoTypeJOTPValue(pSysConfig->ModelName[3], &pDcChargingInfo->deratingByConnOtp);
  3597. break;
  3598. case 'K':
  3599. SetCHAdeMoTypeKOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3600. break;
  3601. case 'S':
  3602. SetCHAdeMoTypeSJOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3603. break;
  3604. // 風冷200A以下
  3605. case 'U':
  3606. case 'E':
  3607. case 'M':
  3608. case 'N':
  3609. SetNatural200AGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3610. break;
  3611. // 風冷300A
  3612. case 'T':
  3613. case 'D':
  3614. SetNatural300AGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3615. break;
  3616. // 水冷
  3617. case 'V':
  3618. case 'F':
  3619. case 'P':
  3620. case 'R':
  3621. SetLiquidCoolGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3622. break;
  3623. }
  3624. } else if (pSysConfig->TotalConnectorCount == 2) {
  3625. Gun1Type = pSysConfig->ModelName[7];
  3626. Gun2Type = pSysConfig->ModelName[9];
  3627. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  3628. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(i);
  3629. pDcChargingInfo->deratingByConnOtp.isNeedDerating = GUNOTPDERATING;
  3630. switch (i == 0 ? Gun1Type : Gun2Type) {
  3631. //CHAdeMo
  3632. case 'J':
  3633. SetCHAdeMoTypeJOTPValue(pSysConfig->ModelName[3], &pDcChargingInfo->deratingByConnOtp);
  3634. break;
  3635. case 'K':
  3636. SetCHAdeMoTypeKOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3637. break;
  3638. case 'S':
  3639. SetCHAdeMoTypeSJOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3640. break;
  3641. // 風冷200A以下
  3642. case 'U':
  3643. case 'E':
  3644. case 'M':
  3645. case 'N':
  3646. SetNatural200AGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3647. break;
  3648. // 風冷300A
  3649. case 'T':
  3650. case 'D':
  3651. SetNatural300AGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3652. break;
  3653. // 水冷
  3654. case 'V':
  3655. case 'F':
  3656. case 'P':
  3657. case 'R':
  3658. SetLiquidCoolGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3659. break;
  3660. }
  3661. }
  3662. }
  3663. }
  3664. int main(void)
  3665. {
  3666. bool isModelNameMatch = true;
  3667. uint8_t _ocppProfileChkFlag;
  3668. uint8_t gunIndex = 0;
  3669. if (CreateAllCsuShareMemory() == FAIL) {
  3670. log_error("create share memory error");
  3671. return FAIL;
  3672. }
  3673. ClearAllShmMemParameter();
  3674. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  3675. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  3676. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  3677. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  3678. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  3679. pInfoCode = (struct InfoCodeData *)GetShmInfoCodeData();
  3680. ShmPsuData = (struct PsuData *)GetShmPsuData();
  3681. ShmCHAdeMOData = (struct CHAdeMOData *)GetShmCHAdeMOData();
  3682. ShmGBTData = (struct GBTData *)GetShmGBTData();
  3683. ShmCcsData = (struct CcsData *)GetShmCcsData();
  3684. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  3685. ShmFanModuleData = (struct FanModuleData *)GetShmFanModuleData();
  3686. ShmRelayModuleData = (struct RelayModuleData *)GetShmRelayModuleData();
  3687. ShmLedModuleData = (struct LedModuleData *)GetShmLedModuleData();
  3688. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  3689. ShmOCPP20Data = (struct OCPP20Data*)GetShmOCPP20Data();
  3690. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  3691. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  3692. ShmSysConfigAndInfo = (struct SysConfigAndInfo *)GetShmSysConfigAndInfo();
  3693. ShmStatusCodeData = (struct StatusCodeData *)GetShmStatusCodeData();
  3694. log_info(" ********************************************************");
  3695. log_info(" ****************** FileSystem Boot up *****************");
  3696. log_info(" ********************************************************");
  3697. log_info(" ****************** Project:DD360Tcc *****************");
  3698. log_info(" ********************************************************");
  3699. if (!InitialSystemDefaultConfig()) {
  3700. log_error("InitialSystemDefaultConfig NG ");
  3701. //StopProcessingLoop();
  3702. sleep(5);
  3703. system("reboot -f");
  3704. }
  3705. CheckGunTypeFromHw();
  3706. CheckIsAlternatvieByModelName();
  3707. InitialShareMemoryInfo();
  3708. GetFirmwareVersion();
  3709. if (!CheckConnectorTypeStatus()) {
  3710. isModelNameMatch = false;
  3711. }
  3712. Initialization();
  3713. SpawnTask();
  3714. log_info("Spawned all Task");
  3715. if (!isModelNameMatch) {
  3716. pAlarmCode->AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  3717. //ChangeLcmByIndex(_LCM_ERROR);
  3718. ChangeLcmByIndex(_PAGE_MAINTAIN);
  3719. // Module Name 與硬體對應不正確
  3720. log_error("Module Name & HW info none match. ");
  3721. sleep(3);
  3722. KillAllTask();
  3723. StopProcessingLoop();
  3724. }
  3725. CreateTimeoutFork();
  3726. log_info("Start self test... ");
  3727. SelfTestRun();
  3728. StopSystemTimeoutDet();
  3729. log_info("Self test finished : SelfTestSeq = %d, Work_Step = %d ",
  3730. pSysInfo->SelfTestSeq,
  3731. ShmPsuData->Work_Step);
  3732. if (pSysInfo->SelfTestSeq == _STEST_FAIL || ShmPsuData->Work_Step == _NO_WORKING ||
  3733. pInfoCode->InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES ) {
  3734. ChangeLcmByIndex(_PAGE_MAINTAIN);
  3735. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3736. setChargerMode(gunIndex, MODE_MAINTAIN);
  3737. }
  3738. } else {
  3739. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3740. setChargerMode(gunIndex, MODE_IDLE);
  3741. }
  3742. }
  3743. // Local DB
  3744. if (DB_Open() != PASS) {
  3745. log_info("DB_Open fail. ");
  3746. isDb_ready = false;
  3747. } else {
  3748. isDb_ready = true;
  3749. for (int _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  3750. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  3751. pDcChargingInfo->IsAvailable = DB_Get_Operactive(_index);
  3752. }
  3753. DB_Reboot_Record();
  3754. }
  3755. if (PowerDB_Open() != PASS) {
  3756. log_info("Power DB_Open fail. ");
  3757. isDb_ready = false;
  3758. } else {
  3759. int _retry = 0;
  3760. isDb_ready = true;
  3761. for (int _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  3762. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(_index);
  3763. while (_retry < 3) {
  3764. if (DB_Get_PowerConsumption(_index)) {
  3765. break;
  3766. } else {
  3767. sleep(1);
  3768. _retry++;
  3769. if (_retry == 3) {
  3770. DB_Insert_PowerConsumption(_index, 0);
  3771. }
  3772. }
  3773. } // while
  3774. } // for
  3775. }
  3776. log_info("===== Create DB End ===== ");
  3777. ChangeLcmByIndex(_PAGE_IDLE);
  3778. sleep(1);
  3779. //***** 須新增的偵測 *****//
  3780. // 1. Thernal - 控制風扇轉速
  3781. // 2. ouput fuse - 控制風扇轉速
  3782. CreateRfidFork();
  3783. // Main loop
  3784. CheckFwSlotStatusLog();
  3785. //AdjustChargerCurrent();
  3786. GetClockTime(&_cmdMainPriority_time, NULL);
  3787. // GunIndexInfo *pGunIndexInfo = (GunIndexInfo *)GetGunIndexInfo();
  3788. CheckTaskAlive();
  3789. CreateWatchdog();
  3790. //SetGunTypeOTPValue();
  3791. showversion();
  3792. for (;;) {
  3793. CheckOcppStatus();
  3794. ChkPrimaryStatus();
  3795. if ((IsConnectorWholeIdle() ||
  3796. //(pSysInfo->PageIndex == _LCM_ERROR)) &&
  3797. (pSysInfo->PageIndex == _PAGE_MAINTAIN)) &&
  3798. (pSysInfo->SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  3799. ) {
  3800. CheckFactoryConfigFunction();
  3801. //CheckFwUpdateFunction();
  3802. }
  3803. // OCPP 邏輯
  3804. OcppRemoteStartChk();
  3805. // 當 AC 沒有搭上時,清除一些錯誤碼
  3806. ClearAlarmCodeWhenAcOff();
  3807. if ((GetClockTimeoutValue(_cmdMainPriority_time) / 1000) > 5000) {
  3808. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3809. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3810. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  3811. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  3812. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3813. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  3814. if (pDcChargingInfo->SystemStatus == S_CHARGING &&
  3815. _ocppProfileChkFlag == 12) {
  3816. ChargingProfileFlat(gunIndex);
  3817. _ocppProfileChkFlag = 0;
  3818. } else if (pDcChargingInfo->SystemStatus != S_CHARGING) {
  3819. ChargingProfileFlat(gunIndex);
  3820. _ocppProfileChkFlag = 0;
  3821. } else {
  3822. _ocppProfileChkFlag++;
  3823. }
  3824. }
  3825. checkGunOTPState(gunIndex); //check gun OTP
  3826. }
  3827. GetClockTime(&_cmdMainPriority_time, NULL);
  3828. }
  3829. gEvBoardErr.GunErrMessage = 0; //清除系統執行中的錯誤訊息
  3830. gChillerTempErr.TempErrMsg = 0;//清除系統執行中的錯誤訊息
  3831. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3832. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3833. // 重新收集各槍的錯誤狀態
  3834. collectError(gunIndex);
  3835. checkEvBoardAlarmState(pDcChargingInfo->Type);
  3836. }
  3837. checkChillerAlarmState();
  3838. // 確認當前錯誤 Level = 2 ?
  3839. ReviewCriticalAlarm();
  3840. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3841. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3842. CheckGpioInStatus();
  3843. CheckErrorOccurStatus(gunIndex);
  3844. // 確認 Relay Welding or Driving Fault
  3845. CheckRelayWeldingOrDrivingFault(gunIndex);
  3846. ChkOcppStatus(gunIndex);
  3847. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  3848. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  3849. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3850. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  3851. CheckSmartChargeProfile(gunIndex);
  3852. }
  3853. switch (pDcChargingInfo->SystemStatus) {
  3854. case S_IDLE:
  3855. if (isModeChange(gunIndex)) {
  3856. log_info("============================= S_IDLE(%x) ============================= ", gunIndex);
  3857. ResetIdleData(gunIndex);
  3858. }
  3859. isChargingAverageState();
  3860. // For RemoteStart Using
  3861. if (pDcChargingInfo->RemoteStartFlag == YES && pDcChargingInfo->IsAvailable) {
  3862. log_info("-------- IDLE Remote Start(%d) --------", gunIndex);
  3863. //ChangeGunSelectByIndex(gunIndex);
  3864. setChargerMode(gunIndex, MODE_AUTHORIZING);
  3865. break;
  3866. }
  3867. autoStartCharging(gunIndex);
  3868. // 讀卡邏輯
  3869. if (!ShmDcCommonData->TradeCancel && pSysInfo->CurGunSelected == gunIndex)
  3870. ScannerCardProcess(gunIndex);
  3871. if (pSysInfo->SystemPage == _PAGE_SELECT_GUN || pSysInfo->SystemPage == _PAGE_IDLE) {
  3872. //ShmDcCommonData->LineStatus[gunIndex] = 0;
  3873. StopGunInfoTimeoutDet(gunIndex);
  3874. }
  3875. if (pSysInfo->CurGunSelected == gunIndex) {
  3876. if (pSysInfo->SystemPage == _PAGE_BILL) {
  3877. StartSystemTimeoutDet(Timeout_AddLine);
  3878. if (/*ShmDcCommonData->LineStatus[gunIndex] == 1 ||*/ ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_ADD_FRIEND) {
  3879. pSysInfo->SystemPage = _PAGE_ADD_FRIEND;
  3880. log_info("Change to Add Line Friend Page");
  3881. }
  3882. } else if (pSysInfo->SystemPage == _PAGE_ADD_FRIEND) {
  3883. StopSystemTimeoutDet();
  3884. StartGunInfoTimeoutDet(gunIndex, Timeout_LineReigster);
  3885. if (ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_CREDITCARD_INVOICE) {
  3886. StopGunInfoTimeoutDet(gunIndex);
  3887. pSysInfo->SystemPage = _PAGE_SELECT_PAY;
  3888. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = FALSE;
  3889. log_info("Change to Select Pay Page");
  3890. }
  3891. if (ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_CREDITCARD_DONATE) {
  3892. StopGunInfoTimeoutDet(gunIndex);
  3893. pSysInfo->SystemPage = _PAGE_SELECT_PAY;
  3894. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = TRUE;
  3895. log_info("Change to Select Pay Page");
  3896. }
  3897. }
  3898. }
  3899. // 檢查預約
  3900. if (ShmDcCommonData->pGunInfo[gunIndex].ReservationStatus) {
  3901. setChargerMode(gunIndex, MODE_RESERVATION);
  3902. }
  3903. goto CheckStatus;
  3904. break;
  3905. case S_RESERVATION:
  3906. if (isModeChange(gunIndex)) {
  3907. log_info("============================= S_RESERVATION(%x) ============================= ", gunIndex);
  3908. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  3909. }
  3910. if (pDcChargingInfo->RemoteStartFlag == YES && pDcChargingInfo->IsAvailable) {
  3911. log_info("-------- S_RESERVATION Remote Start(%d) --------", gunIndex);
  3912. //ChangeGunSelectByIndex(gunIndex);
  3913. setChargerMode(gunIndex, MODE_AUTHORIZING);
  3914. break;
  3915. }
  3916. // 讀卡邏輯
  3917. if (!ShmDcCommonData->TradeCancel && pSysInfo->CurGunSelected == gunIndex)
  3918. ScannerCardProcess(gunIndex);
  3919. // 檢查預約
  3920. if (!ShmDcCommonData->pGunInfo[gunIndex].ReservationStatus) {
  3921. setChargerMode(gunIndex, MODE_IDLE);
  3922. }
  3923. goto CheckStatus;
  3924. break;
  3925. case S_FAULT:
  3926. if (isModeChange(gunIndex)) {
  3927. if (pDcChargingInfo->SystemStatus == S_FAULT) {
  3928. log_info("============================= S_FAULT(%x) ============================= ", gunIndex);
  3929. //pSysInfo->SystemPage = _LCM_ERROR;
  3930. //pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3931. }
  3932. }
  3933. CheckStatus:
  3934. if (pDcChargingInfo->IsAvailable == NO) {
  3935. setChargerMode(gunIndex, MODE_MAINTAIN);
  3936. break;
  3937. }
  3938. // Refresh Error Status
  3939. if (pSysWarning->Level == WARN_LV_ER) {
  3940. CheckErrorCode(gunIndex);
  3941. if (pSysInfo->CurGunSelected == gunIndex)
  3942. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3943. continue;
  3944. }
  3945. if (PrecheckIsPass(gunIndex) == false) {
  3946. log_error("Relay welding");
  3947. setChargerMode(gunIndex, MODE_FAULT);
  3948. if (gunIndex == pSysInfo->CurGunSelected) {
  3949. //pSysInfo->SystemPage = _LCM_ERROR;
  3950. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3951. }
  3952. break;
  3953. }
  3954. if (pDcChargingInfo->SystemStatus == S_FAULT) {
  3955. setChargerMode(gunIndex, MODE_IDLE);
  3956. systemPageRestoreInit();
  3957. }
  3958. break;
  3959. case S_AUTHORIZING:
  3960. if (isModeChange(gunIndex)) {
  3961. log_info("============================= S_AUTHORIZING(%x) ============================= ", gunIndex);
  3962. StartGunInfoTimeoutDet(gunIndex, Timeout_AuthorizingForStop);
  3963. }
  3964. if (pSysInfo->CurGunSelected == gunIndex)
  3965. ScannerCardProcess(gunIndex);
  3966. if (pSysInfo->SystemPage == _PAGE_PLUGIN && ShmDcCommonData->TradeCancel == FALSE) {
  3967. pDcChargingInfo->Replug_flag = TRUE;
  3968. StopSystemTimeoutDet();
  3969. }
  3970. if (isDetectPlugin(gunIndex)) {
  3971. StartGunInfoTimeoutDet(gunIndex, Timeout_WaitPlug);
  3972. // 卡號驗證成功後,等待充電槍插入充電車
  3973. if (pDcChargingInfo->RemoteStartFlag == YES) {
  3974. if (pDcChargingInfo->ConnectorPlugIn == YES &&
  3975. pDcChargingInfo->IsAvailable ) {
  3976. log_info("-------- S_AUTHORIZING Remote Start(%d) --------", gunIndex);
  3977. pDcChargingInfo->RemoteStartFlag = NO;
  3978. pDcChargingInfo->isRemoteStart = YES; //DS60-120
  3979. //ChangeGunSelectByIndex(gunIndex);
  3980. //AddPlugInTimes(gunIndex);
  3981. setChargerMode(gunIndex, MODE_REASSIGN_CHECK);
  3982. strcpy((char *)pDcChargingInfo->StartUserId, "");
  3983. ClearDetectPluginFlag(gunIndex);
  3984. continue;
  3985. }
  3986. } else if (pSysInfo->OrderCharging == NO_DEFINE) {
  3987. if (pDcChargingInfo->ConnectorPlugIn == YES &&
  3988. pDcChargingInfo->IsAvailable &&
  3989. pDcChargingInfo->SystemStatus == S_AUTHORIZING &&
  3990. ShmDcCommonData->AuthPass_flag[gunIndex] == TRUE &&
  3991. (waitRightGunPlugIt(gunIndex) == PASS ||
  3992. waitLeftGunPlugIt(gunIndex) == PASS)
  3993. ) {
  3994. log_info("-------- RFID/Auto Start(%d) --------", gunIndex);
  3995. //ChangeGunSelectByIndex(gunIndex);
  3996. //AddPlugInTimes(gunIndex);
  3997. pDcChargingInfo->isRemoteStart = NO;
  3998. strcpy((char *)pDcChargingInfo->StartUserId, (char *)pSysConfig->UserId);
  3999. log_info("index = %d, CardNumber = %s ",
  4000. gunIndex,
  4001. pDcChargingInfo->StartUserId);
  4002. strcpy((char *)pSysConfig->UserId, "");
  4003. // 當前操作的槍號,進入 Preparing
  4004. setChargerMode(gunIndex, MODE_REASSIGN_CHECK);
  4005. ClearDetectPluginFlag(gunIndex);
  4006. continue;
  4007. }
  4008. }
  4009. if (!GetIsCardScan() && pSysInfo->CurGunSelected == gunIndex &&
  4010. pSysInfo->SystemPage != _PAGE_AUTHORIZE_FAIL &&
  4011. pSysInfo->SystemPage != _PAGE_SENSING) {
  4012. pSysInfo->SystemPage = _PAGE_PLUGIN;
  4013. } else if (!GetIsCardScan() && pSysInfo->SystemPage == _PAGE_IDLE &&
  4014. pDcChargingInfo->RemoteStartFlag == YES) {
  4015. pSysInfo->CurGunSelected = gunIndex;
  4016. }
  4017. } else if (ShmDcCommonData->AuthPass_flag[gunIndex] && pSysInfo->CurGunSelected == gunIndex &&
  4018. pSysInfo->SystemPage != _PAGE_AUTHORIZE_FAIL) {
  4019. pSysInfo->SystemPage = _PAGE_SENSING;
  4020. }
  4021. break;
  4022. case S_REASSIGN_CHECK: {
  4023. if (isModeChange(gunIndex)) {
  4024. log_info("============================= S_REASSIGN_CHECK(%x) ============================= ", gunIndex);
  4025. if (pSysInfo->OrderCharging != NO_DEFINE) {
  4026. pSysInfo->OrderCharging = NO_DEFINE;
  4027. pDcChargingInfo->_SaftyDetect = FALSE;
  4028. }
  4029. pDcChargingInfo->Replug_flag = TRUE;
  4030. StopSystemTimeoutDet();
  4031. StopGunInfoTimeoutDet(gunIndex);
  4032. }
  4033. setChargerMode(gunIndex, S_PREPARNING);
  4034. if (pSysInfo->CurGunSelected == gunIndex) {
  4035. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4036. }
  4037. }
  4038. break;
  4039. case S_PREPARNING:
  4040. if (isModeChange(gunIndex)) {
  4041. log_info("============================= S_PREPARNING(%x) ============================= ", gunIndex);
  4042. StopGunInfoTimeoutDet(gunIndex);
  4043. StartGunInfoTimeoutDet(gunIndex, Timeout_Preparing);
  4044. }
  4045. if (ShmPsuData->SystemPresentPsuQuantity > 0 &&
  4046. ShmPsuData->SystemAvailablePower > 10 &&
  4047. GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) >= 1000000) {
  4048. setChargerMode(gunIndex, MODE_PREPARE_FOR_EV);
  4049. }
  4050. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4051. if (pSysInfo->CurGunSelected == gunIndex) {
  4052. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4053. }
  4054. break;
  4055. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  4056. if (isModeChange(gunIndex)) {
  4057. log_info("============================= S_PREPARING_FOR_EV(%x) ============================= ", gunIndex);
  4058. log_info("Waitting for Gun locked.");
  4059. StopGunInfoTimeoutDet(gunIndex);
  4060. StartGunInfoTimeoutDet(gunIndex, Timeout_EvChargingDet);
  4061. }
  4062. if (pDcChargingInfo->Type == _Type_Chademo) {
  4063. if (isEvGunLocked_chademo(gunIndex) == YES) {
  4064. log_info("Gun Locked");
  4065. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  4066. }
  4067. } else if (pDcChargingInfo->Type == _Type_GB) {
  4068. if (isEvGunLocked_gb(gunIndex) == YES) {
  4069. log_info("Gun Locked");
  4070. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  4071. }
  4072. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  4073. if (isEvGunLocked_ccs(gunIndex) == YES) {
  4074. log_info("Gun Locked");
  4075. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  4076. }
  4077. }
  4078. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4079. if (pSysInfo->CurGunSelected == gunIndex) {
  4080. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4081. }
  4082. break;
  4083. case S_PREPARING_FOR_EVSE: // Ground fault test, 等待 Relay Board 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  4084. if (isModeChange(gunIndex)) {
  4085. log_info("============================= S_PREPARING_FOR_EVSE(%x) ============================= ", gunIndex);
  4086. StopGunInfoTimeoutDet(gunIndex);
  4087. StartGunInfoTimeoutDet(gunIndex, Timeout_EvseChargingDet);
  4088. }
  4089. checkPileEndGfdResult(gunIndex, pDcChargingInfo->Type, pDcChargingInfo->SystemStatus);
  4090. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4091. if (pSysInfo->CurGunSelected == gunIndex) {
  4092. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4093. }
  4094. break;
  4095. case S_CCS_PRECHARGE_ST0:
  4096. if (isModeChange(gunIndex)) {
  4097. log_info("============================= CCS Precharge Processing 1(%x) ============================= ", gunIndex);
  4098. StopGunInfoTimeoutDet(gunIndex);
  4099. StartGunInfoTimeoutDet(gunIndex, Timeout_ForCcsPrechargeDet);
  4100. }
  4101. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  4102. // GFD 錯誤停止
  4103. RecordAlarmCode(gunIndex, "012235");
  4104. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  4105. }
  4106. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4107. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  4108. // 切換 D+ Relay to Precharge Relay
  4109. if (isPrechargeStatus_ccs(gunIndex) == 39 ||
  4110. isPrechargeStatus_ccs(gunIndex) == 40) {
  4111. if ((pDcChargingInfo->RelayKPK2Status == YES || pDcChargingInfo->PantographFlag == YES) &&
  4112. pDcChargingInfo->PrechargeStatus != PRECHARGE_READY)
  4113. //if (pDcChargingInfo->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  4114. {
  4115. pDcChargingInfo->PrechargeStatus = PRECHARGE_READY;
  4116. }
  4117. } else if (isPrechargeStatus_ccs(gunIndex) == 45 ||
  4118. isPrechargeStatus_ccs(gunIndex) == 46
  4119. ) {
  4120. setChargerMode(gunIndex, MODE_CCS_PRECHARGE_STEP1);
  4121. }
  4122. if (pSysInfo->CurGunSelected == gunIndex) {
  4123. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4124. }
  4125. break;
  4126. case S_CCS_PRECHARGE_ST1:
  4127. if (isModeChange(gunIndex)) {
  4128. log_info("============================= CCS Precharge Processing 2(%x) ============================= ", gunIndex);
  4129. }
  4130. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  4131. // GFD 錯誤停止
  4132. RecordAlarmCode(gunIndex, "012235");
  4133. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  4134. }
  4135. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4136. // 等待小板通知進入充電
  4137. // 切換 D+ Relay to Precharge Relay
  4138. if (pDcChargingInfo->RelayK1K2Status == YES || pDcChargingInfo->PantographFlag == YES) {
  4139. pDcChargingInfo->PrechargeStatus = PRECHARGE_READY;
  4140. // pSysInfo->CurGunSelected = gunIndex;
  4141. setChargerMode(gunIndex, MODE_CHARGING);
  4142. }
  4143. if (pSysInfo->CurGunSelected == gunIndex) {
  4144. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4145. }
  4146. break;
  4147. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  4148. if (isModeChange(gunIndex)) {
  4149. log_info("============================= S_CHARGING(%x) ============================= ", gunIndex);
  4150. StopGunInfoTimeoutDet(gunIndex);
  4151. ftime(&startChargingTime[gunIndex]);
  4152. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].ResponseIdTagInfo.Status, ""); //DS60-120 add
  4153. ChangeStartOrStopDateTime(YES, gunIndex);
  4154. pDcChargingInfo->Replug_flag = FALSE;
  4155. ShmDcCommonData->finalcost_flag[gunIndex] = FALSE;
  4156. ShmDcCommonData->TransactionInfo[gunIndex].isIntoCharge = TRUE;
  4157. ShmDcCommonData->RecordEnergyTime[gunIndex] = time((time_t*)NULL);
  4158. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  4159. }
  4160. if (ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionConf) {
  4161. ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionConf = NO;
  4162. }
  4163. ftime(&endChargingTime[gunIndex]);
  4164. pDcChargingInfo->PresentChargedDuration = DiffTimeb(startChargingTime[gunIndex], endChargingTime[gunIndex]);
  4165. ShmDcCommonData->TransactionInfo[gunIndex].Energy = pDcChargingInfo->PresentChargedEnergy;
  4166. // 每秒紀錄使用電量到資料庫內
  4167. if ((time((time_t*)NULL) - ShmDcCommonData->RecordEnergyTime[gunIndex]) >= 1) {
  4168. ShmDcCommonData->RecordEnergyTime[gunIndex] = time((time_t*)NULL);
  4169. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  4170. }
  4171. if ((time((time_t*)NULL) - ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime) >= 5) {
  4172. ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime = time((time_t*)NULL);
  4173. if (ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption != 0)
  4174. DB_Update_PowerConsumption(gunIndex, ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption);
  4175. }
  4176. checkPileEndGfdResult(gunIndex, pDcChargingInfo->Type, pDcChargingInfo->SystemStatus);
  4177. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4178. // Check Stop Charging
  4179. AuthorizeStopCharging(gunIndex);
  4180. if (pSysInfo->SystemPage == _PAGE_STOP_CONFIRM_LEFT ||
  4181. pSysInfo->SystemPage == _PAGE_STOP_CONFIRM_RIGHT)
  4182. break;
  4183. // LCM => Charging
  4184. if (pSysInfo->CurGunSelected == gunIndex && pSysInfo->SystemPage != _PAGE_IDLE &&
  4185. pSysInfo->SystemPage != _PAGE_SELECT_GUN) {
  4186. pSysInfo->SystemPage = _PAGE_CHARGING;
  4187. }
  4188. break;
  4189. case S_TERMINATING:
  4190. if (isModeChange(gunIndex)) {
  4191. log_info("============================= S_TERMINATING(%x) ============================= ", gunIndex);
  4192. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  4193. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Local");
  4194. }
  4195. ShmDcCommonData->StopCharge[gunIndex] = TRUE;
  4196. if (ShmDcCommonData->is_AutoStart[gunIndex]) {
  4197. ShmDcCommonData->finalcost_flag[gunIndex] = TRUE;
  4198. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  4199. }
  4200. if (ShmDcCommonData->finalcost_flag[gunIndex] == FALSE && pSysInfo->CurGunSelected == gunIndex) {
  4201. pSysInfo->SystemPage = _PAGE_PAYING;
  4202. }
  4203. StartSystemTimeoutDet(Timeout_Terminating);
  4204. StartGunInfoTimeoutDet(gunIndex, Timeout_FinalCost);
  4205. ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime = time((time_t*)NULL);
  4206. if (ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption != 0)
  4207. DB_Update_PowerConsumption(gunIndex, ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption);
  4208. }
  4209. // For Precharging timeout
  4210. if (pDcChargingInfo->Type == _Type_Chademo) {
  4211. if (isEvStopCharging_chademo(gunIndex) == YES ||
  4212. isPrechargeStatus_chademo(gunIndex) <= 0) {
  4213. setChargerMode(gunIndex, MODE_COMPLETE);
  4214. }
  4215. } else if (pDcChargingInfo->Type == _Type_GB) {
  4216. if (isEvStopCharging_gb(gunIndex) == YES ||
  4217. isPrechargeStatus_gb(gunIndex) <= 0) {
  4218. setChargerMode(gunIndex, MODE_COMPLETE);
  4219. }
  4220. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  4221. if (isEvStopCharging_ccs(gunIndex) == YES &&
  4222. (isPrechargeStatus_ccs(gunIndex) >= 53 ||
  4223. isPrechargeStatus_ccs(gunIndex) == 0 ||
  4224. isPrechargeStatus_ccs(gunIndex) == 13 ||
  4225. isPrechargeStatus_ccs(gunIndex) == 14)
  4226. ) {
  4227. setChargerMode(gunIndex, MODE_COMPLETE);
  4228. }
  4229. }
  4230. if (pDcChargingInfo->Replug_flag == TRUE && pSysInfo->CurGunSelected == gunIndex) {
  4231. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  4232. break;
  4233. }
  4234. if (pSysInfo->CurGunSelected == gunIndex &&
  4235. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4236. if (ShmDcCommonData->PayPass_flag[gunIndex] == TRUE)
  4237. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4238. else
  4239. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  4240. }
  4241. break;
  4242. case S_COMPLETE:
  4243. if (isModeChange(gunIndex)) {
  4244. log_info ("============================= S_COMPLETE(%x) ============================= ", gunIndex);
  4245. if (strcmp((char *)pDcChargingInfo->StartDateTime, "") != EQUAL) {
  4246. OcppStopTransation(gunIndex);
  4247. }
  4248. TheEndCharging(gunIndex);
  4249. if (pDcChargingInfo->Replug_flag != TRUE && pSysInfo->SystemPage != _PAGE_PLUGOUT)
  4250. StopSystemTimeoutDet();
  4251. if (ShmDcCommonData->is_AutoStart[gunIndex] == TRUE && pSysInfo->CurGunSelected == gunIndex)
  4252. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4253. }
  4254. //if (pSysInfo->SystemPage == _LCM_ERROR) {
  4255. if (pSysInfo->SystemPage == _PAGE_MAINTAIN) {
  4256. break;
  4257. }
  4258. if (pSysInfo->CurGunSelected == gunIndex && ShmDcCommonData->is_exit[gunIndex]) {
  4259. pSysInfo->SystemPage = _PAGE_EXIT;
  4260. break;
  4261. }
  4262. if (pDcChargingInfo->ConnectorPlugIn == NO &&
  4263. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4264. if (pSysInfo->CurGunSelected == gunIndex) {
  4265. StartGunInfoTimeoutDet(gunIndex, Timeout_ExitPage);
  4266. }
  4267. if (pDcChargingInfo->TimeoutFlag != Timeout_ExitPage &&
  4268. pDcChargingInfo->TimeoutFlag != Timeout_FinalCost) {
  4269. StartGunInfoTimeoutDet(gunIndex, Timeout_CompletPlugout);
  4270. }
  4271. }
  4272. if (pDcChargingInfo->Replug_flag == TRUE && pSysInfo->CurGunSelected == gunIndex) {
  4273. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  4274. break;
  4275. }
  4276. if (pSysInfo->CurGunSelected == gunIndex &&
  4277. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4278. if (ShmDcCommonData->PayPass_flag[gunIndex] == TRUE)
  4279. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4280. else
  4281. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  4282. }
  4283. break;
  4284. case S_ALARM:
  4285. if (isModeChange(gunIndex)) {
  4286. log_info("============================= S_ALARM(%x) ============================= ", gunIndex);
  4287. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  4288. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Local");
  4289. }
  4290. UpdateErrorCodeToOcpp(gunIndex);
  4291. if (strcmp((char *)pDcChargingInfo->StartDateTime, "") != EQUAL) {
  4292. OcppStopTransation(gunIndex);
  4293. }
  4294. TheEndCharging(gunIndex);
  4295. StopGunInfoTimeoutDet(gunIndex);
  4296. ShmDcCommonData->StopCharge[gunIndex] = TRUE;
  4297. if (ShmDcCommonData->is_AutoStart[gunIndex]) {
  4298. ShmDcCommonData->finalcost_flag[gunIndex] = TRUE;
  4299. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  4300. }
  4301. if (pDcChargingInfo->Replug_flag == TRUE) {
  4302. } else {
  4303. StartGunInfoTimeoutDet(gunIndex, Timeout_FinalCost);
  4304. if(ShmDcCommonData->finalcost_flag[gunIndex] == FALSE && pSysInfo->CurGunSelected) {
  4305. pSysInfo->SystemPage = _PAGE_PAYING;
  4306. }
  4307. }
  4308. ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime = time((time_t*)NULL);
  4309. if (ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption != 0)
  4310. DB_Update_PowerConsumption(gunIndex, ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption);
  4311. }
  4312. if (pSysWarning->Level == WARN_LV_ER) {
  4313. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4314. continue;
  4315. }
  4316. if (pSysInfo->CurGunSelected == gunIndex && ShmDcCommonData->is_exit[gunIndex]) {
  4317. pSysInfo->SystemPage = _PAGE_EXIT;
  4318. break;
  4319. }
  4320. if (pDcChargingInfo->ConnectorPlugIn == NO &&
  4321. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4322. if (pSysInfo->CurGunSelected == gunIndex) {
  4323. StartGunInfoTimeoutDet(gunIndex, Timeout_ExitPage);
  4324. }
  4325. if (pDcChargingInfo->TimeoutFlag != Timeout_ExitPage &&
  4326. pDcChargingInfo->TimeoutFlag != Timeout_FinalCost) {
  4327. StartGunInfoTimeoutDet(gunIndex, Timeout_CompletPlugout);
  4328. }
  4329. }
  4330. if (pDcChargingInfo->Replug_flag == TRUE && pSysInfo->CurGunSelected == gunIndex) {
  4331. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  4332. break;
  4333. }
  4334. if (pSysInfo->CurGunSelected == gunIndex && pSysInfo->SystemPage != _PAGE_PAYING ) {
  4335. if (ShmDcCommonData->PayPass_flag[gunIndex] == TRUE)
  4336. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4337. else
  4338. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  4339. }
  4340. break;
  4341. case S_MAINTAIN:
  4342. if (isModeChange(gunIndex)) {
  4343. log_info("============================= S_MAINTAIN(%x) ============================= ", gunIndex);
  4344. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4345. if (pSysInfo->FirmwareUpdate == YES) {
  4346. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4347. continue;
  4348. }
  4349. if (pDcChargingInfo->IsAvailable == NO) {
  4350. log_info("Gun%d is not availbale");
  4351. break;
  4352. }
  4353. if (pSysWarning->Level != WARN_LV_ER) {
  4354. if (!DisplaySelfTestFailReason()) { //DS60-120 add
  4355. log_info("Soft reboot for retry self-tets. ");
  4356. sleep(3);
  4357. system("killall OcppBackend &");
  4358. KillAllTask();
  4359. system("/usr/bin/run_evse_restart.sh");
  4360. }
  4361. }
  4362. sleep(3);
  4363. if (pSysWarning->Level == WARN_LV_ER) { //DS60-120 add
  4364. KillTaskExceptPrimary();
  4365. } else {
  4366. KillTask();
  4367. }
  4368. /*
  4369. if (pSysInfo->SelfTestSeq == _STEST_FAIL)
  4370. StopProcessingLoop();
  4371. */
  4372. }
  4373. if (pSysInfo->SelfTestSeq == _STEST_FAIL && pSysInfo->SystemTimeoutFlag != Timeout_SelftestChk) {
  4374. SelfTestRun();
  4375. StopSystemTimeoutDet();
  4376. sleep(3);
  4377. break;
  4378. }
  4379. if (pDcChargingInfo->IsAvailable && pSysInfo->SelfTestSeq == _STEST_COMPLETE) {
  4380. setChargerMode(gunIndex, MODE_IDLE);
  4381. }
  4382. if (pSysInfo->CurGunSelected == gunIndex) {
  4383. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4384. }
  4385. break;
  4386. case S_UPDATE:
  4387. if (isModeChange(gunIndex)) {
  4388. log_info("============================= S_UPDATE ============================= ");
  4389. }
  4390. if (pSysInfo->FirmwareUpdate == YES) {
  4391. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4392. continue;
  4393. } else {
  4394. }
  4395. break;
  4396. }//switch
  4397. }//for
  4398. //if (pSysInfo->SystemPage != _LCM_VIEW)
  4399. //ChangeLcmByIndex(pSysInfo->SystemPage);
  4400. TryFeedWatchdog();
  4401. usleep(WHILE_LOOP_TIME);
  4402. }
  4403. return FAIL;
  4404. }