main.c 192 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014
  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.17.00.0000.00"; // Phihong version
  75. char* DebugVersion = "V2.17.01"; // 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. } else if (pInfoCode->InfoEvents.bits.BackendDisconnectedViaEthernet == YES) {
  1780. memcpy(pDcChargingInfo->ConnectorAlarmCode, "033900", 6);
  1781. }
  1782. //Chiller temperature
  1783. //else if (pAlarmCode->AlarmEvents.bits.SystemChillerOTP == YES) {
  1784. // memcpy(pDcChargingInfo->ConnectorAlarmCode, "012323", 6);
  1785. //} else if (pFaultCode->FaultEvents.bits.ChillerTempSensorBroken == YES) {
  1786. // memcpy(pDcChargingInfo->ConnectorAlarmCode, "011038", 6);
  1787. //}
  1788. }
  1789. /*
  1790. if (pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES ||
  1791. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES ||
  1792. pAlarmCode->AlarmEvents.bits.DoorOpen == YES ||
  1793. pSysWarning->ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  1794. pAlarmCode->AlarmEvents.bits.PsuFailureAlarm == YES ||
  1795. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES ||
  1796. //Power cabinet alarm status
  1797. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop == YES ||
  1798. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen == YES ||
  1799. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP == YES ||
  1800. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputUVP == YES ||
  1801. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP == YES ||
  1802. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP == YES ||
  1803. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP == YES ||
  1804. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure == YES
  1805. ) {
  1806. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1807. pDcChargingInfo->SystemStatus < S_COMPLETE) ||
  1808. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1809. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1 ))
  1810. ChargingAlarmProcess(index);
  1811. pSysWarning->Level = WARN_LV_ER;
  1812. }*/
  1813. }
  1814. //===============================================
  1815. // 確認 GPIO 狀態
  1816. //===============================================
  1817. void gpio_set_value(unsigned int gpio, unsigned int value)
  1818. {
  1819. int fd;
  1820. char buf[MAX_BUF];
  1821. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1822. fd = open(buf, O_WRONLY);
  1823. if (fd < 0) {
  1824. perror("gpio/set-value");
  1825. return;
  1826. }
  1827. if (value) {
  1828. write(fd, "1", 2);
  1829. } else {
  1830. write(fd, "0", 2);
  1831. }
  1832. close(fd);
  1833. }
  1834. int gpio_get_value(unsigned int gpio, unsigned int *value)
  1835. {
  1836. int fd;
  1837. char buf[MAX_BUF];
  1838. char ch;
  1839. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1840. fd = open(buf, O_RDONLY);
  1841. if (fd < 0) {
  1842. perror("gpio/get-value");
  1843. return fd;
  1844. }
  1845. read(fd, &ch, 1);
  1846. if (ch != '0') {
  1847. *value = 1;
  1848. } else {
  1849. *value = 0;
  1850. }
  1851. close(fd);
  1852. return 0;
  1853. }
  1854. void CheckGunTypeFromHw()
  1855. {
  1856. uint8_t i = 0;
  1857. int pinIn[4] = {22, 23, 44, 45};
  1858. unsigned int gpioValue = 0;
  1859. uint8_t tmp[2] = {0};
  1860. log_info("ModelName = %s", pSysConfig->ModelName);
  1861. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1862. gpio_get_value(pinIn[i], &gpioValue);
  1863. switch (pinIn[i]) {
  1864. //right slot
  1865. case 22:
  1866. bd1_1_status = gpioValue;
  1867. break;
  1868. case 23:
  1869. bd1_2_status = gpioValue;
  1870. break;
  1871. //left slot
  1872. case 44:
  1873. bd0_1_status = gpioValue;
  1874. break;
  1875. case 45:
  1876. bd0_2_status = gpioValue;
  1877. break;
  1878. }
  1879. }
  1880. //BD1(Left-CCS-CND1-SMR2-左槍), BD2(Right-CHADEMO-CND2-SMR1-右槍), CCS: 10 , CHAdeMO: 01 , GBT: 11
  1881. //CcsChargingData [0至1] 分別為 Right至Left
  1882. //model name 槍順序左至右分別為Right至Left
  1883. tmp[1] = (bd0_1_status << 4 | bd0_2_status);
  1884. tmp[0] = (bd1_1_status << 4 | bd1_2_status);
  1885. for (i = 0; i < 2; i++) {
  1886. switch (tmp[i]) {
  1887. case 0x01:
  1888. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "CHAdeMO");
  1889. break;
  1890. case 0x10:
  1891. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "CCS");
  1892. break;
  1893. case 0x11:
  1894. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "GBT");
  1895. break;
  1896. case 0x00:
  1897. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "None");
  1898. break;
  1899. }
  1900. }
  1901. }
  1902. void CheckGpioInStatus()
  1903. {
  1904. int i = 0;
  1905. int pinIn[2] = { 27, 47 };//{IO BD1_2, IO BD2_2}
  1906. unsigned int gpioValue = 0;
  1907. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1908. gpio_get_value(pinIn[i], &gpioValue);
  1909. if (gpioValue == 0x01) {
  1910. switch (pinIn[i]) {
  1911. // 小板緊急停止
  1912. case 47:
  1913. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1914. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1915. if (pDcChargingInfo->slotsIndex == 1) {
  1916. if (pDcChargingInfo->Type == _Type_Chademo) {
  1917. BoardErrOccurByString(i, "023730");
  1918. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1919. BoardErrOccurByString(i, "013627");
  1920. }
  1921. break;
  1922. }
  1923. }
  1924. break;
  1925. case 27:
  1926. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1927. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1928. if (pDcChargingInfo->slotsIndex == 3) {
  1929. if (pDcChargingInfo->Type == _Type_Chademo) {
  1930. BoardErrOccurByString(i, "023730");
  1931. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1932. BoardErrOccurByString(i, "013627");
  1933. }
  1934. break;
  1935. }
  1936. }
  1937. break;
  1938. }
  1939. } else {
  1940. switch (pinIn[i]) {
  1941. // 小板解除緊急停止
  1942. case 47:
  1943. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1944. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1945. if (pDcChargingInfo->slotsIndex == 1) {
  1946. if (pDcChargingInfo->Type == _Type_Chademo) {
  1947. ReleaseBoardErrOccurByString(i, "023730");
  1948. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1949. ReleaseBoardErrOccurByString(i, "013627");
  1950. }
  1951. break;
  1952. }
  1953. }
  1954. break;
  1955. case 27:
  1956. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1957. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1958. if (pDcChargingInfo->slotsIndex == 3) {
  1959. if (pDcChargingInfo->Type == _Type_Chademo) {
  1960. ReleaseBoardErrOccurByString(i, "023730");
  1961. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1962. ReleaseBoardErrOccurByString(i, "013627");
  1963. }
  1964. break;
  1965. }
  1966. }
  1967. break;
  1968. }
  1969. }
  1970. }
  1971. }
  1972. //===============================================
  1973. // Main process
  1974. //===============================================
  1975. // 檢查 uint8_t 中某個 Bit 的值
  1976. // _byte : 欲改變的 byte
  1977. // _bit : 該 byte 的第幾個 bit
  1978. uint8_t DetectBitValue(uint8_t _byte, uint8_t _bit)
  1979. {
  1980. uint8_t mask_table[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
  1981. return ( _byte & mask_table[_bit] ) != 0x00;
  1982. }
  1983. // 設定 Byte 中某個 Bit的值
  1984. // _byte : 欲改變的 byte
  1985. // _bit : 該 byte 的第幾個 bit
  1986. // value : 修改的值為 0 or 1
  1987. void SetBitValue(uint8_t *_byte, uint8_t _bit, uint8_t value)
  1988. {
  1989. if (value == 1) {
  1990. *_byte |= (1 << _bit);
  1991. } else if (value == 0) {
  1992. *_byte ^= (1 << _bit);
  1993. }
  1994. }
  1995. uint8_t isModeChange(uint8_t gunIndex)
  1996. {
  1997. uint8_t result = NO;
  1998. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1999. if (pDcChargingInfo->SystemStatus == pDcChargingInfo->PreviousSystemStatus) {
  2000. return result;
  2001. }
  2002. pDcChargingInfo->PreviousSystemStatus = pDcChargingInfo->SystemStatus;
  2003. ShmDcCommonData->SystemModeChange[gunIndex] = YES; //for Module_EvRxComm print temperature message
  2004. return YES;
  2005. }
  2006. static int checkSlotGpioMappingGunType(uint8_t gunIndex, uint8_t gunType)
  2007. {
  2008. uint8_t slot1 = 0, slot2 = 0;
  2009. if (gunIndex == 0) {
  2010. slot1 = bd0_1_status;
  2011. slot2 = bd0_2_status;
  2012. } else if (gunIndex == 1) {
  2013. slot1 = bd1_1_status;
  2014. slot2 = bd1_2_status;
  2015. }
  2016. log_info("slot %d gpio1 = %d, gpio2 = %d, type = %d",
  2017. gunIndex,
  2018. slot1,
  2019. slot2,
  2020. gunType);
  2021. switch (gunType) {
  2022. case _Type_Chademo:
  2023. if (slot1 != NO && slot2 != YES) {
  2024. return FAIL;
  2025. }
  2026. break;
  2027. case _Type_CCS_2:
  2028. if (slot1 != YES && slot2 != NO) {
  2029. return FAIL;
  2030. }
  2031. break;
  2032. case _Type_GB:
  2033. if (slot1 != YES && slot2 != YES) {
  2034. return FAIL;
  2035. }
  2036. break;
  2037. }
  2038. return PASS;
  2039. }
  2040. bool CheckConnectorTypeStatus(void)
  2041. {
  2042. bool result = true;
  2043. uint8_t gunIndex = 0;
  2044. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  2045. struct ChargingInfoData *pDcChargingInfo = NULL;
  2046. if (!MappingGunChargingInfo("CSU Task")) {
  2047. log_error("CheckConnectorTypeStatus MappingGunChargingInfo failed");
  2048. return false;
  2049. }
  2050. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  2051. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2052. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2053. if (checkSlotGpioMappingGunType(gunIndex, pDcChargingInfo->Type) == FAIL) {
  2054. return false;
  2055. }
  2056. pDcChargingInfo->SystemStatus = S_BOOTING;
  2057. switch (gunIndex) {
  2058. case 0:
  2059. if (pSysConfig->TotalConnectorCount == 1) {
  2060. if ((bd0_1_status == YES || bd0_2_status == YES) &&
  2061. (bd1_1_status == NO && bd1_2_status == NO)) {
  2062. pDcChargingInfo->Evboard_id = 0x01;
  2063. }
  2064. if ((bd0_1_status == NO && bd0_2_status == NO) &&
  2065. (bd1_1_status == YES || bd1_2_status == YES)
  2066. ) {
  2067. pDcChargingInfo->Evboard_id = 0x02;
  2068. }
  2069. break;
  2070. }
  2071. pDcChargingInfo->Evboard_id = 0x01;
  2072. break;
  2073. case 1:
  2074. pDcChargingInfo->Evboard_id = 0x02;
  2075. break;
  2076. }
  2077. CheckHwSlotStatusLog(gunIndex);
  2078. if (pDcChargingInfo->Evboard_id == 0x00) {
  2079. return false;
  2080. }
  2081. }
  2082. AdjustChargerCurrent();
  2083. return result;
  2084. }
  2085. int SpawnTask(void)
  2086. {
  2087. if(SIMULATION)
  2088. system("/root/simulation &");
  2089. sleep(2);
  2090. system("/root/Module_EventLogging &");
  2091. system("/root/Module_PrimaryComm &");
  2092. system("/root/Module_EvComm &");
  2093. system("/root/Module_LcmControl &");
  2094. system("/root/Module_InternalComm &");
  2095. system("/root/Module_ProduceUtils &");
  2096. system("/root/Module_UpdateFW &");
  2097. system("/root/Module_DoComm &");
  2098. return PASS;
  2099. }
  2100. void KillTask(void)
  2101. {
  2102. //ChangeLcmByIndex(_LCM_ERROR);
  2103. ChangeLcmByIndex(_PAGE_MAINTAIN);
  2104. system("killall Module_EventLogging");
  2105. system("killall Module_PrimaryComm");
  2106. system("killall Module_EvComm");
  2107. system("killall Module_LcmControl");
  2108. system("killall Module_InternalComm");
  2109. system("killall Module_UpdateFW");
  2110. system("killall Module_ChkSysTask");
  2111. //system("killall Module_DoComm");
  2112. return ;
  2113. }
  2114. void KillTaskExceptPrimary(void)
  2115. {
  2116. //ChangeLcmByIndex(_LCM_ERROR);
  2117. ChangeLcmByIndex(_PAGE_MAINTAIN);
  2118. system("killall Module_EvComm");
  2119. system("killall Module_InternalComm");
  2120. return;
  2121. }
  2122. void KillAllTask(void)
  2123. {
  2124. //ChangeLcmByIndex(_LCM_ERROR);
  2125. ChangeLcmByIndex(_PAGE_MAINTAIN);
  2126. system("killall Module_EventLogging");
  2127. system("killall Module_PrimaryComm");
  2128. system("killall Module_EvComm");
  2129. system("killall Module_LcmControl");
  2130. system("killall Module_InternalComm");
  2131. system("killall Module_UpdateFW");
  2132. system("killall Module_ChkSysTask");
  2133. system("killall Module_DoComm");
  2134. return ;
  2135. }
  2136. void StartSystemTimeoutDet(uint8_t flag)
  2137. {
  2138. if (pSysInfo->SystemTimeoutFlag != flag) {
  2139. //log_info("Set System Timeout Flag %d", flag);
  2140. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  2141. }
  2142. pSysInfo->SystemTimeoutFlag = flag;
  2143. }
  2144. void StopSystemTimeoutDet(void)
  2145. {
  2146. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  2147. pSysInfo->SystemTimeoutFlag = Timeout_None;
  2148. }
  2149. void StartGunInfoTimeoutDet(uint8_t gunIndex, uint8_t flag)
  2150. {
  2151. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2152. if (gunIndex < pSysConfig->TotalConnectorCount) {
  2153. if (pDcChargingInfo->TimeoutFlag != flag) {
  2154. GetClockTime(&pDcChargingInfo->ConnectorTimeout, NULL);
  2155. //log_info("Set Gun%d Timeout Flag %d", gunIndex, flag);
  2156. }
  2157. pDcChargingInfo->TimeoutFlag = flag;
  2158. }
  2159. }
  2160. void StopGunInfoTimeoutDet(uint8_t gunIndex)
  2161. {
  2162. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2163. if (gunIndex < pSysConfig->TotalConnectorCount) {
  2164. pDcChargingInfo->TimeoutFlag = Timeout_None;
  2165. }
  2166. }
  2167. void CheckConnectionTimeout(void)
  2168. {
  2169. if (ShmSelectGunInfo->RemoteSetup.ConnectionTimeout != 0) { //Jerry add
  2170. _connectionTimeout = ShmSelectGunInfo->RemoteSetup.ConnectionTimeout;
  2171. } else {
  2172. _connectionTimeout = CONN_PLUG_TIMEOUT;
  2173. }
  2174. return;
  2175. }
  2176. void _evccidlinktimeout(uint8_t gunIndex)
  2177. {
  2178. log_info("Getting EVCCID Timeout");
  2179. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2180. pDcChargingInfo->isEVCCIDVerify = true;
  2181. StopGunInfoTimeoutDet(gunIndex);
  2182. destroySelGun(pSysInfo->CurGunSelected);
  2183. systemPageRestoreInit();
  2184. }
  2185. void CreateTimeoutFork(void)
  2186. {
  2187. pid_t timeoutPid;
  2188. timeoutPid = fork();
  2189. if (timeoutPid == 0) {
  2190. GetClockTime(&_cmdSubPriority_time, NULL);
  2191. CheckConnectionTimeout();
  2192. //log_info("Timeout Fork Child's PID is %d", getpid());
  2193. while (1) {
  2194. if ((GetClockTimeoutValue(_cmdSubPriority_time) / 1000) > 5000) {
  2195. CheckConnectionTimeout();
  2196. GetClockTime(&_cmdSubPriority_time, NULL);
  2197. }
  2198. /*
  2199. if (pSysInfo->SystemTimeoutFlag != 0)
  2200. log_info("Timeout ***********SystemTimeoutFlag = %d(%d) ********",pSysInfo->SystemTimeoutFlag,
  2201. GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL);
  2202. */
  2203. // 系統
  2204. switch (pSysInfo->SystemTimeoutFlag) {
  2205. case Timeout_SelftestChk:
  2206. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT) {
  2207. _SelfTestTimeout();
  2208. StopSystemTimeoutDet();
  2209. destroySelGun(DESTROY_ALL_SEL); //jerry add
  2210. }
  2211. break;
  2212. case Timeout_ReturnViewPage:
  2213. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= RETURN_VIEWPAGE_TIMEOUT) {
  2214. StopSystemTimeoutDet();
  2215. systemPageRestoreInit();
  2216. }
  2217. break;
  2218. case Timeout_Authorizing:
  2219. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT) {
  2220. log_error("Authorizing Timeout");
  2221. _AuthorizedTimeout();
  2222. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2223. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2224. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2225. log_error("Author timeout restart DoComm");
  2226. system("killall Module_DoComm");
  2227. }
  2228. }
  2229. break;
  2230. case Timeout_WaitBalance:
  2231. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= BALANCE_TIMEOUT) {
  2232. log_error("Wait Balance timeout");
  2233. _AuthorizedTimeout();
  2234. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2235. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2236. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2237. log_error("Author timeout restart DoComm");
  2238. system("killall Module_DoComm");
  2239. }
  2240. }
  2241. break;
  2242. case Timeout_VerifyFail:
  2243. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT) {
  2244. log_info("Timeout_VerifyFail");
  2245. StopSystemTimeoutDet();
  2246. systemPageRestoreInit();
  2247. setChargerMode(pSysInfo->CurGunSelected, S_IDLE);
  2248. }
  2249. break;
  2250. /*
  2251. case Timeout_WaitPlug:
  2252. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout) {
  2253. _DetectPlugInTimeout();
  2254. destroySelGun(pSysInfo->CurGunSelected);
  2255. }
  2256. break;
  2257. */
  2258. /*
  2259. case Timeout_ReturnToChargingGunDet:
  2260. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE) {
  2261. if (getCurLcmPage() != _LCM_VIEW &&
  2262. getCurLcmPage() != _LCM_DETAIL_VIEW ) {
  2263. destroySelGun(pSysInfo->CurGunSelected); //jerry add
  2264. }
  2265. DisplayChargingInfo();
  2266. StopSystemTimeoutDet();
  2267. }
  2268. break;
  2269. */
  2270. case Timeout_ScanCard:
  2271. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_SCANCARD_TIMEOUT) {
  2272. log_info("Timeout_ScanCard");
  2273. strcpy((char *)pSysConfig->UserId, "");
  2274. ClearAuthorizedFlag();
  2275. StopSystemTimeoutDet();
  2276. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2277. pDcChargingInfo->SystemStatus = S_IDLE;
  2278. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  2279. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2280. }
  2281. break;
  2282. case Timeout_Terminating:
  2283. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TERMINATING_TIMEOUT) {
  2284. StopSystemTimeoutDet();
  2285. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2286. log_info("Terminating timeout");
  2287. setChargerMode(pSysInfo->CurGunSelected, S_COMPLETE);
  2288. }
  2289. break;
  2290. case Timeout_AddLine:
  2291. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_ADDLINE_TIMEOUT) {
  2292. log_info("Timeout_AddLine");
  2293. StopSystemTimeoutDet();
  2294. systemPageRestoreInit();
  2295. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2296. }
  2297. break;
  2298. case Timeout_DonateComfirm:
  2299. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_DONATECOMFIRM_TIMEOUT) {
  2300. log_info("Timeout_DonateComfirm");
  2301. StopSystemTimeoutDet();
  2302. systemPageRestoreInit();
  2303. }
  2304. break;
  2305. case Timeout_SelectPayMode:
  2306. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_SELECTPAY_TIMEOUT) {
  2307. log_info("Timeout_SelectPayMode");
  2308. StopSystemTimeoutDet();
  2309. systemPageRestoreInit();
  2310. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2311. }
  2312. break;
  2313. case Timeout_TradeCancel:
  2314. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_TRADECANCEL_TIMEOUT) {
  2315. log_info("Timeout_TradeCancel");
  2316. StopSystemTimeoutDet();
  2317. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2318. setChargerMode(pSysInfo->CurGunSelected, MODE_IDLE);
  2319. systemPageRestoreInit();
  2320. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2321. }
  2322. break;
  2323. case Timeout_LINEPAYING:
  2324. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TCC_LINEPAYING_TIMEOUT) {
  2325. log_info("Timeout_LINEPAYING");
  2326. StopSystemTimeoutDet();
  2327. systemPageRestoreInit();
  2328. ShmDcCommonData->OperateIDLE[pSysInfo->CurGunSelected] = 1;
  2329. }
  2330. break;
  2331. }
  2332. // 各槍
  2333. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2334. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2335. /*
  2336. if (pDcChargingInfo->TimeoutFlag != 0)
  2337. log_info("Timeout ***********GunTimeoutFlag = %d(%d) ********",pDcChargingInfo->TimeoutFlag,
  2338. GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL);
  2339. */
  2340. switch (pDcChargingInfo->TimeoutFlag) {
  2341. case Timeout_AuthorizingForStop:
  2342. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT) {
  2343. log_info("Gun %d Timeout_AuthorizingForStop", gunIndex);
  2344. strcpy((char*)pSysConfig->UserId, "");
  2345. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  2346. pDcChargingInfo->SystemStatus = S_IDLE;
  2347. ClearAuthorizedFlag();
  2348. StopGunInfoTimeoutDet(gunIndex);
  2349. if (pSysInfo->CurGunSelected == gunIndex)
  2350. systemPageRestoreInit();
  2351. }
  2352. break;
  2353. case Timeout_WaitPlug:
  2354. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= _connectionTimeout) {
  2355. _DetectPlugInTimeout(gunIndex);
  2356. destroySelGun(gunIndex);
  2357. }
  2358. break;
  2359. case Timeout_EVCCID_Link:
  2360. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= EVCCID_LINK_TIMEOUT) {
  2361. _evccidlinktimeout(gunIndex);
  2362. }
  2363. break;
  2364. case Timeout_LinkError:
  2365. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= LINKERROR_TIMEOUT) {
  2366. _LinkErrorTimeout(gunIndex);
  2367. StopGunInfoTimeoutDet(gunIndex);
  2368. destroySelGun(gunIndex);
  2369. }
  2370. break;
  2371. case Timeout_Preparing:
  2372. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_PREPARE_TIMEOUT) {
  2373. _PrepareTimeout(gunIndex);
  2374. StopGunInfoTimeoutDet(gunIndex);
  2375. destroySelGun(gunIndex); //jerry add
  2376. }
  2377. break;
  2378. case Timeout_EvChargingDet:
  2379. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT) {
  2380. _DetectEvChargingEnableTimeout(gunIndex);
  2381. StopGunInfoTimeoutDet(gunIndex);
  2382. destroySelGun(gunIndex); //jerry add
  2383. }
  2384. break;
  2385. case Timeout_EvseChargingDet:
  2386. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT) {
  2387. _DetectEvseChargingEnableTimeout(gunIndex);
  2388. StopGunInfoTimeoutDet(gunIndex);
  2389. destroySelGun(gunIndex); //jerry add
  2390. }
  2391. break;
  2392. case Timeout_EvseCompleteDet:
  2393. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT) {
  2394. StopGunInfoTimeoutDet(gunIndex);
  2395. destroySelGun(gunIndex); //jerry add
  2396. }
  2397. break;
  2398. case Timeout_ForCcsPrechargeDet:
  2399. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT) {
  2400. _CcsPrechargeTimeout(gunIndex);
  2401. StopGunInfoTimeoutDet(gunIndex);
  2402. destroySelGun(gunIndex); //jerry add
  2403. }
  2404. break;
  2405. case Timeout_PlugOutGun:
  2406. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= PLUGOUTGUN_TIMEOUT) {
  2407. log_info("Gun[%d] Timeout_PlugOutGun",gunIndex);
  2408. setChargerMode(gunIndex, MODE_IDLE);
  2409. destroySelGun(gunIndex); //Jerry add
  2410. StopGunInfoTimeoutDet(gunIndex);
  2411. if (pSysInfo->CurGunSelected == gunIndex)
  2412. systemPageRestoreInit();
  2413. }
  2414. break;
  2415. case Timeout_FinalCost:
  2416. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_FINALCOST_TIMEOUT) {
  2417. log_info("Gun[%d] Timeout_FinalCost",gunIndex);
  2418. StopGunInfoTimeoutDet(gunIndex);
  2419. if (ShmDcCommonData->finalcost_flag[gunIndex] == FALSE) {
  2420. log_info("Not Get Final Cost");
  2421. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  2422. if (pSysInfo->CurGunSelected == gunIndex)
  2423. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  2424. }
  2425. }
  2426. break;
  2427. case Timeout_LineReigster:
  2428. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_LINEREGISTER_TIMEOUT) {
  2429. log_info("Gun[%d] Timeout_LineReigster",gunIndex);
  2430. StopGunInfoTimeoutDet(gunIndex);
  2431. systemPageRestoreInit();
  2432. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = FALSE;
  2433. ShmDcCommonData->OperateIDLE[gunIndex] = 1;
  2434. }
  2435. break;
  2436. case Timeout_ExitPage:
  2437. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_EXITPAGE_TIMEOUT) {
  2438. log_info("Gun[%d] Timeout_ExitPage", gunIndex);
  2439. StopGunInfoTimeoutDet(gunIndex);
  2440. ShmDcCommonData->is_exit[gunIndex] = TRUE;
  2441. if (pSysInfo->CurGunSelected == gunIndex)
  2442. pSysInfo->SystemPage = _PAGE_EXIT;
  2443. StartGunInfoTimeoutDet(gunIndex, Timeout_PlugOutGun);
  2444. }
  2445. break;
  2446. case Timeout_CompletPlugout:
  2447. if (GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) / uSEC_VAL >= TCC_COMPLETE_PLUGOUT_TIMEOUT) {
  2448. log_info("Gun[%d] Timeout_CompletePlugout", gunIndex);
  2449. setChargerMode(gunIndex, MODE_IDLE);
  2450. destroySelGun(gunIndex); //Jerry add
  2451. StopGunInfoTimeoutDet(gunIndex);
  2452. }
  2453. break;
  2454. }
  2455. }
  2456. sleep(1);
  2457. }
  2458. }
  2459. }
  2460. void CheckFactoryConfigFunction(void)
  2461. {
  2462. char Buf[256] = {0};
  2463. if (pSysInfo->FactoryConfiguration) {
  2464. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  2465. pSysConfig->ModelName,
  2466. pSysConfig->SerialNumber);
  2467. system(Buf);
  2468. system("rm -f /Storage/OCPP/OCPPConfiguration");
  2469. system("sync");
  2470. sleep(5);
  2471. system("reboot -f");
  2472. sleep(5);
  2473. system("reboot -f");
  2474. }
  2475. }
  2476. //===============================================
  2477. // Check reservation date is expired
  2478. //===============================================
  2479. int isReservationExpired(uint8_t gunIndex)
  2480. {
  2481. int result = NO;
  2482. struct tm expiredDate;
  2483. struct timeb expiredTime;
  2484. if (sscanf((char *) ShmOCPP16Data->ReserveNow[gunIndex].ExpiryDate,
  2485. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  2486. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  2487. &expiredDate.tm_min, &expiredDate.tm_sec) == 6) {
  2488. expiredDate.tm_year -= 1900;
  2489. expiredDate.tm_mon -= 1;
  2490. expiredTime.time = mktime(&expiredDate);
  2491. if (!CheckTimeOut(expiredTime)) {
  2492. result = YES;
  2493. }
  2494. }
  2495. return result;
  2496. }
  2497. //===============================================
  2498. // OCPP
  2499. //===============================================
  2500. void CheckOcppStatus(void)
  2501. {
  2502. bool canReset = true;
  2503. //bool canHardReset = true; //DS60-120 add
  2504. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES) {
  2505. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  2506. }
  2507. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES) {
  2508. if (pSysWarning->Level != WARN_LV_ER) {
  2509. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  2510. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2511. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2512. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  2513. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  2514. canReset = false;
  2515. if (pDcChargingInfo->SystemStatus >= S_REASSIGN &&
  2516. pDcChargingInfo->SystemStatus < S_TERMINATING) {
  2517. //canHardReset = false;
  2518. ChargingTerminalProcess(_index);
  2519. //restartFlag = 1;
  2520. }
  2521. }
  2522. }
  2523. }
  2524. if (canReset) {
  2525. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  2526. sprintf((char *)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2527. if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL) {
  2528. log_error("****** Hard Reboot ****** ");
  2529. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  2530. sleep(3);
  2531. system("reboot -f");
  2532. } else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL) {
  2533. log_error("****** Soft Reboot ****** ");
  2534. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  2535. sleep(3);
  2536. system("killall OcppBackend &");
  2537. KillAllTask();
  2538. TryCloseWatchdog();
  2539. system("/usr/bin/run_evse_restart.sh");
  2540. }
  2541. }
  2542. }
  2543. }
  2544. void OcppStopTransation(uint8_t gunIndex)
  2545. {
  2546. uint8_t _OcppGunIndex = gunIndex;
  2547. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2548. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  2549. if (pSysConfig->AcConnectorCount == 1 && gunIndex == 1) {
  2550. _OcppGunIndex = 2;
  2551. }
  2552. if (strcmp((char *)pDcChargingInfo->StartUserId, "") == EQUAL) {
  2553. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  2554. } else {
  2555. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)pDcChargingInfo->StartUserId);
  2556. }
  2557. log_info("IdTag = %s ", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  2558. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  2559. }
  2560. bool OcppRemoteStop(uint8_t gunIndex)
  2561. {
  2562. uint8_t acDirIndex = pSysConfig->AcConnectorCount;
  2563. // 有 AC 槍的話
  2564. if (acDirIndex > 0 && gunIndex > 0) {
  2565. gunIndex += acDirIndex;
  2566. }
  2567. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  2568. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES) {
  2569. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  2570. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  2571. }
  2572. return result;
  2573. }
  2574. bool WifiScheduleStop(uint8_t gunIndex)
  2575. {
  2576. bool result = false;
  2577. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2578. result = pDcChargingInfo->schedule.isTriggerStop;
  2579. pDcChargingInfo->schedule.isTriggerStop = NO;
  2580. return result;
  2581. }
  2582. void OcppRemoteStartChk()
  2583. {
  2584. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING) {
  2585. } else {
  2586. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  2587. uint8_t acDirIndex = pSysConfig->AcConnectorCount;
  2588. if (!isDetectPlugin(acDirIndex)) {
  2589. for (uint8_t ac_index = 0; ac_index < pSysConfig->AcConnectorCount; ac_index++) {
  2590. pAcChargingInfo = (struct ChargingInfoData*)GetAcChargingInfoData(ac_index);
  2591. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES) {
  2592. if (pAcChargingInfo->SystemStatus == S_IDLE ||
  2593. pAcChargingInfo->SystemStatus == S_RESERVATION) {
  2594. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  2595. pAcChargingInfo->RemoteStartFlag = YES;
  2596. pSysInfo->OrderCharging = YES;
  2597. //pSysInfo->OrderCharging = DEFAULT_AC_INDEX;
  2598. ShmOCPP16Data->CsMsg.bits[pSysConfig->TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  2599. DetectPluginStart(acDirIndex);
  2600. return;
  2601. }
  2602. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  2603. }
  2604. }
  2605. }
  2606. //uint8_t threeGunIndex = 0;
  2607. //uint8_t dcIndex = 0;
  2608. //bool isGunUsingStatus = false;
  2609. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  2610. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(_index);
  2611. /*
  2612. // 如果有 AC 槍,且 DC 槍也有兩把
  2613. if (acDirIndex == 1 && _index == 1) {
  2614. threeGunIndex = 1;
  2615. }
  2616. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES) {
  2617. dcIndex = _index;
  2618. }
  2619. if (pDcChargingInfo->SystemStatus != S_IDLE) {
  2620. isGunUsingStatus = true;
  2621. }
  2622. if (pDcChargingInfo->RemoteStartFlag == YES) {
  2623. //_remotestarting = true;
  2624. }
  2625. }
  2626. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  2627. if (isGunUsingStatus && pSysInfo->IsAlternatvieConf) {
  2628. if (dcIndex == 0) {
  2629. threeGunIndex = 0;
  2630. }
  2631. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  2632. return;
  2633. }
  2634. if (dcIndex == 0) {
  2635. threeGunIndex = 0;
  2636. }
  2637. */
  2638. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(_index);
  2639. if (ShmOCPP16Data->CsMsg.bits[_index].RemoteStartTransactionReq == YES &&
  2640. !isDetectPlugin(_index)) {
  2641. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2642. pDcChargingInfo->SystemStatus == S_RESERVATION) {
  2643. pDcChargingInfo->RemoteStartFlag = YES;
  2644. ShmDcCommonData->is_RemoteStart[_index] = TRUE;
  2645. //pSysInfo->OrderCharging = YES;
  2646. //pDcChargingInfo->SystemStatus = S_AUTHORIZING;
  2647. //pSysInfo->OrderCharging = gunIndex;
  2648. ShmOCPP16Data->CsMsg.bits[_index].RemoteStartTransactionReq = NO;
  2649. DetectPluginStart(_index);
  2650. setSelGunWaitToAuthor(_index);
  2651. log_info("Ocpp Remote Start Gun%d Pass", _index);
  2652. }
  2653. ShmOCPP16Data->CsMsg.bits[_index].RemoteStartTransactionReq = NO;
  2654. }
  2655. }
  2656. }
  2657. }
  2658. void ChkOcppStatus(uint8_t gunIndex)
  2659. {
  2660. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2661. if (pDcChargingInfo->SystemStatus == S_IDLE &&
  2662. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES) {
  2663. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  2664. if (isReservationExpired(gunIndex)) {
  2665. log_error("***************ChkOcppStatus : OcppReservedStatus******************** ");
  2666. pDcChargingInfo->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  2667. pDcChargingInfo->SystemStatus = S_RESERVATION;
  2668. }
  2669. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  2670. }
  2671. if (pDcChargingInfo->SystemStatus == S_RESERVATION &&
  2672. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES) {
  2673. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  2674. if (isReservationExpired(gunIndex)) {
  2675. log_error("***************ChkOcppStatus : Cancel OcppReservedStatus******************** ");
  2676. pDcChargingInfo->ReservationId = 0;
  2677. pDcChargingInfo->SystemStatus = S_IDLE;
  2678. }
  2679. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  2680. }
  2681. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES) {
  2682. log_error("***************ChkOcppStatus : OcppChangeAvailability******************** ");
  2683. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  2684. if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL) {
  2685. if (isDb_ready) {
  2686. DB_Update_Operactive(gunIndex, true);
  2687. }
  2688. pDcChargingInfo->IsAvailable = YES;
  2689. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2690. pDcChargingInfo->SystemStatus == S_RESERVATION ||
  2691. pDcChargingInfo->SystemStatus == S_MAINTAIN) {
  2692. setChargerMode(gunIndex, MODE_IDLE);
  2693. }
  2694. } else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL) {
  2695. if (isDb_ready) {
  2696. DB_Update_Operactive(gunIndex, false);
  2697. }
  2698. pDcChargingInfo->IsAvailable = NO;
  2699. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2700. pDcChargingInfo->SystemStatus == S_RESERVATION ||
  2701. pDcChargingInfo->SystemStatus == S_MAINTAIN) {
  2702. setChargerMode(gunIndex, MODE_MAINTAIN);
  2703. }
  2704. }
  2705. }
  2706. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES) {
  2707. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  2708. if (pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK && // DS60-120 add ||
  2709. pDcChargingInfo->SystemStatus <= S_CHARGING) {
  2710. // 充電中,需停止充電
  2711. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  2712. ChargingTerminalProcess(gunIndex);
  2713. }
  2714. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  2715. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  2716. }
  2717. }
  2718. bool CheckBackendChargingTimeout(uint8_t gunIndex)
  2719. {
  2720. bool result = false;
  2721. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2722. if (pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE) {
  2723. if (pSysConfig->MaxChargingDuration > 0) {
  2724. if (pDcChargingInfo->PresentChargedDuration > (pSysConfig->MaxChargingDuration * 60)) {
  2725. result = true;
  2726. }
  2727. }
  2728. }
  2729. return result;
  2730. }
  2731. bool CheckBackendChargingEnergy(uint8_t gunIndex)
  2732. {
  2733. bool result = false;
  2734. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2735. if (pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE) {
  2736. if (pSysConfig->MaxChargingEnergy > 0) {
  2737. if (pDcChargingInfo->PresentChargedEnergy > pSysConfig->MaxChargingEnergy) {
  2738. result = true;
  2739. }
  2740. }
  2741. }
  2742. return result;
  2743. }
  2744. void InformOcppErrOccur(uint8_t codeType)
  2745. {
  2746. char _error[25];
  2747. switch (codeType) {
  2748. case 4:
  2749. strcpy(_error, "InternalError");
  2750. break;
  2751. case 6:
  2752. strcpy(_error, "NoError");
  2753. break;
  2754. case 7:
  2755. strcpy(_error, "OtherError");
  2756. break;
  2757. case 13:
  2758. strcpy(_error, "UnderVoltage");
  2759. break;
  2760. case 14:
  2761. strcpy(_error, "OverVoltage");
  2762. break;
  2763. }
  2764. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2765. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, _error);
  2766. }
  2767. }
  2768. //===============================================
  2769. // Config process
  2770. //===============================================
  2771. /*
  2772. void AddPlugInTimes(uint8_t gunIndex)
  2773. {
  2774. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2775. if (pDcChargingInfo->Type == _Type_Chademo) {
  2776. pSysConfig->ChademoPlugInTimes += 1;
  2777. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  2778. pSysConfig->Ccs2PlugInTimes += 1;
  2779. } else if (pDcChargingInfo->Type == _Type_GB) {
  2780. pSysConfig->GbPlugInTimes += 1;
  2781. }
  2782. }*/
  2783. void ChangeStartOrStopDateTime(uint8_t isStart, uint8_t gunIndex)
  2784. {
  2785. char cmdBuf[32];
  2786. struct timeb csuTime;
  2787. struct tm *tmCSU;
  2788. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2789. ftime(&csuTime);
  2790. tmCSU = localtime(&csuTime.time);
  2791. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  2792. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  2793. tmCSU->tm_sec);
  2794. if (isStart) {
  2795. strcpy((char *)pDcChargingInfo->StartDateTime, cmdBuf);
  2796. } else {
  2797. strcpy((char *)pDcChargingInfo->StopDateTime, cmdBuf);
  2798. }
  2799. }
  2800. void ChangeGunSelectByIndex(uint8_t sel)
  2801. {
  2802. pSysInfo->CurGunSelected = sel;
  2803. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  2804. }
  2805. void CheckIsAlternatvieByModelName()
  2806. {
  2807. if (pSysConfig->ModelName[1] == 'W') { //DS60-120
  2808. // 壁掛
  2809. pSysInfo->IsAlternatvieConf = YES;
  2810. } else {
  2811. pSysInfo->IsAlternatvieConf = NO;
  2812. }
  2813. }
  2814. void StopProcessingLoop()
  2815. {
  2816. log_info("Stop Processing....");
  2817. for (;;) {
  2818. CheckFactoryConfigFunction();
  2819. //CheckFwUpdateFunction();
  2820. if (pSysWarning->Level == WARN_LV_ER) {
  2821. ChkPrimaryStatus();
  2822. if (pSysWarning->Level == WARN_LV_NL) {
  2823. log_info("Soft reboot for retry self-tets (Primary). ");
  2824. KillAllTask();
  2825. sleep(3);
  2826. TryCloseWatchdog();
  2827. system("/usr/bin/run_evse_restart.sh");
  2828. return;
  2829. }
  2830. }
  2831. sleep(1);
  2832. TryFeedWatchdog();
  2833. }
  2834. }
  2835. bool IsConnectorWholeIdle()
  2836. {
  2837. bool result = true;
  2838. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  2839. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  2840. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2841. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  2842. result = false;
  2843. break;
  2844. }
  2845. }
  2846. for (uint8_t count = 0; count < pSysConfig->AcConnectorCount; count++) {
  2847. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(count);
  2848. if (pAcChargingInfo->SystemStatus != S_IDLE &&
  2849. pAcChargingInfo->IsErrorOccur == NO) {
  2850. result = false;
  2851. break;
  2852. }
  2853. }
  2854. return result;
  2855. }
  2856. void ClearAlarmCodeWhenAcOff()
  2857. {
  2858. if (!pSysInfo->AcContactorStatus) {
  2859. pAlarmCode->AlarmEvents.bits.PsuNoResource = NO;
  2860. }
  2861. }
  2862. //==========================================
  2863. // Check Smart Charging Profile
  2864. //==========================================
  2865. int GetStartScheduleTime(uint8_t *time)
  2866. {
  2867. int result = -1;
  2868. struct tm tmScheduleStart;
  2869. struct timeb tbScheduleStart;
  2870. 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)) {
  2871. tmScheduleStart.tm_year -= 1900;
  2872. tmScheduleStart.tm_mon -= 1;
  2873. tbScheduleStart.time = mktime(&tmScheduleStart);
  2874. tbScheduleStart.millitm = 0;
  2875. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  2876. }
  2877. return result;
  2878. }
  2879. void CheckSmartChargeProfile(uint8_t _index)
  2880. {
  2881. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2882. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == YES) {
  2883. // Get Charging Profile
  2884. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  2885. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL &&
  2886. ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileId == YES //DS60-120 add
  2887. ) {
  2888. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  2889. uint8_t _startCount = NO_DEFINE;
  2890. uint8_t _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  2891. for (uint8_t _count = 0; _count < _maxCount; _count++) {
  2892. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  2893. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod) {
  2894. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  2895. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR) {
  2896. _startCount = _count;
  2897. }
  2898. }
  2899. }
  2900. log_info("_startCount = %d ", _startCount);
  2901. if (_startCount < _maxCount) {
  2902. //DS60-120 add
  2903. log_info("Profile Limit = %f ", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  2904. pDcChargingInfo->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  2905. if (pDcChargingInfo->EvBatterytargetVoltage > 0 && pDcChargingInfo->PresentChargingVoltage > 0) {
  2906. pDcChargingInfo->ChargingProfileCurrent = (pDcChargingInfo->ChargingProfilePower / pDcChargingInfo->PresentChargingVoltage) * 10;
  2907. } else {
  2908. pDcChargingInfo->ChargingProfileCurrent = 0;
  2909. }
  2910. } else {
  2911. pDcChargingInfo->ChargingProfilePower = -1;
  2912. pDcChargingInfo->ChargingProfileCurrent = -1;
  2913. }
  2914. } else {
  2915. pDcChargingInfo->ChargingProfilePower = -1;
  2916. pDcChargingInfo->ChargingProfileCurrent = -1;
  2917. }
  2918. log_info("ChargingProfilePower = %f ", pDcChargingInfo->ChargingProfilePower);
  2919. log_info("ChargingProfileCurrent = %f ", pDcChargingInfo->ChargingProfileCurrent);
  2920. }
  2921. }
  2922. void ChargingProfileFlat(uint8_t _index)
  2923. {
  2924. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO) {
  2925. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO) {
  2926. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  2927. }
  2928. }
  2929. }
  2930. /*
  2931. void CheckReturnToChargingConn()
  2932. {
  2933. if ((pSysConfig->TotalConnectorCount + pSysConfig->AcConnectorCount) > 1 &&
  2934. pSysInfo->PageIndex != _LCM_START_AUTHORIZING &&
  2935. pSysInfo->PageIndex != _LCM_START_AUTHORIZE_FAIL &&
  2936. pSysInfo->PageIndex != _LCM_WAIT_PLUGIN) {
  2937. bool isReturnTimeout = false;
  2938. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  2939. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  2940. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  2941. if (count == pSysInfo->CurGunSelected) {
  2942. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2943. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2944. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2945. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  2946. ) {
  2947. isReturnTimeout = false;
  2948. break;
  2949. }
  2950. } else if (count != pSysInfo->CurGunSelected) {
  2951. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2952. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2953. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2954. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  2955. ) {
  2956. isReturnTimeout = true;
  2957. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2958. }
  2959. }
  2960. }
  2961. // AC 槍
  2962. if (!isReturnTimeout && pSysConfig->AcConnectorCount > 0) {
  2963. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2964. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  2965. // 沒有選中 AC,且 AC 在充電中
  2966. if (pSysInfo->CurGunSelectedByAc == NO_DEFINE &&
  2967. (pAcChargingInfo->SystemStatus >= S_PREPARNING &&
  2968. pAcChargingInfo->SystemStatus <= S_COMPLETE)) {
  2969. // 當前 DC 充電槍在 idle 狀態
  2970. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2971. pDcChargingInfo->SystemStatus == S_RESERVATION) {
  2972. isReturnTimeout = true;
  2973. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2974. }
  2975. } else if (pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2976. ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2977. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2978. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2979. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))) {
  2980. // 當前 DC 充電槍在 idle 狀態
  2981. if (pAcChargingInfo->SystemStatus == S_IDLE ||
  2982. pAcChargingInfo->SystemStatus == S_RESERVATION) {
  2983. isReturnTimeout = true;
  2984. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2985. }
  2986. }
  2987. }
  2988. if (!isReturnTimeout) {
  2989. StopSystemTimeoutDet();
  2990. }
  2991. }
  2992. }
  2993. bool GetStartChargingByAlterMode(uint8_t _gun)
  2994. {
  2995. bool result = true;
  2996. if (pSysConfig->TotalConnectorCount == 2 &&
  2997. pSysInfo->IsAlternatvieConf == YES) {
  2998. for (uint8_t _select = 0; _select < pSysConfig->TotalConnectorCount; _select++) {
  2999. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_select);
  3000. if (_select != _gun) {
  3001. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  3002. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  3003. result = false;
  3004. break;
  3005. }
  3006. }
  3007. }
  3008. }
  3009. return result;
  3010. }
  3011. */
  3012. void TheEndCharging(uint8_t gunIndex)
  3013. {
  3014. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3015. ftime(&endChargingTime[gunIndex]);
  3016. if (pDcChargingInfo->PresentChargedDuration != 0) {
  3017. pDcChargingInfo->PresentChargedDuration = DiffTimeb(startChargingTime[gunIndex], endChargingTime[gunIndex]);
  3018. }
  3019. pDcChargingInfo->isRemoteStart = NO;
  3020. if (pDcChargingInfo->TimeoutFlag != Timeout_FinalCost)
  3021. StopGunInfoTimeoutDet(gunIndex);
  3022. //StartGunInfoTimeoutDet(gunIndex, Timeout_EvseCompleteDet);
  3023. ChangeStartOrStopDateTime(NO, gunIndex);
  3024. DB_Insert_Record(gunIndex);
  3025. }
  3026. void UpdateErrorCodeToOcpp(uint8_t index)
  3027. {
  3028. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  3029. //log_info("%d = ConnectorAlarmCode = %s", index, pDcChargingInfo->ConnectorAlarmCode);
  3030. //log_info("%d = EvConnAlarmCode = %s", index, pDcChargingInfo->EvConnAlarmCode);
  3031. if (strcmp((char *)pDcChargingInfo->ConnectorAlarmCode, "") != EQUAL) {
  3032. //if (strlen((char *)pDcChargingInfo->ConnectorAlarmCode) == EQUAL) {
  3033. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "InternalError");
  3034. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)pDcChargingInfo->ConnectorAlarmCode);
  3035. } else if (strcmp((char *)pDcChargingInfo->EvConnAlarmCode, "") != EQUAL) {
  3036. //} else if (strlen((char *)pDcChargingInfo->EvConnAlarmCode) == EQUAL) {
  3037. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "OtherError");
  3038. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)pDcChargingInfo->EvConnAlarmCode);
  3039. }
  3040. //log_info("Gun %d = VendorErrorCode = %s", index, (char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode);
  3041. }
  3042. void AdjustChargerCurrent()
  3043. {
  3044. pSysConfig->RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  3045. // 設定的電流~ 如超過可輸出的電流,則 bypass
  3046. if (pSysConfig->RatingCurrent < pSysConfig->MaxChargingCurrent ||
  3047. pSysConfig->RatingCurrent == 0) {
  3048. pSysConfig->MaxChargingCurrent = 0;
  3049. }
  3050. log_info("PSU : MaxChargingPower = %d, MaxChargingCurrent = %d",
  3051. ShmPsuData->SystemAvailablePower / 10,
  3052. ShmPsuData->SystemAvailableCurrent / 10
  3053. );
  3054. log_info("Config : ChargingPower = %d, ChargingCurrent = %d",
  3055. pSysConfig->MaxChargingPower,
  3056. pSysConfig->MaxChargingCurrent
  3057. );
  3058. }
  3059. void ResetDetAlarmStatus(uint8_t gun)
  3060. {
  3061. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gun);
  3062. if (pDcChargingInfo->Type == _Type_Chademo) {
  3063. if (pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP == YES) {
  3064. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = NO;
  3065. }
  3066. } else if (pDcChargingInfo->Type == _Type_GB) {
  3067. if (pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP == YES) {
  3068. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = NO;
  3069. }
  3070. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3071. if (pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP == YES) {
  3072. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = NO;
  3073. }
  3074. }
  3075. }
  3076. static void autoStartCharging(uint8_t gunIndex)
  3077. {
  3078. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3079. if ( (pSysInfo->SystemPage >= _PAGE_AUTHORIZE && pSysInfo->SystemPage <= _PAGE_SENSING) ||
  3080. (pDcChargingInfo->isEVCCIDVerify && strcmp( (char *)pSysConfig->UserId , "" ) != EQUAL)) {
  3081. return;
  3082. }
  3083. if ((pDcChargingInfo->ConnectorPlugIn == YES) &&
  3084. (pDcChargingInfo->SystemStatus == S_IDLE )
  3085. ) {
  3086. if(!pSysConfig->AuthorisationMode) {
  3087. /*
  3088. if (pSysConfig->isAuthrizeByEVCCID && !pDcChargingInfo->isEVCCIDVerify &&
  3089. pDcChargingInfo->Type == _Type_CCS_2) {
  3090. // LCM show linking pic. & timeout 2 min
  3091. if( pSysInfo->CurGunSelected == gunIndex ) {
  3092. systemPageRestoreInit();
  3093. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3094. }
  3095. StartGunInfoTimeoutDet(gunIndex,Timeout_EVCCID_Link);
  3096. // Getting EVCCID
  3097. if( strcmp( (char *)pDcChargingInfo->EVCCID, "" ) != EQUAL ) {
  3098. log_info("Authorizing EVCCID");
  3099. StopSystemTimeoutDet();
  3100. strcpy((char *)pSysConfig->UserId, (char *)pDcChargingInfo->EVCCID);
  3101. ChangeGunSelectByIndex(gunIndex);
  3102. confirmSelGun(gunIndex);
  3103. pDcChargingInfo->isEVCCIDVerify = true;
  3104. pSysInfo->SystemPage = _LCM_START_AUTHORIZING;
  3105. log_info("Get User(%d) ID:%s",gunIndex,pSysConfig->UserId);
  3106. setChargerMode(gunIndex, MODE_AUTHORIZING);
  3107. }
  3108. } else
  3109. return;
  3110. */
  3111. } else {
  3112. strcpy((char *)&pSysConfig->UserId, "AutoStartCharging");
  3113. ChangeGunSelectByIndex(gunIndex);
  3114. confirmSelGun(gunIndex);
  3115. ShmDcCommonData->TradeCancel = FALSE;
  3116. ShmDcCommonData->is_AutoStart[gunIndex] = TRUE;
  3117. pSysInfo->SystemPage = _PAGE_SENSING;
  3118. ShmDcCommonData->AuthPass_flag[gunIndex] = TRUE;
  3119. //setChargerMode(gunIndex, MODE_AUTHORIZING);
  3120. log_info("Get User(%d) ID:%s",gunIndex,pSysConfig->UserId);
  3121. }
  3122. }
  3123. }
  3124. static bool PrecheckIsPass(uint8_t gunIndex)
  3125. {
  3126. bool result = true;
  3127. // relay welding or driving 是反向
  3128. result = !ShmDcCommonData->GunRelayWeldingOccur[gunIndex];
  3129. return result;
  3130. }
  3131. static void ReviewCriticalAlarm(void)
  3132. {
  3133. if (
  3134. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES ||
  3135. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES ||
  3136. pAlarmCode->AlarmEvents.bits.DoorOpen == YES ||
  3137. pSysWarning->ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  3138. pAlarmCode->AlarmEvents.bits.PsuFailureAlarm == YES ||
  3139. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES ||
  3140. //Power cabinet alarm status
  3141. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop == YES ||
  3142. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen == YES ||
  3143. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP == YES ||
  3144. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputUVP == YES ||
  3145. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP == YES ||
  3146. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP == YES ||
  3147. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP == YES ||
  3148. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure == YES ) {
  3149. pSysWarning->Level = WARN_LV_ER;
  3150. } else {
  3151. if (ShmSelectGunInfo->EthDevStatus.Backend == 0 ||
  3152. ShmSelectGunInfo->EthDevStatus.Backend == 2 ) {
  3153. pInfoCode->InfoEvents.bits.BackendDisconnectedViaEthernet = TRUE;
  3154. if (ShmDcCommonData->DebugFlag || pSysConfig->AuthorisationMode) {
  3155. pSysWarning->Level = WARN_LV_NL;
  3156. } else {
  3157. pSysWarning->Level = WARN_LV_ER;
  3158. return;
  3159. }
  3160. } else if (ShmSelectGunInfo->EthDevStatus.Backend == 1) {
  3161. pInfoCode->InfoEvents.bits.BackendDisconnectedViaEthernet = FALSE;
  3162. }
  3163. pSysWarning->Level = WARN_LV_NL;
  3164. }
  3165. }
  3166. static void CheckRelayWeldingOrDrivingFault(uint8_t gunIndex)
  3167. {
  3168. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3169. // relay welding fault then stop the charging process.
  3170. uint8_t faultCode = RELAY_STATUS_ERROR_NONE;
  3171. //static uint8_t drivingCount = 0;
  3172. if (gunIndex == 0) {
  3173. if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  3174. ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  3175. faultCode = RELAY_STATUS_ERROR_WELDING;
  3176. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  3177. ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  3178. faultCode = RELAY_STATUS_ERROR_DRIVING;
  3179. }
  3180. } else if (gunIndex == 1) {
  3181. if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  3182. ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  3183. faultCode = RELAY_STATUS_ERROR_WELDING;
  3184. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  3185. ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  3186. faultCode = RELAY_STATUS_ERROR_DRIVING;
  3187. }
  3188. }
  3189. if (pSysConfig->TotalConnectorCount >= 2 &&
  3190. !pSysInfo->IsAlternatvieConf) {
  3191. // 橋接
  3192. if (ShmDcCommonData->CheckRelayStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  3193. ShmDcCommonData->CheckRelayStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  3194. faultCode = RELAY_STATUS_ERROR_WELDING;
  3195. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  3196. ShmDcCommonData->CheckRelayStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  3197. faultCode = RELAY_STATUS_ERROR_DRIVING;
  3198. }
  3199. }
  3200. if (faultCode == RELAY_STATUS_ERROR_WELDING) {
  3201. // welding
  3202. if (pDcChargingInfo->Type == _Type_Chademo) {
  3203. RecordAlarmCode(gunIndex, "011011");
  3204. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = YES;
  3205. } else if (pDcChargingInfo->Type == _Type_GB) {
  3206. RecordAlarmCode(gunIndex, "011015");
  3207. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = YES;
  3208. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3209. RecordAlarmCode(gunIndex, "011013");
  3210. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = YES;
  3211. }
  3212. ShmDcCommonData->GunRelayWeldingOccur[gunIndex] = YES;
  3213. //EmcOccureByString("");
  3214. } else if (faultCode == RELAY_STATUS_ERROR_DRIVING) {
  3215. //if (drivingCount++ != 2) {
  3216. // return;
  3217. //}
  3218. // driving
  3219. if (pDcChargingInfo->Type == _Type_Chademo) {
  3220. RecordAlarmCode(gunIndex, "011012");
  3221. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = YES;
  3222. } else if (pDcChargingInfo->Type == _Type_GB) {
  3223. RecordAlarmCode(gunIndex, "011016");
  3224. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = YES;
  3225. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3226. RecordAlarmCode(gunIndex, "011014");
  3227. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = YES;
  3228. }
  3229. ShmDcCommonData->GunRelayDrivingOccur[gunIndex] = YES;
  3230. //EmcOccureByString("");
  3231. } else {
  3232. ShmDcCommonData->GunRelayWeldingOccur[gunIndex] = NO;
  3233. ShmDcCommonData->GunRelayDrivingOccur[gunIndex] = NO;
  3234. //drivingCount = 0;
  3235. }
  3236. }
  3237. static void isChargingAverageState(void)
  3238. {
  3239. if (pSysInfo->MainChargingMode != _MAIN_CHARGING_MODE_AVER) {
  3240. pSysInfo->ReAssignedFlag = _REASSIGNED_NONE;
  3241. return;
  3242. }
  3243. // 均充 -> 最大充
  3244. if (pSysInfo->BridgeRelayStatus == NO) {
  3245. if (pSysInfo->ReAssignedFlag == _REASSIGNED_NONE) {
  3246. log_info("=============Smart Charging============= Step 11 ");
  3247. pSysInfo->ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  3248. }
  3249. } else if (pSysInfo->ReAssignedFlag != _REASSIGNED_COMP &&
  3250. pSysInfo->ReAssignedFlag != _REASSIGNED_WAITING) {
  3251. log_info("=============Smart Charging============= Step 14 ");
  3252. pSysInfo->ReAssignedFlag = _REASSIGNED_WAITING;
  3253. } else if (pSysInfo->ReAssignedFlag == _REASSIGNED_COMP) {
  3254. pSysInfo->MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3255. pSysInfo->ReAssignedFlag = _REASSIGNED_NONE;
  3256. }
  3257. }
  3258. bool Taskconutstring(char *src, char *taskname)
  3259. {
  3260. bool result = false;
  3261. if (src == NULL || strlen(src) == 0)
  3262. return result;
  3263. if (strstr(src, taskname) != NULL &&
  3264. strstr(src, "grep") == NULL &&
  3265. strstr(src, "[") == NULL)
  3266. {
  3267. result = true;
  3268. }
  3269. return result;
  3270. }
  3271. int GetProcessCount(char *procName)
  3272. {
  3273. int result = 0;
  3274. FILE *fp;
  3275. char cmd[256];
  3276. char buf[256];
  3277. sprintf(cmd, "ps -ef |grep %s", procName);
  3278. fp = popen(cmd, "r");
  3279. if(fp != NULL)
  3280. {
  3281. while(fgets(buf, sizeof(buf), fp) != NULL)
  3282. {
  3283. if (Taskconutstring(buf, procName))
  3284. result++;
  3285. }
  3286. }
  3287. pclose(fp);
  3288. return result;
  3289. }
  3290. void CheckTaskAlive()
  3291. {
  3292. pid_t Pid = fork();
  3293. if ( Pid == 0 ) {
  3294. //log_info("CheckTaskAlive fork Child's PID is %d", getpid());
  3295. while(1) {
  3296. sleep(3);
  3297. unsigned char count = GetProcessCount("Module_ChkSysTask");
  3298. if( count != 1 ) {
  3299. system("pkill Module_ChkSysTask");
  3300. sleep(10);
  3301. //log_info("Create Check System Task");
  3302. system("/root/Module_ChkSysTask &");
  3303. }
  3304. }
  3305. return;
  3306. }
  3307. log_info("Create Check Systam fork:%d",Pid);
  3308. return;
  3309. }
  3310. /**
  3311. * [checkPileEndGfdResult 確認充電樁GFD狀態]
  3312. * @Author Jerry
  3313. * @DateTime 2021-07-01
  3314. * @param gunIndex [當前的槍]
  3315. * @param gunType [槍的類型]
  3316. * @param sysStatus [槍的系統狀態]
  3317. */
  3318. static void checkPileEndGfdResult(uint8_t gunIndex, uint8_t gunType, uint8_t sysStatus)
  3319. {
  3320. switch (gunType) {
  3321. case _Type_Chademo:
  3322. // 檢查樁端的 GFD 結果
  3323. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3324. isPrechargeStatus_chademo(gunIndex) >= 6 ) {
  3325. // 當前操作的槍號,進入 Charging
  3326. setChargerMode(gunIndex, MODE_CHARGING);
  3327. }
  3328. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3329. // GFD 錯誤停止
  3330. RecordAlarmCode(gunIndex, "012234");
  3331. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = YES;
  3332. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  3333. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3334. // GFD 警告
  3335. //RecordAlarmCode(gunIndex, "012296");
  3336. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  3337. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = YES;
  3338. }
  3339. else {
  3340. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  3341. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  3342. }
  3343. break;
  3344. case _Type_GB:
  3345. // 檢查樁端的 GFD 結果
  3346. //if (isPrechargeStatus_gb(gunIndex) > 5 && isPrechargeStatus_gb(gunIndex) < 9) {
  3347. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3348. isPrechargeStatus_gb(gunIndex) >= 6) {
  3349. setChargerMode(gunIndex, MODE_CHARGING);
  3350. }
  3351. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3352. // GFD 錯誤停止
  3353. RecordAlarmCode(gunIndex, "012236");
  3354. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = YES;
  3355. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  3356. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3357. // GFD 警告
  3358. //RecordAlarmCode(gunIndex, "012298");
  3359. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  3360. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = YES;
  3361. }
  3362. else {
  3363. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  3364. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  3365. }
  3366. break;
  3367. case _Type_CCS_2:
  3368. // 檢查樁端的 GFD 結果
  3369. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3370. (pDcChargingInfo->GroundFaultStatus == GFD_PASS ||
  3371. pDcChargingInfo->GroundFaultStatus == GFD_WARNING)
  3372. ) {
  3373. setChargerMode(gunIndex, MODE_CCS_PRECHARGE_STEP0);
  3374. }
  3375. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3376. // GFD 錯誤停止
  3377. RecordAlarmCode(gunIndex, "012235");
  3378. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  3379. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  3380. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3381. // GFD 警告
  3382. //RecordAlarmCode(gunIndex, "012297");
  3383. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  3384. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = YES;
  3385. }
  3386. else {
  3387. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  3388. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  3389. }
  3390. break;
  3391. }
  3392. }
  3393. static void checkEvBoardReqStop(uint8_t sysStatus, uint8_t gunIndex)
  3394. {
  3395. uint8_t evBoardStopState = 0;
  3396. if ((evBoardStopState = isEvBoardStopChargeFlag(gunIndex)) > 0) {
  3397. // 板端要求停止 (錯誤)
  3398. if (sysStatus != S_CCS_PRECHARGE_ST0 &&
  3399. sysStatus != S_CCS_PRECHARGE_ST1) {
  3400. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3401. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "EVDisconnected");
  3402. }
  3403. }
  3404. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  3405. log_info("EV Board Stop Charging with some error");
  3406. ChargingAlarmProcess(gunIndex);
  3407. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  3408. log_info("Power Cabient Stop Charging");
  3409. ChargingTerminalProcess(gunIndex);
  3410. }
  3411. } else if (isEvBoardNormalStopChargeFlag(gunIndex) == YES) {
  3412. // 板端要求停止 (正常)
  3413. if (sysStatus != S_CCS_PRECHARGE_ST0 &&
  3414. sysStatus != S_CCS_PRECHARGE_ST1) {
  3415. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3416. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "EVDisconnected");
  3417. }
  3418. }
  3419. log_info("EV Board Normal Stop Charging");
  3420. ChargingTerminalProcess(gunIndex);
  3421. } else if (OcppRemoteStop(gunIndex) == YES ||
  3422. WifiScheduleStop(gunIndex)) {
  3423. // 後臺要求停止
  3424. log_info("OCPP Stop Charging");
  3425. ChargingTerminalProcess(gunIndex);
  3426. }
  3427. }
  3428. static void checkOCPPReqStop(uint8_t gunIndex)
  3429. {
  3430. if (OcppRemoteStop(gunIndex) == YES ||
  3431. WifiScheduleStop(gunIndex) ||
  3432. CheckBackendChargingTimeout(gunIndex) ||
  3433. CheckBackendChargingEnergy(gunIndex) ||
  3434. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex].ResponseIdTagInfo.Status, "Invalid") == EQUAL) {
  3435. // 後臺要求停止
  3436. ChargingTerminalProcess(gunIndex);
  3437. }
  3438. }
  3439. void ResetIdleData(uint8_t gunIndex)
  3440. {
  3441. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3442. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  3443. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  3444. float powerconsumption = ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption;
  3445. pDcChargingInfo->PresentChargedDuration = 0;
  3446. pDcChargingInfo->RemainChargingDuration = 0;
  3447. pDcChargingInfo->PresentChargingVoltage = 0;//DS60-120 add
  3448. pDcChargingInfo->PresentChargingCurrent = 0;//DS60-120 add
  3449. pDcChargingInfo->RemoteStartFlag = NO;
  3450. pDcChargingInfo->Replug_flag = FALSE;
  3451. strcpy((char *)pDcChargingInfo->StartDateTime, "");
  3452. strcpy((char *)pDcChargingInfo->StopDateTime, "");
  3453. strcpy((char *)pDcChargingInfo->StartUserId, "");
  3454. strcpy((char *)pSysConfig->UserId, "");
  3455. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "");
  3456. ClearDetectPluginFlag(gunIndex);
  3457. //Jerry add
  3458. memset(&ShmSelectGunInfo->PricesInfo[gunIndex], 0, sizeof(PricesInfo));
  3459. memset(&ShmDcCommonData->TransactionInfo[gunIndex], 0, sizeof(RecordTransactionInfo));
  3460. memset(&ShmDcCommonData->pGunInfo[gunIndex], 0, sizeof(GunInfo));
  3461. ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption = powerconsumption;
  3462. ShmDcCommonData->finalcost_flag[gunIndex] = FALSE;
  3463. ShmDcCommonData->AuthPass_flag[gunIndex] = FALSE;
  3464. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  3465. ShmDcCommonData->OperateIDLE[gunIndex] = 1;
  3466. ShmDcCommonData->is_RemoteStart[gunIndex] = FALSE;
  3467. ShmDcCommonData->is_AutoStart[gunIndex] = FALSE;
  3468. ShmDcCommonData->is_exit[gunIndex] = FALSE;
  3469. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  3470. ShmDcCommonData->is_plugout[gunIndex] = FALSE;
  3471. ShmSelectGunInfo->PricesInfo[gunIndex].Balance = FAIL_BALANCE_PRICES;
  3472. destroySelGun(gunIndex);
  3473. ResetDetAlarmStatus(gunIndex); //recovery OVP status code
  3474. if (ShmSelectGunInfo->AuthorStateFromCabinet[gunIndex] == YES) {
  3475. ShmSelectGunInfo->AuthorStateFromCabinet[gunIndex] = NO;
  3476. }
  3477. //strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].VendorErrorCode, "");
  3478. ReleaseAlarmCode(gunIndex);
  3479. if (pSysInfo->SystemPage != _PAGE_SELECT_GUN &&
  3480. pSysInfo->SystemPage != _PAGE_SELECT_PAY &&
  3481. pSysInfo->CurGunSelected == gunIndex) {
  3482. systemPageRestoreInit();
  3483. }
  3484. }
  3485. void CheckErrorCode(uint8_t gunIndex)
  3486. {
  3487. /*
  3488. struct ChargingInfoData *pSelectedDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3489. if (gunIndex == pSysInfo->CurGunSelected) {
  3490. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3491. } else if (pSelectedDcChargingInfo->SystemStatus != S_IDLE &&
  3492. pSelectedDcChargingInfo->SystemStatus != S_RESERVATION &&
  3493. pSelectedDcChargingInfo->SystemStatus != S_MAINTAIN &&
  3494. pSelectedDcChargingInfo->SystemStatus != S_FAULT) {
  3495. if (pSelectedDcChargingInfo->SystemStatus == S_CHARGING) {
  3496. if(ShmDcCommonData->PayPass_flag[gunIndex] == FALSE)
  3497. pSysInfo->SystemPage = _PAGE_PAYING;
  3498. else
  3499. pSysInfo->SystemPage = _PAGE_COMPLETE;
  3500. } else {
  3501. systemPageRestoreInit();
  3502. }
  3503. }
  3504. */
  3505. ClearDetectPluginFlag(gunIndex);
  3506. if (pDcChargingInfo->SystemStatus != S_FAULT)
  3507. {
  3508. UpdateErrorCodeToOcpp(gunIndex);
  3509. setChargerMode(gunIndex, MODE_FAULT);
  3510. }
  3511. }
  3512. void AuthorizeStopCharging(uint8_t gunIndex)
  3513. {
  3514. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3515. checkOCPPReqStop(gunIndex);
  3516. //RfidStopCharging();
  3517. }
  3518. void showversion()
  3519. {
  3520. char str[10240];
  3521. int len = 0;
  3522. uint8_t index = 0;
  3523. len += sprintf(&str[len],"\n=========== Dispenser info =========== \n");
  3524. len += sprintf(&str[len],"SW Version = %s\n", fwVersion);
  3525. len += sprintf(&str[len],"Debug Version = %s\n",DebugVersion);
  3526. len += sprintf(&str[len],"ModelName = %s\n", pSysConfig->ModelName);
  3527. len += sprintf(&str[len],"Relay Board FW Rev = %s\n", pSysInfo->RelayModuleFwRev);
  3528. len += sprintf(&str[len],"Fan Board FW Rev = %s\n", pSysInfo->FanModuleFwRev);
  3529. len += sprintf(&str[len],"Primary FW Rev = %s\n", pSysInfo->CsuPrimFwRev);
  3530. len += sprintf(&str[len],"LED FW Rev = %s\n", pSysInfo->LedModuleFwRev);
  3531. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  3532. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  3533. if (pDcChargingInfo->Type == _Type_Chademo) {
  3534. len += sprintf(&str[len],"CHAdeMO[%d] FW Rev = %s\n",
  3535. index,
  3536. ShmCHAdeMOData->evse[pDcChargingInfo->type_index].version);
  3537. } else if (pDcChargingInfo->Type == _Type_GB) {
  3538. len += sprintf(&str[len],"GBT[%d] FW Rev = %s\n",
  3539. index,
  3540. ShmGBTData->evse[pDcChargingInfo->type_index].version);
  3541. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3542. len += sprintf(&str[len],"CCS[%d] FW Rev = %s\n",
  3543. index,
  3544. ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].version);
  3545. }
  3546. }
  3547. len += sprintf(&str[len],"==================================== ");
  3548. log_info("%s",str);
  3549. }
  3550. void SetNatural200AGunOTPValue(struct DERATING_BY_OTP* deratingByConnOtp)
  3551. {
  3552. memset(&deratingByConnOtp->deratingTargetRate[0], 200, sizeof(deratingByConnOtp->deratingTargetRate));
  3553. }
  3554. void SetNatural300AGunOTPValue(struct DERATING_BY_OTP* deratingByConnOtp)
  3555. {
  3556. deratingByConnOtp->deratingTargetCurrent[0] = 500;
  3557. deratingByConnOtp->deratingTargetCurrent[1] = 300;
  3558. deratingByConnOtp->deratingTargetCurrent[2] = 100;
  3559. deratingByConnOtp->deratingTargetCurrent[3] = 100;
  3560. deratingByConnOtp->deratingTargetCurrent[4] = 100;
  3561. }
  3562. void SetLiquidCoolGunOTPValue(struct DERATING_BY_OTP* deratingByConnOtp)
  3563. {
  3564. deratingByConnOtp->deratingTargetCurrent[0] = 500;
  3565. deratingByConnOtp->deratingTargetCurrent[1] = 400;
  3566. deratingByConnOtp->deratingTargetCurrent[2] = 300;
  3567. deratingByConnOtp->deratingTargetCurrent[3] = 300;
  3568. deratingByConnOtp->deratingTargetCurrent[4] = 300;
  3569. }
  3570. void SetCHAdeMoTypeJOTPValue(uint8_t regulation,struct DERATING_BY_OTP* deratingByConnOtp)
  3571. {
  3572. if (regulation == 'J') {
  3573. memset(&deratingByConnOtp->deratingTargetRate[0], 125, sizeof(deratingByConnOtp->deratingTargetRate));
  3574. } else {
  3575. memset(&deratingByConnOtp->deratingTargetRate[0], 120, sizeof(deratingByConnOtp->deratingTargetRate));
  3576. }
  3577. }
  3578. void SetCHAdeMoTypeKOTPValue(struct DERATING_BY_OTP* deratingByConnOtp)
  3579. {
  3580. memset(&deratingByConnOtp->deratingTargetRate[0], 200, sizeof(deratingByConnOtp->deratingTargetRate));
  3581. }
  3582. void SetCHAdeMoTypeSJOTPValue(struct DERATING_BY_OTP* deratingByConnOtp)
  3583. {
  3584. deratingByConnOtp->deratingTargetRate[0] = 1;
  3585. deratingByConnOtp->deratingTargetRate[1] = 0.8;
  3586. }
  3587. static void SetGunTypeOTPValue(void)
  3588. {
  3589. //struct ChargingInfoData* chargingData_2 = NULL;
  3590. uint8_t Gun1Type = 0;
  3591. uint8_t Gun2Type = 0;
  3592. int i;
  3593. if (pSysConfig->TotalConnectorCount == 1) {
  3594. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(0);
  3595. Gun1Type = pSysConfig->ModelName[7];
  3596. Gun2Type = pSysConfig->ModelName[7];
  3597. switch (Gun1Type) {
  3598. //CHAdeMo
  3599. case 'J':
  3600. SetCHAdeMoTypeJOTPValue(pSysConfig->ModelName[3], &pDcChargingInfo->deratingByConnOtp);
  3601. break;
  3602. case 'K':
  3603. SetCHAdeMoTypeKOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3604. break;
  3605. case 'S':
  3606. SetCHAdeMoTypeSJOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3607. break;
  3608. // 風冷200A以下
  3609. case 'U':
  3610. case 'E':
  3611. case 'M':
  3612. case 'N':
  3613. SetNatural200AGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3614. break;
  3615. // 風冷300A
  3616. case 'T':
  3617. case 'D':
  3618. SetNatural300AGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3619. break;
  3620. // 水冷
  3621. case 'V':
  3622. case 'F':
  3623. case 'P':
  3624. case 'R':
  3625. SetLiquidCoolGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3626. break;
  3627. }
  3628. } else if (pSysConfig->TotalConnectorCount == 2) {
  3629. Gun1Type = pSysConfig->ModelName[7];
  3630. Gun2Type = pSysConfig->ModelName[9];
  3631. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  3632. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(i);
  3633. pDcChargingInfo->deratingByConnOtp.isNeedDerating = GUNOTPDERATING;
  3634. switch (i == 0 ? Gun1Type : Gun2Type) {
  3635. //CHAdeMo
  3636. case 'J':
  3637. SetCHAdeMoTypeJOTPValue(pSysConfig->ModelName[3], &pDcChargingInfo->deratingByConnOtp);
  3638. break;
  3639. case 'K':
  3640. SetCHAdeMoTypeKOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3641. break;
  3642. case 'S':
  3643. SetCHAdeMoTypeSJOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3644. break;
  3645. // 風冷200A以下
  3646. case 'U':
  3647. case 'E':
  3648. case 'M':
  3649. case 'N':
  3650. SetNatural200AGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3651. break;
  3652. // 風冷300A
  3653. case 'T':
  3654. case 'D':
  3655. SetNatural300AGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3656. break;
  3657. // 水冷
  3658. case 'V':
  3659. case 'F':
  3660. case 'P':
  3661. case 'R':
  3662. SetLiquidCoolGunOTPValue(&pDcChargingInfo->deratingByConnOtp);
  3663. break;
  3664. }
  3665. }
  3666. }
  3667. }
  3668. int main(void)
  3669. {
  3670. bool isModelNameMatch = true;
  3671. uint8_t _ocppProfileChkFlag;
  3672. uint8_t gunIndex = 0;
  3673. if (CreateAllCsuShareMemory() == FAIL) {
  3674. log_error("create share memory error");
  3675. return FAIL;
  3676. }
  3677. ClearAllShmMemParameter();
  3678. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  3679. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  3680. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  3681. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  3682. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  3683. pInfoCode = (struct InfoCodeData *)GetShmInfoCodeData();
  3684. ShmPsuData = (struct PsuData *)GetShmPsuData();
  3685. ShmCHAdeMOData = (struct CHAdeMOData *)GetShmCHAdeMOData();
  3686. ShmGBTData = (struct GBTData *)GetShmGBTData();
  3687. ShmCcsData = (struct CcsData *)GetShmCcsData();
  3688. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  3689. ShmFanModuleData = (struct FanModuleData *)GetShmFanModuleData();
  3690. ShmRelayModuleData = (struct RelayModuleData *)GetShmRelayModuleData();
  3691. ShmLedModuleData = (struct LedModuleData *)GetShmLedModuleData();
  3692. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  3693. ShmOCPP20Data = (struct OCPP20Data*)GetShmOCPP20Data();
  3694. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  3695. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  3696. ShmSysConfigAndInfo = (struct SysConfigAndInfo *)GetShmSysConfigAndInfo();
  3697. ShmStatusCodeData = (struct StatusCodeData *)GetShmStatusCodeData();
  3698. log_info(" ********************************************************");
  3699. log_info(" ****************** FileSystem Boot up *****************");
  3700. log_info(" ********************************************************");
  3701. log_info(" ****************** Project:DD360Tcc *****************");
  3702. log_info(" ********************************************************");
  3703. if (!InitialSystemDefaultConfig()) {
  3704. log_error("InitialSystemDefaultConfig NG ");
  3705. //StopProcessingLoop();
  3706. sleep(5);
  3707. system("reboot -f");
  3708. }
  3709. CheckGunTypeFromHw();
  3710. CheckIsAlternatvieByModelName();
  3711. InitialShareMemoryInfo();
  3712. GetFirmwareVersion();
  3713. if (!CheckConnectorTypeStatus()) {
  3714. isModelNameMatch = false;
  3715. }
  3716. Initialization();
  3717. SpawnTask();
  3718. log_info("Spawned all Task");
  3719. if (!isModelNameMatch) {
  3720. pAlarmCode->AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  3721. //ChangeLcmByIndex(_LCM_ERROR);
  3722. ChangeLcmByIndex(_PAGE_MAINTAIN);
  3723. // Module Name 與硬體對應不正確
  3724. log_error("Module Name & HW info none match. ");
  3725. sleep(3);
  3726. KillAllTask();
  3727. StopProcessingLoop();
  3728. }
  3729. CreateTimeoutFork();
  3730. log_info("Start self test... ");
  3731. SelfTestRun();
  3732. StopSystemTimeoutDet();
  3733. log_info("Self test finished : SelfTestSeq = %d, Work_Step = %d ",
  3734. pSysInfo->SelfTestSeq,
  3735. ShmPsuData->Work_Step);
  3736. if (pSysInfo->SelfTestSeq == _STEST_FAIL || ShmPsuData->Work_Step == _NO_WORKING ||
  3737. pInfoCode->InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES ) {
  3738. ChangeLcmByIndex(_PAGE_MAINTAIN);
  3739. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3740. setChargerMode(gunIndex, MODE_MAINTAIN);
  3741. }
  3742. } else {
  3743. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3744. setChargerMode(gunIndex, MODE_IDLE);
  3745. }
  3746. }
  3747. // Local DB
  3748. if (DB_Open() != PASS) {
  3749. log_info("DB_Open fail. ");
  3750. isDb_ready = false;
  3751. } else {
  3752. isDb_ready = true;
  3753. for (int _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  3754. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  3755. pDcChargingInfo->IsAvailable = DB_Get_Operactive(_index);
  3756. }
  3757. DB_Reboot_Record();
  3758. }
  3759. if (PowerDB_Open() != PASS) {
  3760. log_info("Power DB_Open fail. ");
  3761. isDb_ready = false;
  3762. } else {
  3763. int _retry = 0;
  3764. isDb_ready = true;
  3765. for (int _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  3766. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(_index);
  3767. while (_retry < 3) {
  3768. if (DB_Get_PowerConsumption(_index)) {
  3769. break;
  3770. } else {
  3771. sleep(1);
  3772. _retry++;
  3773. if (_retry == 3) {
  3774. DB_Insert_PowerConsumption(_index, 0);
  3775. }
  3776. }
  3777. } // while
  3778. } // for
  3779. }
  3780. log_info("===== Create DB End ===== ");
  3781. ChangeLcmByIndex(_PAGE_IDLE);
  3782. sleep(1);
  3783. //***** 須新增的偵測 *****//
  3784. // 1. Thernal - 控制風扇轉速
  3785. // 2. ouput fuse - 控制風扇轉速
  3786. CreateRfidFork();
  3787. // Main loop
  3788. CheckFwSlotStatusLog();
  3789. //AdjustChargerCurrent();
  3790. GetClockTime(&_cmdMainPriority_time, NULL);
  3791. // GunIndexInfo *pGunIndexInfo = (GunIndexInfo *)GetGunIndexInfo();
  3792. CheckTaskAlive();
  3793. CreateWatchdog();
  3794. //SetGunTypeOTPValue();
  3795. showversion();
  3796. for (;;) {
  3797. CheckOcppStatus();
  3798. ChkPrimaryStatus();
  3799. if ((IsConnectorWholeIdle() ||
  3800. //(pSysInfo->PageIndex == _LCM_ERROR)) &&
  3801. (pSysInfo->PageIndex == _PAGE_MAINTAIN)) &&
  3802. (pSysInfo->SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  3803. ) {
  3804. CheckFactoryConfigFunction();
  3805. //CheckFwUpdateFunction();
  3806. }
  3807. // OCPP 邏輯
  3808. OcppRemoteStartChk();
  3809. // 當 AC 沒有搭上時,清除一些錯誤碼
  3810. ClearAlarmCodeWhenAcOff();
  3811. if ((GetClockTimeoutValue(_cmdMainPriority_time) / 1000) > 5000) {
  3812. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3813. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3814. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  3815. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  3816. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3817. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  3818. if (pDcChargingInfo->SystemStatus == S_CHARGING &&
  3819. _ocppProfileChkFlag == 12) {
  3820. ChargingProfileFlat(gunIndex);
  3821. _ocppProfileChkFlag = 0;
  3822. } else if (pDcChargingInfo->SystemStatus != S_CHARGING) {
  3823. ChargingProfileFlat(gunIndex);
  3824. _ocppProfileChkFlag = 0;
  3825. } else {
  3826. _ocppProfileChkFlag++;
  3827. }
  3828. }
  3829. checkGunOTPState(gunIndex); //check gun OTP
  3830. }
  3831. GetClockTime(&_cmdMainPriority_time, NULL);
  3832. }
  3833. gEvBoardErr.GunErrMessage = 0; //清除系統執行中的錯誤訊息
  3834. gChillerTempErr.TempErrMsg = 0;//清除系統執行中的錯誤訊息
  3835. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3836. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3837. // 重新收集各槍的錯誤狀態
  3838. collectError(gunIndex);
  3839. checkEvBoardAlarmState(pDcChargingInfo->Type);
  3840. }
  3841. checkChillerAlarmState();
  3842. // 確認當前錯誤 Level = 2 ?
  3843. ReviewCriticalAlarm();
  3844. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3845. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3846. CheckGpioInStatus();
  3847. CheckErrorOccurStatus(gunIndex);
  3848. // 確認 Relay Welding or Driving Fault
  3849. CheckRelayWeldingOrDrivingFault(gunIndex);
  3850. ChkOcppStatus(gunIndex);
  3851. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  3852. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  3853. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3854. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  3855. CheckSmartChargeProfile(gunIndex);
  3856. }
  3857. switch (pDcChargingInfo->SystemStatus) {
  3858. case S_IDLE:
  3859. if (isModeChange(gunIndex)) {
  3860. log_info("============================= S_IDLE(%x) ============================= ", gunIndex);
  3861. ResetIdleData(gunIndex);
  3862. }
  3863. isChargingAverageState();
  3864. // For RemoteStart Using
  3865. if (pDcChargingInfo->RemoteStartFlag == YES && pDcChargingInfo->IsAvailable) {
  3866. log_info("-------- IDLE Remote Start(%d) --------", gunIndex);
  3867. //ChangeGunSelectByIndex(gunIndex);
  3868. setChargerMode(gunIndex, MODE_AUTHORIZING);
  3869. break;
  3870. }
  3871. autoStartCharging(gunIndex);
  3872. // 讀卡邏輯
  3873. if (!ShmDcCommonData->TradeCancel && pSysInfo->CurGunSelected == gunIndex)
  3874. ScannerCardProcess(gunIndex);
  3875. if (pSysInfo->SystemPage == _PAGE_SELECT_GUN || pSysInfo->SystemPage == _PAGE_IDLE) {
  3876. //ShmDcCommonData->LineStatus[gunIndex] = 0;
  3877. StopGunInfoTimeoutDet(gunIndex);
  3878. }
  3879. if (pSysInfo->CurGunSelected == gunIndex) {
  3880. if (pSysInfo->SystemPage == _PAGE_BILL) {
  3881. StartSystemTimeoutDet(Timeout_AddLine);
  3882. if (/*ShmDcCommonData->LineStatus[gunIndex] == 1 ||*/ ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_ADD_FRIEND) {
  3883. pSysInfo->SystemPage = _PAGE_ADD_FRIEND;
  3884. log_info("Change to Add Line Friend Page");
  3885. }
  3886. } else if (pSysInfo->SystemPage == _PAGE_ADD_FRIEND) {
  3887. StopSystemTimeoutDet();
  3888. StartGunInfoTimeoutDet(gunIndex, Timeout_LineReigster);
  3889. if (ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_CREDITCARD_INVOICE) {
  3890. StopGunInfoTimeoutDet(gunIndex);
  3891. pSysInfo->SystemPage = _PAGE_SELECT_PAY;
  3892. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = FALSE;
  3893. log_info("Change to Select Pay Page");
  3894. }
  3895. if (ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_CREDITCARD_DONATE) {
  3896. StopGunInfoTimeoutDet(gunIndex);
  3897. pSysInfo->SystemPage = _PAGE_SELECT_PAY;
  3898. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = TRUE;
  3899. log_info("Change to Select Pay Page");
  3900. }
  3901. }
  3902. }
  3903. // 檢查預約
  3904. if (ShmDcCommonData->pGunInfo[gunIndex].ReservationStatus) {
  3905. setChargerMode(gunIndex, MODE_RESERVATION);
  3906. }
  3907. goto CheckStatus;
  3908. break;
  3909. case S_RESERVATION:
  3910. if (isModeChange(gunIndex)) {
  3911. log_info("============================= S_RESERVATION(%x) ============================= ", gunIndex);
  3912. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  3913. }
  3914. if (pDcChargingInfo->RemoteStartFlag == YES && pDcChargingInfo->IsAvailable) {
  3915. log_info("-------- S_RESERVATION Remote Start(%d) --------", gunIndex);
  3916. //ChangeGunSelectByIndex(gunIndex);
  3917. setChargerMode(gunIndex, MODE_AUTHORIZING);
  3918. break;
  3919. }
  3920. // 讀卡邏輯
  3921. if (!ShmDcCommonData->TradeCancel && pSysInfo->CurGunSelected == gunIndex)
  3922. ScannerCardProcess(gunIndex);
  3923. // 檢查預約
  3924. if (!ShmDcCommonData->pGunInfo[gunIndex].ReservationStatus) {
  3925. setChargerMode(gunIndex, MODE_IDLE);
  3926. }
  3927. goto CheckStatus;
  3928. break;
  3929. case S_FAULT:
  3930. if (isModeChange(gunIndex)) {
  3931. if (pDcChargingInfo->SystemStatus == S_FAULT) {
  3932. log_info("============================= S_FAULT(%x) ============================= ", gunIndex);
  3933. //pSysInfo->SystemPage = _LCM_ERROR;
  3934. //pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3935. }
  3936. }
  3937. CheckStatus:
  3938. if (pDcChargingInfo->IsAvailable == NO) {
  3939. setChargerMode(gunIndex, MODE_MAINTAIN);
  3940. break;
  3941. }
  3942. // Refresh Error Status
  3943. if (pSysWarning->Level == WARN_LV_ER) {
  3944. CheckErrorCode(gunIndex);
  3945. if (pSysInfo->CurGunSelected == gunIndex)
  3946. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3947. continue;
  3948. }
  3949. if (PrecheckIsPass(gunIndex) == false) {
  3950. log_error("Relay welding");
  3951. setChargerMode(gunIndex, MODE_FAULT);
  3952. if (gunIndex == pSysInfo->CurGunSelected) {
  3953. //pSysInfo->SystemPage = _LCM_ERROR;
  3954. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  3955. }
  3956. break;
  3957. }
  3958. if (pDcChargingInfo->SystemStatus == S_FAULT) {
  3959. setChargerMode(gunIndex, MODE_IDLE);
  3960. systemPageRestoreInit();
  3961. }
  3962. break;
  3963. case S_AUTHORIZING:
  3964. if (isModeChange(gunIndex)) {
  3965. log_info("============================= S_AUTHORIZING(%x) ============================= ", gunIndex);
  3966. StartGunInfoTimeoutDet(gunIndex, Timeout_AuthorizingForStop);
  3967. }
  3968. if (pSysInfo->CurGunSelected == gunIndex)
  3969. ScannerCardProcess(gunIndex);
  3970. if (pSysInfo->SystemPage == _PAGE_PLUGIN && ShmDcCommonData->TradeCancel == FALSE) {
  3971. pDcChargingInfo->Replug_flag = TRUE;
  3972. StopSystemTimeoutDet();
  3973. }
  3974. if (isDetectPlugin(gunIndex)) {
  3975. StartGunInfoTimeoutDet(gunIndex, Timeout_WaitPlug);
  3976. // 卡號驗證成功後,等待充電槍插入充電車
  3977. if (pDcChargingInfo->RemoteStartFlag == YES) {
  3978. if (pDcChargingInfo->ConnectorPlugIn == YES &&
  3979. pDcChargingInfo->IsAvailable ) {
  3980. log_info("-------- S_AUTHORIZING Remote Start(%d) --------", gunIndex);
  3981. pDcChargingInfo->RemoteStartFlag = NO;
  3982. pDcChargingInfo->isRemoteStart = YES; //DS60-120
  3983. //ChangeGunSelectByIndex(gunIndex);
  3984. //AddPlugInTimes(gunIndex);
  3985. setChargerMode(gunIndex, MODE_REASSIGN_CHECK);
  3986. strcpy((char *)pDcChargingInfo->StartUserId, "");
  3987. ClearDetectPluginFlag(gunIndex);
  3988. continue;
  3989. }
  3990. } else if (pSysInfo->OrderCharging == NO_DEFINE) {
  3991. if (pDcChargingInfo->ConnectorPlugIn == YES &&
  3992. pDcChargingInfo->IsAvailable &&
  3993. pDcChargingInfo->SystemStatus == S_AUTHORIZING &&
  3994. ShmDcCommonData->AuthPass_flag[gunIndex] == TRUE &&
  3995. (waitRightGunPlugIt(gunIndex) == PASS ||
  3996. waitLeftGunPlugIt(gunIndex) == PASS)
  3997. ) {
  3998. log_info("-------- RFID/Auto Start(%d) --------", gunIndex);
  3999. //ChangeGunSelectByIndex(gunIndex);
  4000. //AddPlugInTimes(gunIndex);
  4001. pDcChargingInfo->isRemoteStart = NO;
  4002. strcpy((char *)pDcChargingInfo->StartUserId, (char *)pSysConfig->UserId);
  4003. log_info("index = %d, CardNumber = %s ",
  4004. gunIndex,
  4005. pDcChargingInfo->StartUserId);
  4006. strcpy((char *)pSysConfig->UserId, "");
  4007. // 當前操作的槍號,進入 Preparing
  4008. setChargerMode(gunIndex, MODE_REASSIGN_CHECK);
  4009. ClearDetectPluginFlag(gunIndex);
  4010. continue;
  4011. }
  4012. }
  4013. if (!GetIsCardScan() && pSysInfo->CurGunSelected == gunIndex &&
  4014. pSysInfo->SystemPage != _PAGE_AUTHORIZE_FAIL &&
  4015. pSysInfo->SystemPage != _PAGE_SENSING) {
  4016. pSysInfo->SystemPage = _PAGE_PLUGIN;
  4017. } else if (!GetIsCardScan() && pSysInfo->SystemPage == _PAGE_IDLE &&
  4018. pDcChargingInfo->RemoteStartFlag == YES) {
  4019. pSysInfo->CurGunSelected = gunIndex;
  4020. }
  4021. } else if (ShmDcCommonData->AuthPass_flag[gunIndex] && pSysInfo->CurGunSelected == gunIndex &&
  4022. pSysInfo->SystemPage != _PAGE_AUTHORIZE_FAIL) {
  4023. pSysInfo->SystemPage = _PAGE_SENSING;
  4024. }
  4025. break;
  4026. case S_REASSIGN_CHECK: {
  4027. if (isModeChange(gunIndex)) {
  4028. log_info("============================= S_REASSIGN_CHECK(%x) ============================= ", gunIndex);
  4029. if (pSysInfo->OrderCharging != NO_DEFINE) {
  4030. pSysInfo->OrderCharging = NO_DEFINE;
  4031. pDcChargingInfo->_SaftyDetect = FALSE;
  4032. }
  4033. pDcChargingInfo->Replug_flag = TRUE;
  4034. StopSystemTimeoutDet();
  4035. StopGunInfoTimeoutDet(gunIndex);
  4036. }
  4037. setChargerMode(gunIndex, S_PREPARNING);
  4038. if (pSysInfo->CurGunSelected == gunIndex) {
  4039. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4040. }
  4041. }
  4042. break;
  4043. case S_PREPARNING:
  4044. if (isModeChange(gunIndex)) {
  4045. log_info("============================= S_PREPARNING(%x) ============================= ", gunIndex);
  4046. StopGunInfoTimeoutDet(gunIndex);
  4047. StartGunInfoTimeoutDet(gunIndex, Timeout_Preparing);
  4048. }
  4049. if (ShmPsuData->SystemPresentPsuQuantity > 0 &&
  4050. ShmPsuData->SystemAvailablePower > 10 &&
  4051. GetClockTimeoutValue(pDcChargingInfo->ConnectorTimeout) >= 1000000) {
  4052. setChargerMode(gunIndex, MODE_PREPARE_FOR_EV);
  4053. }
  4054. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4055. if (pSysInfo->CurGunSelected == gunIndex) {
  4056. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4057. }
  4058. break;
  4059. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  4060. if (isModeChange(gunIndex)) {
  4061. log_info("============================= S_PREPARING_FOR_EV(%x) ============================= ", gunIndex);
  4062. log_info("Waitting for Gun locked.");
  4063. StopGunInfoTimeoutDet(gunIndex);
  4064. StartGunInfoTimeoutDet(gunIndex, Timeout_EvChargingDet);
  4065. }
  4066. if (pDcChargingInfo->Type == _Type_Chademo) {
  4067. if (isEvGunLocked_chademo(gunIndex) == YES) {
  4068. log_info("Gun Locked");
  4069. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  4070. }
  4071. } else if (pDcChargingInfo->Type == _Type_GB) {
  4072. if (isEvGunLocked_gb(gunIndex) == YES) {
  4073. log_info("Gun Locked");
  4074. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  4075. }
  4076. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  4077. if (isEvGunLocked_ccs(gunIndex) == YES) {
  4078. log_info("Gun Locked");
  4079. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  4080. }
  4081. }
  4082. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4083. if (pSysInfo->CurGunSelected == gunIndex) {
  4084. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4085. }
  4086. break;
  4087. case S_PREPARING_FOR_EVSE: // Ground fault test, 等待 Relay Board 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  4088. if (isModeChange(gunIndex)) {
  4089. log_info("============================= S_PREPARING_FOR_EVSE(%x) ============================= ", gunIndex);
  4090. StopGunInfoTimeoutDet(gunIndex);
  4091. StartGunInfoTimeoutDet(gunIndex, Timeout_EvseChargingDet);
  4092. }
  4093. checkPileEndGfdResult(gunIndex, pDcChargingInfo->Type, pDcChargingInfo->SystemStatus);
  4094. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4095. if (pSysInfo->CurGunSelected == gunIndex) {
  4096. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4097. }
  4098. break;
  4099. case S_CCS_PRECHARGE_ST0:
  4100. if (isModeChange(gunIndex)) {
  4101. log_info("============================= CCS Precharge Processing 1(%x) ============================= ", gunIndex);
  4102. StopGunInfoTimeoutDet(gunIndex);
  4103. StartGunInfoTimeoutDet(gunIndex, Timeout_ForCcsPrechargeDet);
  4104. }
  4105. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  4106. // GFD 錯誤停止
  4107. RecordAlarmCode(gunIndex, "012235");
  4108. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  4109. }
  4110. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4111. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  4112. // 切換 D+ Relay to Precharge Relay
  4113. if (isPrechargeStatus_ccs(gunIndex) == 39 ||
  4114. isPrechargeStatus_ccs(gunIndex) == 40) {
  4115. if ((pDcChargingInfo->RelayKPK2Status == YES || pDcChargingInfo->PantographFlag == YES) &&
  4116. pDcChargingInfo->PrechargeStatus != PRECHARGE_READY)
  4117. //if (pDcChargingInfo->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  4118. {
  4119. pDcChargingInfo->PrechargeStatus = PRECHARGE_READY;
  4120. }
  4121. } else if (isPrechargeStatus_ccs(gunIndex) == 45 ||
  4122. isPrechargeStatus_ccs(gunIndex) == 46
  4123. ) {
  4124. setChargerMode(gunIndex, MODE_CCS_PRECHARGE_STEP1);
  4125. }
  4126. if (pSysInfo->CurGunSelected == gunIndex) {
  4127. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4128. }
  4129. break;
  4130. case S_CCS_PRECHARGE_ST1:
  4131. if (isModeChange(gunIndex)) {
  4132. log_info("============================= CCS Precharge Processing 2(%x) ============================= ", gunIndex);
  4133. }
  4134. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  4135. // GFD 錯誤停止
  4136. RecordAlarmCode(gunIndex, "012235");
  4137. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  4138. }
  4139. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4140. // 等待小板通知進入充電
  4141. // 切換 D+ Relay to Precharge Relay
  4142. if (pDcChargingInfo->RelayK1K2Status == YES || pDcChargingInfo->PantographFlag == YES) {
  4143. pDcChargingInfo->PrechargeStatus = PRECHARGE_READY;
  4144. // pSysInfo->CurGunSelected = gunIndex;
  4145. setChargerMode(gunIndex, MODE_CHARGING);
  4146. }
  4147. if (pSysInfo->CurGunSelected == gunIndex) {
  4148. pSysInfo->SystemPage = _PAGE_PRECHARGE;
  4149. }
  4150. break;
  4151. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  4152. if (isModeChange(gunIndex)) {
  4153. log_info("============================= S_CHARGING(%x) ============================= ", gunIndex);
  4154. StopGunInfoTimeoutDet(gunIndex);
  4155. ftime(&startChargingTime[gunIndex]);
  4156. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].ResponseIdTagInfo.Status, ""); //DS60-120 add
  4157. ChangeStartOrStopDateTime(YES, gunIndex);
  4158. pDcChargingInfo->Replug_flag = FALSE;
  4159. ShmDcCommonData->finalcost_flag[gunIndex] = FALSE;
  4160. ShmDcCommonData->TransactionInfo[gunIndex].isIntoCharge = TRUE;
  4161. ShmDcCommonData->RecordEnergyTime[gunIndex] = time((time_t*)NULL);
  4162. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  4163. }
  4164. if (ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionConf) {
  4165. ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionConf = NO;
  4166. }
  4167. ftime(&endChargingTime[gunIndex]);
  4168. pDcChargingInfo->PresentChargedDuration = DiffTimeb(startChargingTime[gunIndex], endChargingTime[gunIndex]);
  4169. ShmDcCommonData->TransactionInfo[gunIndex].Energy = pDcChargingInfo->PresentChargedEnergy;
  4170. // 每秒紀錄使用電量到資料庫內
  4171. if ((time((time_t*)NULL) - ShmDcCommonData->RecordEnergyTime[gunIndex]) >= 1) {
  4172. ShmDcCommonData->RecordEnergyTime[gunIndex] = time((time_t*)NULL);
  4173. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  4174. }
  4175. if ((time((time_t*)NULL) - ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime) >= 5) {
  4176. ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime = time((time_t*)NULL);
  4177. if (ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption != 0)
  4178. DB_Update_PowerConsumption(gunIndex, ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption);
  4179. }
  4180. checkPileEndGfdResult(gunIndex, pDcChargingInfo->Type, pDcChargingInfo->SystemStatus);
  4181. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  4182. // Check Stop Charging
  4183. AuthorizeStopCharging(gunIndex);
  4184. if (pSysInfo->SystemPage == _PAGE_STOP_CONFIRM_LEFT ||
  4185. pSysInfo->SystemPage == _PAGE_STOP_CONFIRM_RIGHT)
  4186. break;
  4187. // LCM => Charging
  4188. if (pSysInfo->CurGunSelected == gunIndex && pSysInfo->SystemPage != _PAGE_IDLE &&
  4189. pSysInfo->SystemPage != _PAGE_SELECT_GUN) {
  4190. pSysInfo->SystemPage = _PAGE_CHARGING;
  4191. }
  4192. break;
  4193. case S_TERMINATING:
  4194. if (isModeChange(gunIndex)) {
  4195. log_info("============================= S_TERMINATING(%x) ============================= ", gunIndex);
  4196. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  4197. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Local");
  4198. }
  4199. ShmDcCommonData->StopCharge[gunIndex] = TRUE;
  4200. if (ShmDcCommonData->is_AutoStart[gunIndex]) {
  4201. ShmDcCommonData->finalcost_flag[gunIndex] = TRUE;
  4202. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  4203. }
  4204. if (ShmDcCommonData->finalcost_flag[gunIndex] == FALSE && pSysInfo->CurGunSelected == gunIndex) {
  4205. pSysInfo->SystemPage = _PAGE_PAYING;
  4206. }
  4207. StartSystemTimeoutDet(Timeout_Terminating);
  4208. StartGunInfoTimeoutDet(gunIndex, Timeout_FinalCost);
  4209. ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime = time((time_t*)NULL);
  4210. if (ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption != 0)
  4211. DB_Update_PowerConsumption(gunIndex, ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption);
  4212. }
  4213. // For Precharging timeout
  4214. if (pDcChargingInfo->Type == _Type_Chademo) {
  4215. if (isEvStopCharging_chademo(gunIndex) == YES ||
  4216. isPrechargeStatus_chademo(gunIndex) <= 0) {
  4217. setChargerMode(gunIndex, MODE_COMPLETE);
  4218. }
  4219. } else if (pDcChargingInfo->Type == _Type_GB) {
  4220. if (isEvStopCharging_gb(gunIndex) == YES ||
  4221. isPrechargeStatus_gb(gunIndex) <= 0) {
  4222. setChargerMode(gunIndex, MODE_COMPLETE);
  4223. }
  4224. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  4225. if (isEvStopCharging_ccs(gunIndex) == YES &&
  4226. (isPrechargeStatus_ccs(gunIndex) >= 53 ||
  4227. isPrechargeStatus_ccs(gunIndex) == 0 ||
  4228. isPrechargeStatus_ccs(gunIndex) == 13 ||
  4229. isPrechargeStatus_ccs(gunIndex) == 14)
  4230. ) {
  4231. setChargerMode(gunIndex, MODE_COMPLETE);
  4232. }
  4233. }
  4234. if (pDcChargingInfo->Replug_flag == TRUE && pSysInfo->CurGunSelected == gunIndex) {
  4235. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  4236. break;
  4237. }
  4238. if (pSysInfo->CurGunSelected == gunIndex &&
  4239. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4240. if (ShmDcCommonData->PayPass_flag[gunIndex] == TRUE)
  4241. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4242. else
  4243. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  4244. }
  4245. break;
  4246. case S_COMPLETE:
  4247. if (isModeChange(gunIndex)) {
  4248. log_info ("============================= S_COMPLETE(%x) ============================= ", gunIndex);
  4249. if (strcmp((char *)pDcChargingInfo->StartDateTime, "") != EQUAL) {
  4250. OcppStopTransation(gunIndex);
  4251. }
  4252. TheEndCharging(gunIndex);
  4253. if (pDcChargingInfo->Replug_flag != TRUE && pSysInfo->SystemPage != _PAGE_PLUGOUT)
  4254. StopSystemTimeoutDet();
  4255. if (ShmDcCommonData->is_AutoStart[gunIndex] == TRUE && pSysInfo->CurGunSelected == gunIndex)
  4256. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4257. }
  4258. //if (pSysInfo->SystemPage == _LCM_ERROR) {
  4259. if (pSysInfo->SystemPage == _PAGE_MAINTAIN) {
  4260. break;
  4261. }
  4262. if (pSysInfo->CurGunSelected == gunIndex && ShmDcCommonData->is_exit[gunIndex]) {
  4263. pSysInfo->SystemPage = _PAGE_EXIT;
  4264. break;
  4265. }
  4266. if (pDcChargingInfo->ConnectorPlugIn == NO &&
  4267. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4268. if (pSysInfo->CurGunSelected == gunIndex) {
  4269. StartGunInfoTimeoutDet(gunIndex, Timeout_ExitPage);
  4270. }
  4271. if (pDcChargingInfo->TimeoutFlag != Timeout_ExitPage &&
  4272. pDcChargingInfo->TimeoutFlag != Timeout_FinalCost) {
  4273. StartGunInfoTimeoutDet(gunIndex, Timeout_CompletPlugout);
  4274. }
  4275. }
  4276. if (pDcChargingInfo->Replug_flag == TRUE && pSysInfo->CurGunSelected == gunIndex) {
  4277. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  4278. break;
  4279. }
  4280. if (pSysInfo->CurGunSelected == gunIndex &&
  4281. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4282. if (ShmDcCommonData->PayPass_flag[gunIndex] == TRUE)
  4283. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4284. else
  4285. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  4286. }
  4287. break;
  4288. case S_ALARM:
  4289. if (isModeChange(gunIndex)) {
  4290. log_info("============================= S_ALARM(%x) ============================= ", gunIndex);
  4291. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  4292. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Local");
  4293. }
  4294. UpdateErrorCodeToOcpp(gunIndex);
  4295. if (strcmp((char *)pDcChargingInfo->StartDateTime, "") != EQUAL) {
  4296. OcppStopTransation(gunIndex);
  4297. }
  4298. TheEndCharging(gunIndex);
  4299. StopGunInfoTimeoutDet(gunIndex);
  4300. ShmDcCommonData->StopCharge[gunIndex] = TRUE;
  4301. if (ShmDcCommonData->is_AutoStart[gunIndex]) {
  4302. ShmDcCommonData->finalcost_flag[gunIndex] = TRUE;
  4303. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  4304. }
  4305. if (pDcChargingInfo->Replug_flag == TRUE) {
  4306. } else {
  4307. StartGunInfoTimeoutDet(gunIndex, Timeout_FinalCost);
  4308. if(ShmDcCommonData->finalcost_flag[gunIndex] == FALSE && pSysInfo->CurGunSelected == gunIndex) {
  4309. pSysInfo->SystemPage = _PAGE_PAYING;
  4310. }
  4311. }
  4312. ShmDcCommonData->pGunInfo[gunIndex].RecordEnergyTime = time((time_t*)NULL);
  4313. if (ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption != 0)
  4314. DB_Update_PowerConsumption(gunIndex, ShmDcCommonData->pGunInfo[gunIndex].PowerConsumption);
  4315. }
  4316. if (pSysWarning->Level == WARN_LV_ER) {
  4317. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4318. continue;
  4319. }
  4320. if (pSysInfo->CurGunSelected == gunIndex && ShmDcCommonData->is_exit[gunIndex]) {
  4321. pSysInfo->SystemPage = _PAGE_EXIT;
  4322. break;
  4323. }
  4324. if (pDcChargingInfo->ConnectorPlugIn == NO &&
  4325. pSysInfo->SystemPage != _PAGE_PAYING && pSysInfo->SystemPage != _PAGE_SENSING) {
  4326. if (pSysInfo->CurGunSelected == gunIndex) {
  4327. StartGunInfoTimeoutDet(gunIndex, Timeout_ExitPage);
  4328. }
  4329. if (pDcChargingInfo->TimeoutFlag != Timeout_ExitPage &&
  4330. pDcChargingInfo->TimeoutFlag != Timeout_FinalCost) {
  4331. StartGunInfoTimeoutDet(gunIndex, Timeout_CompletPlugout);
  4332. }
  4333. }
  4334. if (pDcChargingInfo->Replug_flag == TRUE && pSysInfo->CurGunSelected == gunIndex) {
  4335. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  4336. break;
  4337. }
  4338. if (pSysInfo->CurGunSelected == gunIndex && pSysInfo->SystemPage != _PAGE_PAYING ) {
  4339. if (ShmDcCommonData->PayPass_flag[gunIndex] == TRUE)
  4340. pSysInfo->SystemPage = _PAGE_COMPLETE;
  4341. else
  4342. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  4343. }
  4344. break;
  4345. case S_MAINTAIN:
  4346. if (isModeChange(gunIndex)) {
  4347. log_info("============================= S_MAINTAIN(%x) ============================= ", gunIndex);
  4348. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4349. if (pSysInfo->FirmwareUpdate == YES) {
  4350. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4351. continue;
  4352. }
  4353. if (pDcChargingInfo->IsAvailable == NO) {
  4354. log_info("Gun%d is not availbale");
  4355. break;
  4356. }
  4357. if (pSysWarning->Level != WARN_LV_ER) {
  4358. if (!DisplaySelfTestFailReason()) { //DS60-120 add
  4359. log_info("Soft reboot for retry self-tets. ");
  4360. sleep(3);
  4361. system("killall OcppBackend &");
  4362. KillAllTask();
  4363. system("/usr/bin/run_evse_restart.sh");
  4364. }
  4365. }
  4366. sleep(3);
  4367. if (pSysWarning->Level == WARN_LV_ER) { //DS60-120 add
  4368. KillTaskExceptPrimary();
  4369. } else {
  4370. KillTask();
  4371. }
  4372. /*
  4373. if (pSysInfo->SelfTestSeq == _STEST_FAIL)
  4374. StopProcessingLoop();
  4375. */
  4376. }
  4377. if (pSysInfo->SelfTestSeq == _STEST_FAIL && pSysInfo->SystemTimeoutFlag != Timeout_SelftestChk) {
  4378. SelfTestRun();
  4379. StopSystemTimeoutDet();
  4380. sleep(3);
  4381. break;
  4382. }
  4383. if (pDcChargingInfo->IsAvailable && pSysInfo->SelfTestSeq == _STEST_COMPLETE) {
  4384. setChargerMode(gunIndex, MODE_IDLE);
  4385. }
  4386. if (pSysInfo->CurGunSelected == gunIndex) {
  4387. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4388. }
  4389. break;
  4390. case S_UPDATE:
  4391. if (isModeChange(gunIndex)) {
  4392. log_info("============================= S_UPDATE ============================= ");
  4393. }
  4394. if (pSysInfo->FirmwareUpdate == YES) {
  4395. pSysInfo->SystemPage = _PAGE_MAINTAIN;
  4396. continue;
  4397. } else {
  4398. }
  4399. break;
  4400. }//switch
  4401. }//for
  4402. //if (pSysInfo->SystemPage != _LCM_VIEW)
  4403. //ChangeLcmByIndex(pSysInfo->SystemPage);
  4404. TryFeedWatchdog();
  4405. usleep(WHILE_LOOP_TIME);
  4406. }
  4407. return FAIL;
  4408. }