main.c 174 KB

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