main.c 170 KB

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