main.c 168 KB

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