main.c 106 KB

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