main.c 136 KB

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