main.c 176 KB

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