main.c 177 KB

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