main.c 175 KB

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