main.c 193 KB

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