main.c 171 KB

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