main.c 178 KB

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