main.c 183 KB

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